2012-09-26 66 views
1
public class Problem3 { 

    public static void main (String args[]) { 
     System.out.print(primeMod(60085147514L)); 
    } 

    public static double primeMod(long d) { 
     long max = 0; 
     int count = 0; 

     for (long i = 2; i < d; i++) { 
      if (d % i == 0) { 
       boolean isPrime = primeCounter(i); 
       if(isPrime == true) { 
        max = i; 
        System.out.println(max); 
       } 
      } else { 
       max = max; 
      } 
     } 

     return max; 
    } 

    public static boolean primeCounter(long x) { 
     int count = 0; 
     for (int s = 1; s <= x; s++) { 
      if (x % s == 0) { 
       count++; 
      } 
     } 

     if (count == 2) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
} 

我的程序適用於更小的數字,但它由0拋出的鴻溝的Arthmetic異常時,它不是除以zero.please不給我答案,只是想了解它,並提高我的技能 謝謝項目歐拉#3的Java

+8

當您發生異常時,請張貼堆棧跟蹤的相關部分,並至少發佈發生異常的行。 – assylias

+1

我試過你的程序,我沒有得到任何例外。 – PermGenError

+0

你怎麼知道它沒有被零除?請張貼一些輸出。 –

回答

4

我的猜測是s溢出,最終導致除以零。改爲使s代替long

+0

這看起來像是這個問題。我在'if(x%s == 0){'這意味着's'爲零的行上得到異常。 –

+0

'for(int s = 1; s <= x; s ++)''x'是一個'long','s'確實溢出了,因爲所討論的數字的除數超過了'int'範圍。 –

+0

@ Code-Guru當它初始化爲1時,它將如何變爲零?並在s--上。 – PermGenError