2015-01-22 2 views
-4

그래서 N'th 피보나치 수를 찾을 수있는 자바 코드를 작성했습니다. 내 문제는 Java BigInteger mod 1000000007

System.out.println(answer.mod(1000000007)); 

를 작성하여 내가 그것을 간단하게 할 수있는 응답 % 1000000007. 내가 생각 출력이있다하지만 내가 그렇게하는 것을 허용하지 않습니다, 나는 (오류 "방법의 모드를 얻을 수 BigInteger 유형의 BigInteger는 인수에 사용할 수 없습니다. (int) "

누군가 제발 도와 드릴까요?

import java.io.IOException; 
import java.math.BigDecimal; 
import java.math.BigInteger; 
import java.util.Scanner; 

public class dmoj 
{ 
    public static void main(String[] args) throws IOException{ 
     Scanner scan = new Scanner(System.in); 
     BigDecimal d, v, t, a; 
     BigInteger b; 
     int index; 

     int n = scan.nextInt(); 

     double phi1 = (1 + Math.sqrt(5))/2; 
     double phi2 = (1 - Math.sqrt(5))/2;  
     double sqrt = Math.sqrt(5); 

     d = new BigDecimal(phi1); 
     v = new BigDecimal(phi2); 
     a = new BigDecimal(sqrt); 

     d = d.pow(n); 
     v = v.pow(n); 
     t = d.subtract(v); 
     t = t.divide(a); 

     b = t.toBigInteger(); 

     System.out.println(b.mod(1000000007)); //I get an error here. 
    } 
} 
+2

이 오류 메시지에 대한 불분명 무엇입니까? –

+3

오류 메시지를 읽었습니까? 'BigInteger'를 기대하는 메소드에'int'를 넘깁니다. –

+0

어떻게 해결할 수 있습니까? 나는 모든 것을 시도했습니다. – BorisMediaProds

답변

5

당신은 또 다른`의 BigInteger를 통과해야 :

System.out.println(answer.mod(BigInteger.valueOf(1000000007))); 
+0

아니면 문자열로 큰 정수를 인스턴스화 할 수 있습니다 :'b.mod (new BigInteger ("1000000007"))'. @Todd의 코드가 오른쪽에있는 숫자를 'Long'에 맞출 수있는 한 선호하지만 그렇지 않으면 문자열 버전이 필요합니다. –