我的欧拉工程之路_12

Highly divisible triangular number

Problem 12

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?

三角数的因子数

问题 12

三角数由按顺序叠加自然数获得. 所以第7个三角数是 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. 前十个三角数是:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28

我们可以看到28是第一个拥有5个以上因子的三角数.

求第一个拥有超过500个以上因子的三角数?

public class Euler12
{
    public static int[] getPrimeArray(int length)
    {
        int[] prime =new int[length];
        prime[0]=2;
        for(int num=3,lengthTemp=1;lengthTemp!=length;num+=2)
        {
            int i;
            boolean primeFlag=true;
            for(i=3;i<=Math.sqrt(num);i+=2)
            {
                if(num%i==0)
                {
                    primeFlag=false;
                    break;
                }
            }
            if(primeFlag==true)
            {
                prime[lengthTemp]=num;
                lengthTemp++;
            }
        }
        /*
        for(int temp:prime)
        {
            System.out.print(temp+" ");
        }
        */
        return prime;
    }
    public static void main(String[] args)
    {
        int primeLength=10000,divisorMax=0;
        int[] prime = Euler12.getPrimeArray(primeLength);
        for(int i=2,divisor=1,num=1;divisor<=500;i++)
        {
            int[] primeCount=new int[primeLength];divisor=1;
            num+=i;
            for(int j=0,numTemp=num;prime[j]<=numTemp;j++)
            {
                //System.out.println("num="+num+" numTemp="+numTemp+" prime="+prime[j]);
                while(numTemp%prime[j]==0)
                {
                    primeCount[j]++;
                    numTemp/=prime[j];
                }
                //System.out.println(prime[j]+"Count="+primeCount[j]);
            }
            for(int j=0;j<primeLength;j++)
            {
                if(primeCount[j]!=0)
                    divisor*=primeCount[j]+1;
            }
            if(divisor>divisorMax)
            {
                divisorMax=divisor;
                System.out.println("num="+num+"\tdivisor="+divisor);
            }
        }
    }
}


你可能感兴趣的:(欧拉工程,数学问题)