2014-11-21 35 views
0

我正在試圖製作一個程序,輸出Fibonacci序列中某個用戶輸入數字的Pisano期間。該程序的話很好,直到我到達10.該程序循環無限。當我有120個剩餘數組時,我檢查數組(10的Pisano週期是60,因此爲了檢查數組是否重複,我至少需要60 * 2 = 120個剩餘數)。但是陣列不一樣。下面是陣列(的餘數爲1〜60和61〜120)Java - 皮薩諾期間計算錯誤

[1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, 7, 7, 4, 1, 5, 6, 1, 7, 8, 5, 3, 8, 1, 9, 0, 9, 9, 8, 7, 5, 2, 7, 9, 6, 5, 1, 6, 7, 3, 0, 3, 3, 6, 9, 5, 4, 9, 3, 2, 5, 7, 2, 9, 1, 0] 
    [1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, 7, 7, 4, 0, 4, 4, 4, 8, 2, 0, 4, 4, 8, 4, 0, 6, 6, 2, 6, 0, 6, 6, 4, 4, 6, 0, 6, 4, 0, 4, 2, 4, 6, 0, 8, 4, 0, 4, 4, 8, 4, 2, 8, 4, 4] 

這是我的代碼:

主代碼: `進口java.util.Scanner中;

公共類PisanoPeriod {

public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    boolean quit = false; 
    while (!quit) { 
     System.out.print("Enter a number: "); 
     int divisor = scan.nextInt(); 
     FibonacciSequence f = new FibonacciSequence(); 
     System.out.println("Computing..."); 
     System.out.println("The Pisano Period for "+divisor+" is: "+f.getPisanoPeriod(divisor));    
     printDivisor(20); 
     System.out.print("Do you want to do another one? (y/n): "); 
     String reply = scan.next(); 
     if (reply.equals("n") || reply.equals("no")) { 
      printDivisor(20); 
      System.out.println("Bye!"); 
      quit = true; 
     } 
    } 

} 

private static void printDivisor(int n) { 
    System.out.print("\n"); 
    for (int i=1; i<=n; i++) { 
     System.out.print("*"); 
     if (i==n) { 
      System.out.print("\n\n"); 
     } 
    } 
} 
} 

而且FibonacciSequence類,這裏的問題可能是: `進口java.util.Arrays中;

公共類FibonacciSequence {

private double[] Sequence = new double[2]; 
private BadNumberException badnumexception = new BadNumberException("Number of fibonnacci numbers is too small."); 
private TooLittleNumbersException tlnexception = new TooLittleNumbersException(); 

public FibonacciSequence() { 
    try { 
     generate(10); 
    } 
    catch(BadNumberException e) {System.out.println(e.getMessage());} 
} 

private int getSequenceLength() { 
    return Sequence.length; 
} 

private boolean checkForPattern(int[] array) { 
    int half_point = array.length/2; 
    boolean return_boolean = true; 
    for (int i=0; i<half_point; i++){ 
     if (array[i]!=array[i+half_point]){ 
      return_boolean = false; 

      break; 
     } 
     else { 
      if (i==half_point-1) { 
       System.out.println(Arrays.toString(Arrays.copyOfRange(array, 0, half_point))); 
       System.out.println(Arrays.toString(Arrays.copyOfRange(array, half_point, array.length))); 
      } 
     } 
    } 
    if (array.length==120){ 
     System.out.println(Arrays.toString(Arrays.copyOfRange(array, 0, half_point))); 
     System.out.println(Arrays.toString(Arrays.copyOfRange(array, half_point, array.length))); 
    } 
    return return_boolean; 
} 

public int getPisanoPeriod(int n) { 
    int return_value = -1; 
    int max_index_counter=0; 
    boolean error = true; 
    while (error) { 
     try { 
      boolean pattern_reached = false; 
      int[] remainder_array = new int[0]; 
      int index_counter = 0; 
      while (!pattern_reached) { 
       if (index_counter>=max_index_counter) { 
        max_index_counter = index_counter; 
        System.out.println(max_index_counter); 
       } 
       int[] temp_array = new int[remainder_array.length+1]; 
       for (int i=0; i<remainder_array.length; i++) { 
        temp_array[i] = remainder_array[i]; 
       } 
       remainder_array = temp_array; 
       remainder_array[index_counter] = (int) (Sequence[index_counter]%n); 
       //System.out.println(Arrays.toString(Sequence)); 
       if (remainder_array.length%2==0 && index_counter>n) 
        pattern_reached = checkForPattern(remainder_array); 
       index_counter++; 
      } 
      return_value = remainder_array.length/2; 
      error = false; 
     } 
     catch (IndexOutOfBoundsException e) { 
      try { 
       throw tlnexception; 
      } catch (TooLittleNumbersException a) { 
       try { 
        //if (getSequenceLength()<50) 
        generate(getSequenceLength()+1); 
        /*else 
         error=false;*/ 
        //System.out.println(getSequenceLength()+10); 
       } catch (BadNumberException b) {} 
      } 
     } 
    } 
    return return_value; 
} 

public void generate(int n) throws BadNumberException { 

    if (n<=2) throw badnumexception; 
    double[] generated_array = new double[n]; 
    generated_array[0] = 1; 
    generated_array[1] = 1; 
    for (int i=2; i<n; i++) { 
     generated_array[i] = generated_array[i-1]+generated_array[i-2]; 
     //System.out.println(generated_array[i]); 
    } 
    Sequence = generated_array; 
} 
} 

先感謝您的任何提示。

回答

1

的問題是顯然與鑄造機制在線路remainder_array[index_counter] = (int) (Sequence[index_counter]%n);

鑄造四捨五入的整數從雙降,所以餘竟然是在兩個陣列不同。由於內存容量不足,我無法使用很長時間。所以相反,我使用BigInteger類來處理序列數組,並解決了這個問題。