项目Euler#5 Java-关于使我的代码更干净的建议

时间:2015-02-10 04:43:07

标签: java

所以我经历过并重做我的代码。我之前尝试过蛮力方法,但它花了大约20分钟才得到正确的答案。我现在的代码几乎立即通过找到每个数字1-20的主要分解并在所有分解中获取最大数量的素数,对于每个20以下的素数来做到这一点。这一切都很好,但是我如果我想找到不同数字范围之间的最小倍数,例如1-30,或者甚至更热情,10-30?使用我现在拥有的代码,如果不改变我的大量代码就不可能。

package number_5;

public class Number5 {

    public static void main(String[] args) {
        long smallestnumber = 0;
        boolean exitloop = false;

        int finalTwo = 0;
        int finalThree = 0;
        int finalFive = 0;
        int finalSeven = 0;
        int finalEleven = 0;
        int finalThirteen = 0;
        int finalSeventeen = 0;
        int finalNineteen = 0;

        int two = 0;
        int three = 0;
        int five = 0;
        int seven = 0;
        int eleven = 0;
        int thirteen = 0;
        int seventeen = 0;
        int nineteen = 0;



        int temp = 0;


        for(int num = 1; num<=20; num++)
        {
            temp = num;
            for(int i=2; i<=temp; i++)
            {
                if(temp%i == 0)
                {
                    temp = temp/i;

                    switch ( i ){
                        case 2:
                            two++;
                            break;
                        case 3:
                            three++;
                            break;
                        case 5:
                            five++;
                            break;
                        case 7:
                            seven++;
                            break;
                        case 11:
                            eleven++;
                            break;
                        case 13:
                            thirteen++;
                            break;
                        case 17:
                            seventeen++;
                            break;
                        case 19:
                            nineteen++;
                            break;
                    }

                    i--;
                }


            }
            if(two>finalTwo)
                finalTwo = two;
            if(three>finalThree)
                finalThree = three;
            if(five>finalFive)
                finalFive = five;
            if(seven>finalSeven)
                finalSeven = seven;
            if(eleven>finalEleven)
                finalEleven = eleven;
            if(thirteen>finalThirteen)
                finalThirteen = thirteen;
            if(seventeen>finalSeventeen)
                finalSeventeen = seventeen;
            if(nineteen>finalNineteen)
                finalNineteen = nineteen;

            two = 0;
            three = 0;
            five = 0;
            seven = 0;
            eleven = 0;
            thirteen = 0;
            seventeen = 0;
            nineteen = 0;

        }


        int result = 1;

        if(finalTwo>0)
            result = (int) ((Math.pow(2, finalTwo)) * result);
        if(finalThree>0)
            result = (int) ((Math.pow(3, finalThree)) * result);
        if(finalFive>0)
            result = (int) ((Math.pow(5, finalFive)) * result);
        if(finalSeven>0)
            result = (int) ((Math.pow(7, finalSeven)) * result);
        if(finalEleven>0)
            result = (int) ((Math.pow(11, finalEleven)) * result);
        if(finalThirteen>0)
            result = (int) ((Math.pow(13, finalThirteen)) * result);
        if(finalSeventeen>0)
            result = (int) ((Math.pow(17, finalSeventeen)) * result);
        if(finalNineteen>0)
            result = (int) ((Math.pow(19, finalNineteen)) * result);


        System.out.print(result);

    }

}

1 个答案:

答案 0 :(得分:0)

尝试使用http://www.mathblog.dk/project-euler-problem-5/作为缩短代码并使其更具可扩展性的参考。

以下是该页面的摘要:

private int[] generatePrimes(int upperLimit)
{
    List<Integer> primes = new ArrayList<Integer>();
    boolean isPrime;
    int j;

    primes.add(2);

    for (int i = 3; i <= upperLimit; i += 2) 
    {
        j = 0;
        isPrime = true;
        while (primes.get(j) * primes.get(j) <= i) 
        {
            if (i % primes.get(j) == 0) 
            {
                isPrime = false;
                break;
            }
            j++;
        }
        if (isPrime) 
        {
            primes.add(i);
        }
    }

    return primes;
}

主要看起来像这样:

int divisorMax = 20;
List<Integer> p = generatePrimes(divisorMax);
int result = 1;

for (int i = 0; i < p.size(); i++) 
{
    int a = (int) Math.floor(Math.log(divisorMax) / Math.log(p.get(i)));
    result = result * ((int)Math.pow(p.get(i),a));
}

System.out.print(result);

这应该允许你通过将divisorMax更改为30来缩放1 - 30.我不确定10 - 30。