-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject-euler.html
More file actions
174 lines (144 loc) · 4.6 KB
/
project-euler.html
File metadata and controls
174 lines (144 loc) · 4.6 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Project Euler Solutions</title>
<meta name="description" content="TODO" />
<meta name="author" content="Chinh Do" />
<link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet" />
<script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
<link
href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap"
rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/p5@1.0.0/lib/p5.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
/* RESET */
/* Box sizing rules */
*,
*::before,
*::after {
box-sizing: border-box;
}
html,
body {
font-family: "Roboto", sans-serif;
margin: 0;
background-color: #222222;
color: white;
}
a {
color: white;
text-decoration: none;
}
li {
padding: 2px;
}
h1 {
font-size: 1.5rem;
}
.container {
padding: 20px;
}
.answer {
color: green
}
</style>
</head>
<body>
<div class="container">
<h1>Project Euler Problems & Chinh's Solutions</h1>
<h2><a href="https://projecteuler.net/problem=1">Multiples of 3 and 5</a></h2>
<p>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these
multiples is 23.</p>
<p>Find the sum of all the multiples of 3 or 5 below 1000.</p>
<p>Answer: <span class="answer" id="p1"></span></p>
<h2><a href="https://projecteuler.net/problem=2">Even Fibonacci numbers</a></h2>
<p>Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2,
the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...</p>
<p>By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the
even-valued terms.</p>
<p>Answer: <span class="answer" id="p2"></span></p>
<h2><a href="https://projecteuler.net/problem=3">Largest prime factor</a></h2>
<p>The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?</p>
<p>Answer: <span class="answer" id="p3"></span></p>
<h2><a href="https://projecteuler.net/problem=4">Largest palindrome product</a></h2>
<p>A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit
numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.</p>
<p>Answer: <span class="answer" id="p4"></span></p>
<p><a href="https://github.com/chinhdo/examples/blob/master/project-euler.html">GitHub</a></p>
</div>
<script>
$(function () {
p1();
p2();
p3LargestPrimeFactor();
p4LargestPalindrome();
});
function p4LargestPalindrome() {
let max = 0;
for (let i = 999; i >= 100; i--) {
for (let j = 999; j >= 100; j--) {
const p = i * j;
const str = p.toString();
const reversed = str.split('').reverse().join('');
if (str === reversed) {
if (p > max) {
max = p;
}
}
}
}
$("#p4").text(max);
}
// The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?
function p3LargestPrimeFactor() {
let x = 600851475143;
let y = 2;
let factor = 0;
while (true) {
if (x % y === 0 && isPrime(y)) {
factor = y;
}
y++;
if (y > Math.sqrt(x)) break;
}
$("#p3").text(factor);
}
function p1() {
let sum = 0;
for (let i = 0; i < 1000; i++) {
if (i % 3 === 0 || i % 5 === 0) {
sum += i;
}
}
$("#p1").text(sum);
}
function p2() {
let sum = 2;
let n1 = 1;
let n2 = 2;
let i = 0;
while (true) {
const n3 = n1 + n2;
if (n3 % 2 === 0) {
sum += n3;
}
n1 = n2;
n2 = n3;
if (n3 > 4000000) {
break;
}
}
$("#p2").text(sum);
}
const isPrime = num => {
for (let i = 2; i < num; i++)
if (num % i === 0) return false;
return num > 1;
}
</script>
</body>
</html>