Home   Cover Cover Cover Cover
 

Number sequences

/csbook/solutions/18/A06.cs
using System;
using System.Collections;

// Generates a sequence of 4-digit numbers in which every number
// is the square of the middle digits of its predcessor
class NumberSequence {
  
  static void Main(string[] arg) {
    int num = 1234;
    if (arg.Length > 0) num = Convert.ToInt32(arg[0]);
    ArrayList list = new ArrayList();
    while (!list.Contains(num)) {
      list.Add(num);
      int x = num / 10 % 100;
      num = x * x;
    }
    foreach (int x in list) Console.Write(x + " ");
  }
}