-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path064.java
More file actions
137 lines (125 loc) · 3.57 KB
/
064.java
File metadata and controls
137 lines (125 loc) · 3.57 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package euler;
public class euler {
/*
* y
* ------------
* sqrt(n)-x
*
* y sqrt(n) + x
* ------------ x ------------
* sqrt(n) - x sqrt(n) + x
*
*
* y [sqrt(n) + x]
* ----------------- ..... = a
* n - x^2
*
*
* y [sqrt(n)+x]
* (int) a + -------------- - (int) a
* n - x^2
*
*
* y [sqrt(n)+x] - [(int)a](n - x^2)
* (int) a + ------------------------------------
* n - x^2
*
* 1
* -------------------------------------
* (int) a + n - x^2
* ------------------------------------
* y [sqrt(n)+x] - [(int)a](n - x^2)
*
* 1
* -------------------------------------
* (int) a + n - x^2
* --------------------------------------------
* y {[sqrt(n)+x] - (1/y)[(int)a](n - x^2)}
*
* 1
* -------------------------------------
* (int) a + [(n - x^2)/y]
* --------------------------------------------
* sqrt(n)+ x-(1/y)[(int)a](n - x^2)
*
* therefore...
* y1 = [(n - x^2)/y0]
* x1 = x0-(1/y)[(int)a](n - x^2)
*/
public static class FractionSet implements Comparable<FractionSet> {
int n;
int x;
int y;
int a;
public FractionSet () {}
public FractionSet (int a, int n, int x, int y) {
this.n=n;
this.x=x;
this.y=y;
this.a=a;
}
public FractionSet expand () {
FractionSet f=new FractionSet();
f.a= (int)((this.y*(Math.sqrt(n)+this.x)/(this.n-this.x*this.x)));
f.n=n;
f.y=(n-(this.x*this.x))/this.y;
f.x=-(this.x-f.a*(this.n-this.x*this.x)/this.y);
return f;
}
@Override
public int compareTo(FractionSet f) {
if (this.n==f.n && this.y==f.y && this.x==f.x && this.a==f.a) {
return 0;
}
return -1;
}
@Override
public String toString() {
return " "+y+" \n---------------\nsqrt("+this.n+")"+" - "+this.x;
}
}
public static FractionSet [] expansions;
public static int expansionCount;
public static void expandHelper (FractionSet f) {
boolean exists=false;
for (int i=0;i<expansionCount;i++) {
if (expansions[i].compareTo(f)==0) {
exists=true;
}
}
if (!exists) {
expansions[expansionCount++]=f;
expandHelper(f.expand());
}
}
public static void expand (int n) {
expansions=new FractionSet[300];
expansionCount=0;
expandHelper(new FractionSet((int)Math.sqrt(n),n,(int)Math.sqrt(n),1));
expansionCount--; //Ignore the first expansion.
}
public static void debugExpand (int n) {
expand(n);
System.out.print("Sqrt("+n+")=["+expansions[0].a+"(");
for (int i=1;i<expansionCount-1;i++) {
System.out.print(expansions[1].a+",");
}
System.out.println(expansions[expansionCount-1].a+")], period="+expansionCount);
}
public static void main (String [] zzzz) {
long before=System.currentTimeMillis();
int oddCount=0;
for (int n=2;n<=10000;n++) {
double sqrt=Math.sqrt(n);
if (sqrt!=(double)(int)sqrt) { //perform operation on non perfect squares.
expand(n);
//debugExpand(n);
if (expansionCount%2==1) {
oddCount++;
}
}
}
System.out.println(oddCount);
System.out.println("Took "+(System.currentTimeMillis()-before)+"ms.");
}
}