Home>

### java - "project euler""problem 12"

Questions about"problem 12"on the website"project euler" ;.
The problem is

The sequence of triangular numbers is represented by the sum of natural numbers, and the seventh triangular number is 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first 10 terms of the triangular number are:

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

For the first seven terms, the divisors are listed as follows:

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

The seventh triangle number 28 is now the first triangle number with more than 5 divisors.

In

, there are some initial triangular numbers with more than 500 divisors.

and I wrote the following source code.
However, it is struggling because the result value does not come out even if it executes many times.

Applicable source code

`` `Java

package eulerpro12;

public class Main {

public static boolean sosuHantei (long number) {

boolean hantei = true;         for (int i = 2;i<= (int) Math.sqrt ((double) number);i ++) {             if (number% i == 0) {

hantei = false;

break;

}

}         return hantei;
}>A method that assigns a number and returns true if it is a prime number, false if it is not a prime number

public static void main (String [] args) {

long triangleNum = 1;// Triangle number

int kosu = 1;// Number of divisors

int i = 1;

// Solve using the divisor number formula.

do {

i ++;

triangleNum + = i;

for (int j = 2;j<= triangleNum;j ++) {

if (sosuHantei (j) == false) {

continue;

}

int count = 0;// How many times is divisible by prime j?

while (triangleNum% j == 0) {

count ++;

}

kosu * = (count + 1);

}

} while (kosu<= 500);

System.out.println (triangleNum);

}

} `` `

Tried

Divisor formula
"When a natural number N can be expressed as N = (a_1) ^ p_1 × ,, (a_n) ^ p_n using prime numbers a_1, a_2 ,,,, a_n, the number of divisors is (p_1 + 1) × ,, , X (p_n + 1) "

I decided to use

to find the value.

I'd be grateful if you could give me some hints about what's wrong with the above source code.

``````while (triangleNum% j == 0) {
count ++;
}``````

In this loop,`triangleNum`does not change at all, so it is either "does not enter the loop" or "becomes an infinite loop".

About the method`sosuHantei`

``````public static boolean sosuHantei (long number)
{
boolean hantei = true;
for (int i = 2;i<= (int) Math.sqrt ((double) number);i ++) {
if (number% i == 0) {
hantei = false;
break;
}
}
return hantei;
}``````

However, if it is less than`1`, it will be`true`. Judgment is also required if`number`is 1 or less.
Also, where I came to know
1.`int`is enough for argument type.
2.`Math.sqrt ((double) number)`->`Math.sqrt (number)`Is good.
3. You can write`return true;``return false;`, so`boolean hantei;`is unnecessary.
If you write the code in consideration of the following, you can write like this.

``````public static boolean sosuHantei (int n)
{
if (n<= 1) {
return false;
}
for (int i = 2;i<= (int) Math.sqrt (n);i ++) {
if (n% i == 0) {
return false;
}
}
return true;
}``````