-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem26.html
290 lines (270 loc) · 8.98 KB
/
Problem26.html
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<html>
<head>
<title>
Project Euler, Problem 26: Reciprocal Cycles
</title>
<link rel="stylesheet" type="text/css" href="eulerStyle.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<header>
<svg height="5rem" width="90vw">
<a href="https://dkallen78.github.io/Project-Euler-Files/projectEulerIndex.html">
<text id="euler" x="50%" y="50%" text-anchor="middle" dominant-baseline="middle">
Project Euler
</text>
</a>
</svg>
<br />
<svg height="4rem" width="90vw">
<a href="https://projecteuler.net/problem=6">
<text id="problemID" x="50%" y="50%" text-anchor="middle" dominant-baseline="middle">
Problem 26: Reciprocal Cycles
</text>
</a>
</svg>
</header>
<nav>
<svg height="3rem" width="75vw">
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem25.html">
<text x="0%" y="50%" text-anchor="start" dominant-baseline="middle">
Problem 25
</text>
</a>
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem27.html">
<text x="100%" y="50%" text-anchor="end" dominant-baseline="middle">
Problem 27
</text>
</a>
</svg>
</nav>
<main>
<p>
A unit fraction contains 1 in the numerator. The decimal representation of
the unit fractions with denominators 2 to 10 are given:
<br />
<div class="inset">
<math style="padding:1rem;">
<mfrac bevelled="true">
<mn> 1 </mn>
<mn> 2 </mn>
</mfrac>
<mo> = </mo>
<mn> 0.5 </mn>
</math> <br />
<math style="padding:1rem;">
<mfrac bevelled="true">
<mn> 1 </mn>
<mn> 3 </mn>
</mfrac>
<mo> = </mo>
<mn> 0. </mn>
<mover>
<mn> 3 </mn>
<mo> ‾ </mo>
</mover>
</math> <br />
<math style="padding:1rem;">
<mfrac bevelled="true">
<mn> 1 </mn>
<mn> 4 </mn>
</mfrac>
<mo> = </mo>
<mn> 0.25 </mn>
</math> <br />
<math style="padding:1rem;">
<mfrac bevelled="true">
<mn> 1 </mn>
<mn> 5 </mn>
</mfrac>
<mo> = </mo>
<mn> 0.2 </mn>
</math> <br />
<math style="padding:1rem;">
<mfrac bevelled="true">
<mn> 1 </mn>
<mn> 6 </mn>
</mfrac>
<mo> = </mo>
<mn> 0.1 </mn>
<mover>
<mn> 6 </mn>
<mo> ‾ </mo>
</mover>
</math> <br />
<math style="padding:1rem;">
<mfrac bevelled="true">
<mn> 1 </mn>
<mn> 7 </mn>
</mfrac>
<mo> = </mo>
<mn> 0. </mn>
<mover>
<mn> 142857 </mn>
<mo> ‾ </mo>
</mover>
</math> <br />
<math style="padding:1rem;">
<mfrac bevelled="true">
<mn> 1 </mn>
<mn> 8 </mn>
</mfrac>
<mo> = </mo>
<mn> 0.125 </mn>
</math> <br />
<math style="padding:1rem;">
<mfrac bevelled="true">
<mn> 1 </mn>
<mn> 9 </mn>
</mfrac>
<mo> = </mo>
<mn> 0. </mn>
<mover>
<mn> 1 </mn>
<mo> ‾ </mo>
</mover>
</math> <br />
<math style="padding:1rem;">
<mfrac bevelled="true">
<mn> 1 </mn>
<mn> 10 </mn>
</mfrac>
<mo> = </mo>
<mn> 0.1 </mn>
</math> <br />
</div>
<br />
Where
<math>
<mn> 0.1 </mn>
<mover>
<mn> 6 </mn>
<mo> ‾ </mo>
</mover>
</math> means
<math>
<mn> 0.166666 </mn>
<mi> … </mi>
</math>, and has a 1-digit recurring cycle. It can be seen that
<math>
<mfrac bevelled="true">
<mn> 1 </mn>
<mn> 7 </mn>
</mfrac>
</math> has a 6-digit recurring cycle.
<br /><br />
Find the value of
<math>
<mi> d </mi>
<mo> < </mo>
<mn> 1000 </mn>
</math> for which
<math>
<mfrac bevelled="true">
<mn> 1 </mn>
<mi> d </mi>
</mfrac>
</math> contains the longest recurring cycle in its decimal fraction part.
</p>
<button id="problem"onclick="projectEulerProblem26()">
Find Repetend
</button>
<br />
<summary id="notes">
<div id="totalTime"></div>
<p>
For this problem I taught the computer how to do long division.
<br /><br />
As I converted the fraction into decimal form with long division, I saved
the pairs of quotients and remainders in an array. Then, after each operation
I checked the pairs to see if there was a match. If so, I knew the sequence
had repeated and based on the index of the pairs I could determine the
repetend length. I also only checked 900 - 1000 since, based on research,
I had a good idea the final answer would be a large prime.
<br /><br />
74,423
</p>
</summary>
</main>
</body>
<script>
function projectEulerProblem26() {
let startTime = new Date();
let maxLength = 0
let denominator = 0;
for (let i = 900; i < 1001; i++) {
let length = crunchRepetend(i);
if (length > maxLength) {
maxLength = length;
denominator = i
}
}
/*var divisor = "14";
let length = crunchRepetend(16);*/
console.log(maxLength);
console.log(denominator);
let endTime = new Date();
let totalTime = endTime - startTime;
document.getElementById("totalTime").innerHTML = totalTime + " ms";
document.getElementById("notes").style.display = "block";
document.getElementById("problem").innerHTML = denominator;
}
function crunchRepetend(divisor) {
let pairArray = [];
let dividend = 10;
let counter = 0;
let remainder = 0;
let abandonShip = false;
do {
//console.log(counter);
if (divisor > dividend) {
pairArray[counter] = [dividend, 0];
//console.log(divisor + " is too large to divide into " + dividend);
//console.log(pairArray);
dividend *= 10;
counter++;
} else {
remainder = Math.floor(dividend / divisor);
//console.log(divisor + " goes into " + dividend + " " + remainder + " times: " + (divisor * remainder));
pairArray[counter] = [dividend, (remainder * divisor)];
//console.log(pairArray);
dividend = (dividend - (remainder * divisor)) * 10;
counter++;
if (dividend === 0) {
//console.log("Dividend = 0, we're done here, no repetend");
abandonShip = true;
return 0;
} else if (comparePairs(pairArray) > 0) {
abandonShip = true;
//console.log(pairArray);
return comparePairs(pairArray);
}
}
} while (!abandonShip);
}
function comparePairs(pairArray) {
let abandonShip = false;
let counter = 0;
while (!abandonShip) {
//console.log("Comparing pairs at index " + counter + " and index " + (pairArray.length - 1));
//console.log(pairArray[counter][0] + " = " + pairArray[pairArray.length - 1][0]);
//console.log(pairArray[counter][1] + " = " + pairArray[pairArray.length - 1][1])
if (pairArray[counter][0] === pairArray[pairArray.length - 1][0] && pairArray[counter][1] === pairArray[pairArray.length - 1][1]) {
//console.log("We've got a match! We're heading home!");
abandonShip = true;
//console.log((pairArray.length - 1) - counter);
return ((pairArray.length - 1) - counter);
} else {
//console.log("No match, checking the next index");
counter++;
}
if (counter === pairArray.length - 1) {
//console.log("Out of elements to test, heading home");
abandonShip = true;
return 0;
}
}
}
</script>
</html>