2012-09-26 71 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