2016-10-16 23 views
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); 
     } 
    } 
} 

我試圖找出Neumans隨機生成 例如:我試圖找出Neumans隨機

5761 - 讓它成爲了第一個數字
5761 * 5761 = 33189121 - 提升到功率2
33(1891)21 => 1891 - 截斷,以獲得中間

1891 - 它是序列
1891 * 1891 = 3575881中的第二個數字 - 升高到功率2(添加前導零得到8位數字)
03(5758)81 => 5758 - 截斷,以獲得中間

5758 - 這是序列中的第三個數字(等等...)

請幫助爲什麼我沒有得到任何結果:(

回答

0

我不知道一個迭代的規則,其中你的號碼被填充爲0315.如果填充值爲00099225,結果數字爲992,那麼這將是一個好的開始。我也沒有看到你的規則重新生成,所以它只是遞歸。

我很快這樣做,所以有可能是這裏的一些不必要的代碼

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); 
} 

參考文獻:12,並3