-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem14.1.html
192 lines (173 loc) · 5.76 KB
/
Problem14.1.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
<html>
<head>
<title>
Project Euler, Problem 14: Longest Collatz Sequence
</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=14">
<text id="problemID" x="50%" y="50%" text-anchor="middle" dominant-baseline="middle">
Problem 14: Longest Collatz Sequence (Stored Values)
</text>
</a>
</svg>
</header>
<nav>
<svg height="3rem" width="75vw">
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem14.html">
<text x="0%" y="50%" text-anchor="start" dominant-baseline="middle">
Problem 14 (Brute Force)
</text>
</a>
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem15.html">
<text x="100%" y="50%" text-anchor="end" dominant-baseline="middle">
Problem 15
</text>
</a>
</svg>
</nav>
<main>
<p>
The following iterative sequence is defined for the set of positive integers:
<br />
<div class="inset">
<math>
<mi>n</mi>
<mo> → </mo>
<mfrac>
<mrow>
<mi>n</mi>
</mrow>
<mn>2</mn>
</mfrac>
<mtext>
(n is even)
</mtext>
</math>
<br />
<math>
<mi>n</mi>
<mo> → </mo>
<mn>3</mn>
<mo>⁢</mo>
<mi>n</mi>
<mo>+</mo>
<mn>1</mn>
<mtext>
(n is odd)
</mtext>
</math>
<br />
</div>
<br />
Using the rule above and starting with 13, we generate the following sequence:
<br /><br />
<div class="example">
<math>
<mn>13</mn>
<mo> → </mo>
<mn>40</mn>
<mo> → </mo>
<mn>20</mn>
<mo> → </mo>
<mn>10</mn>
<mo> → </mo>
<mn>5</mn>
<mo> → </mo>
<mn>16</mn>
<mo> → </mo>
<mn>8</mn>
<mo> → </mo>
<mn>4</mn>
<mo> → </mo>
<mn>2</mn>
<mo> → </mo>
<mn>1</mn>
</math>
<br />
</div>
<br />
It can be seen that this sequence (starting at 13 and finishing at 1) contains
10 terms. Although it has not been proved yet (Collatz Problem), it is thought
that all starting numbers finish at 1.
<br /><br />
Which starting number, under one million, produces the longest chain?
<br /><br />
<span class="bold">NOTE:</span> Once the chain starts the terms are allowed
to go above one million.
</p>
<button id="problem" onclick="projectEulerProblem14()">
Find Number
</button>
</br>
<summary id="notes">
<div id="totalTime"></div>
<p>
I store the lengths of the Collatz sequences in an array so I don't have
to calculate the entire sequence for every number. This reduces the run
time over my previous attempt by a factor of 20.
<br /><br />
I use a while loop to run through the sequence until I reach one or a number
previously in the sequence and I store the longest sequence and the number
that produced it as I go along.
<br /><br />
###,###
</p>
</summary>
</main>
</body>
<script>
function projectEulerProblem14() {
let startTime = new Date();
let collatzCounter = [];
let longestChain = 0;
let longestSeed = 0
let collatz = 0;
let limit = 1000000;
for (let x = 1; x < limit; x++) {
collatz = getCollatz(x);
if (collatz > longestChain) {
longestChain = collatz;
longestSeed = x;
}
}
let endTime = new Date();
let totalTime = endTime - startTime;
document.getElementById("totalTime").innerHTML = totalTime + " ms";
document.getElementById("notes").style.display = "block";
document.getElementById("problem").innerHTML = longestSeed;
console.log("Number with longest chain: " + longestSeed);
console.log("Chain length: " + longestChain);
function getCollatz(start) {
var stepCounter = 1;
var originalStart = start;
while (start !== 1) {
if (collatzCounter[start] > 0) {
collatzCounter[originalStart] = (collatzCounter[start] - 1) + stepCounter;
return (collatzCounter[originalStart]);
} else if ((start % 2) === 0) {
start /= 2;
} else {
start = (start * 3) + 1;
}
stepCounter++;
}
collatzCounter[originalStart] = stepCounter;
return stepCounter;
}
}
</script>
</html>