-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem25.html
320 lines (295 loc) · 8.43 KB
/
Problem25.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<html>
<head>
<title>
Project Euler, Problem 25: 1,000-Digit Fibonacci Number
</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 25: 1,000-digit Fibonacci Number
</text>
</a>
</svg>
</header>
<nav>
<svg height="3rem" width="75vw">
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem24.html">
<text x="0%" y="50%" text-anchor="start" dominant-baseline="middle">
Problem 24
</text>
</a>
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem26.html">
<text x="100%" y="50%" text-anchor="end" dominant-baseline="middle">
Problem 26
</text>
</a>
</svg>
</nav>
<main>
<p>
The Fibonacci sequence is defined by the recurrence relation:<br />
<div class="inset">
<math>
<msub>
<mi>F</mi>
<mi>n</mi>
</msub>
<mo>=</mo>
<msub>
<mi>F</mi>
<mrow>
<mi>n</mi>
<mo>-</mo>
<mn>1</mn>
</mrow>
</msub>
<mo>+</mo>
<msub>
<mi>F</mi>
<mrow>
<mi>n</mi>
<mo>-</mo>
<mn>2</mn>
</mrow>
</msub>
</math>, where
<math>
<msub>
<mi> F </mi>
<mn> 1 </mn>
</msub>
<mo> = </mo>
<mn> 1 </mn>
</math> and
<math>
<msub>
<mi> F </mi>
<mn> 2 </mn>
</msub>
<mo> = </mo>
<mn> 1 </mn>
</math>.
</div>
<br />
Hence the first 12 terms will be:<br /><br />
<div class="inset">
<math>
<msub>
<mi> F </mi>
<mn> 1 </mn>
</msub>
<mo> = </mo>
<mn> 1 </mn>
</math> <br />
<math>
<msub>
<mi> F </mi>
<mn> 2 </mn>
</msub>
<mo> = </mo>
<mn> 1 </mn>
</math> <br />
<math>
<msub>
<mi> F </mi>
<mn> 3 </mn>
</msub>
<mo> = </mo>
<mn> 2 </mn>
</math> <br />
<math>
<msub>
<mi> F </mi>
<mn> 4 </mn>
</msub>
<mo> = </mo>
<mn> 3 </mn>
</math> <br />
<math>
<msub>
<mi> F </mi>
<mn> 5 </mn>
</msub>
<mo> = </mo>
<mn> 5 </mn>
</math> <br />
<math>
<msub>
<mi> F </mi>
<mn> 6 </mn>
</msub>
<mo> = </mo>
<mn> 8 </mn>
</math> <br />
<math>
<msub>
<mi> F </mi>
<mn> 7 </mn>
</msub>
<mo> = </mo>
<mn> 13 </mn>
</math> <br />
<math>
<msub>
<mi> F </mi>
<mn> 8 </mn>
</msub>
<mo> = </mo>
<mn> 21 </mn>
</math> <br />
<math>
<msub>
<mi> F </mi>
<mn> 9 </mn>
</msub>
<mo> = </mo>
<mn> 34 </mn>
</math> <br />
<math>
<msub>
<mi> F </mi>
<mn> 10 </mn>
</msub>
<mo> = </mo>
<mn> 55 </mn>
</math> <br />
<math>
<msub>
<mi> F </mi>
<mn> 11 </mn>
</msub>
<mo> = </mo>
<mn> 89 </mn>
</math> <br />
<math>
<msub>
<mi> F </mi>
<mn> 12 </mn>
</msub>
<mo> = </mo>
<mn> 144 </mn>
</math> <br />
</div>
<br />
The 12th term,
<math>
<msub>
<mi> F </mi>
<mn> 12 </mn>
</msub>
</math>, is the first term to contain three digits.
<br /><br />
What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
</p>
<button id="problem"onclick="projectEulerProblem25()">
Action
</button>
<br />
<summary id="notes">
<div id="totalTime"></div>
<p>
Because numbers with so many digits are hard to represent in Javascript,
I used arrays of numbers instead.
<br /><br />
I made a function to add arrays of numbers together from left to right then
I kept count of how many Fibonacci numbers I had cycled through. I also used
the formula
<math>
<mn> 0.209 </mn>
<mo> × </mo>
<mi> n </mi>
<mo> ≈ </mo>
<mi> digits </mi>
</math> where
<math>
<mi> n </mi>
</math> is the
<math>
<msup>
<mi> n </mi>
<mi> th </mi>
</msup>
</math> Fibonacci number to act as a checksum and give me a rough idea
of what index I should be at.
<br /><br />
138,843
</p>
</summary>
</main>
</body>
<script>
function projectEulerProblem25() {
var startTime = new Date();
let fibonacciArray = [1];
let fibonacciArrayAddend = [1];
let fibonacciIndex = 2;
while (fibonacciArray.length < 1000) {
let holdingArray = fibonacciArray;
fibonacciArray = addBigNumbers(fibonacciArray, fibonacciArrayAddend);
fibonacciArrayAddend = holdingArray;
fibonacciIndex++;
}
var endTime = new Date();
var totalTime = endTime - startTime;
document.getElementById("totalTime").innerHTML = totalTime + " ms";
document.getElementById("notes").style.display = "block";
document.getElementById("problem").innerHTML = fibonacciIndex;
console.log(fibonacciArray);
fibonacciArray = reverseArray(fibonacciArray);
console.log(fibonacciArray);
}
function addBigNumbers(array1, array2) {
let tempArray = [];
var carry = false;
if (array1.length > array2.length) {
array2.push(0);
}
for (let i = 0; i < array1.length; i++) {
if (carry) {
array2[i]++;
carry = false;
}
if (array1[i] + array2[i] > 9) {
if (i === array1.length - 1) {
tempArray[i + 1] = 1;
tempArray[i] = (array1[i] + array2[i]) % 10;
} else {
tempArray[i] = (array1[i] + array2[i]) % 10;
carry = true;
}
} else {
tempArray[i] = array1[i] + array2[i];
}
}
return tempArray;
}
function reverseArray(array) {
let difference = (array.length - 1)
let cycles = Math.ceil(difference / 2);
for (let i = 0; i < cycles; i++) {
array = swapElements(i, (array.length -1) - i, array);
}
return array;
}
function swapElements(index1, index2, array) {
let holderVariable = array[index1];
array[index1] = array[index2];
array[index2] = holderVariable;
return array;
}
</script>
</html>