24 Neumans Generator

Back to General discussions forum

Abdullah Anwar     2016-12-21 18:23:46
import java.util.Scanner;
public class NeumannsRandomGenerator {
public static int NRG(int a){
    int count = 0;
    int b = a * a;
    b = b/100;
    b = b % 10000;
    do {
        b = b * b;
        b = b / 100;
        b = b % 10000;
        count++;
    }while (a != b);
    return count;
}
public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    System.out.println("Enter Cases:");
    int cases = in.nextInt();
    for (int i = 0; i < cases; i++){
        int n = in.nextInt();
        System.out.println(NRG(n));
    }
}
}


That is my code below i've been trying to figure out Neumans generator but my while loop wont even
work could someone please help me?  
Quandray     2016-12-21 19:02:26
User avatar

Hi,

The problem description has these two examples

0001 -> 0000 -> 0000                   - came to loop after 2 iterations
4100 -> 8100 -> 6100 -> 2100 -> 4100   - came to loop after 4 iterations

Note that the first one does not return to the initial number 0001. It looks like the "while" in your code, is comparing the current value b with the initial value a, so it will never end.

Please login and solve 5 problems to be able to post at forum