0
import java.util.Scanner;

public class NeumannsRandomGenerator {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter cases:");
        int cases = in.nextInt();
        int iterations = 0;
        for (int i = 0; i <= cases; i++) {
            int a = in.nextInt();
            int res = ((a * a) / 100) % 10000;
            if(res == a){
                iterations++;
            }
            do {
                int b = ((res * res) / 100) % 10000;
                iterations++;
                b = res;
            } while (a != res);
            System.out.println(iterations);
        }
    }
}

I am trying to figure Neumans random generator For Example:

5761 - let it be the first number
5761 * 5761 = 33189121 - raised to power 2
33(1891)21 => 1891 - truncate to get the middle

1891 - it is the second number in the sequence
1891 * 1891 = 3575881 - raised to power 2 (add leading zero to get 8 digits)
03(5758)81 => 5758 - truncate to get the middle

5758 - it is the third number in the sequence (and so on...)

Please help why I am not getting any results:(

RamenChef
  • 5,533
  • 11
  • 28
  • 39
AAnwar007
  • 43
  • 1
  • 6

1 Answers1

0

I don't know the rules for an iteration where your number is padded like 0315. If the padded value would be 00099225, and the resulting number is 992, then this would be a good start. I also don't see your rule for re-generating, so it just recurses.

I'm doing this quickly, so there could be some unnecessary code here

public int getNeumansRandomNumber(int starting) {

    int nsquared = (int) Math.pow(starting, 2);
    int length = String.valueOf(nsquared).length();

    if (length < 8) {
        String zeroPad = "00000000";
        String padded = zeroPad.substring(length) + nsquared;
        System.out.println("padded="+padded);
        nsquared = Integer.valueOf(padded);
    }
    int middle = (nsquared % 1000000) / 100;
    System.out.println(middle);
    return getNeumansRandomNumber(middle);
}

References: 1, 2, and 3

Community
  • 1
  • 1
vphilipnyc
  • 6,607
  • 6
  • 44
  • 70