Project Euler #3 Java

∥☆過路亽.° 提交于 2019-12-11 13:06:13

问题


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

My program works for smaller numbers but it Throws an Arthmetic Exception for Divide by 0 when its not dividing by zero.please dont give me the answer,just wanna understand it and improve my skills thank you


回答1:


My guess would be that s is overflowing, leading eventually to a divide by zero. Make s a long instead.



来源:https://stackoverflow.com/questions/12611149/project-euler-3-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!