-
Notifications
You must be signed in to change notification settings - Fork 0
/
p39.java
57 lines (45 loc) · 1.25 KB
/
p39.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.*;
public class p39 {
private static int[] mem = new int[1000];
static{
Arrays.fill(mem,-1);
}
public static void main(String[] args){
int max = 0;
int pval = 0;
for (int i =1; i< 1000; i++){
int temp = total(i);
if (max < temp){max = temp; pval = i;}
}
System.out.println(pval);
}
private static int primitive(int p){
if (mem[p] != -1){return mem[p];}
int count = 0;
if(p %2 != 0){
mem[p] = count;
return count;
}
for (int i = 1; i <= Math.sqrt(p)/2; i++){
double d = (Math.sqrt(i*i + 2*p) - i)/2;
if (d > 0 && d % 1 == 0){
int m = (int) d;
if (m <= i) {continue;}
if (m % 2 == i % 2){continue;}
if (gcd(m,i) != 1){continue;}
count++;
}
}
mem[p] = count;
return count;
}
private static int total(int p){
int total = 0;
for (int i = 1; i <= p/2; i++){
if (p % i == 0) total += primitive(i);
}
return total;
}
//a > b
private static int gcd(int a, int b){return b == 0 ? a : gcd(b, a%b);}
}