-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem4 - Largest Palindrome Product.html
92 lines (82 loc) · 2.42 KB
/
problem4 - Largest Palindrome Product.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
<html>
<head>
<title>Project Euler, Problem 4: Largest Palindrome Product</title>
<style>
#explanation {
visibility: hidden;
}
#problem {
text-align: center;
border: 2px solid black;
border-radius: 5px;
width: 85px;
cursor: default;
}
</style>
</head>
<body>
<p>
A palindromic number reads the same both ways. <br />
The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
<br /><br />
Find the sum of all the multiples of 3 or 5 below 1000.
</p>
<div id="problem" onclick="projectEulerProblem4()">
Palindrome Product
</div>
<div id="explanation">
</br>
<div id="totalTime"></div>
<p>
To solve this, I only multiplied the largest 3-digit numbers: 900-999.
</br></br>
Using nested for loops, I iterated through the 10,000 candidate number combinations finding palindrome numbers <br />
(via a helper function) and output the largest number.
</p>
</div>
</body>
<script>
//Finds the Palindrome Nubmers that are products of 3-digit integers.
function projectEulerProblem4() {
var startTime = new Date();
let maxPalindrome = 0;
let maxPalindromeI = 0;
let maxPalindromeJ = 0;
for (let i = 901; i < 999; i++) {
for (let j = 901; j < 999; j++) {
if (isPalindrome(i * j)) {
if ((i * j) > maxPalindrome) {
maxPalindrome = (i * j);
maxPalindromeI = i;
maxPalindromeJ = j;
}
}
}
}
var endTime = new Date();
var totalTime = endTime - startTime;
document.getElementById("problem").innerHTML = maxPalindromeI + " * " + maxPalindromeJ + " = " + maxPalindrome;
document.getElementById("totalTime").innerHTML = totalTime + " ms";
document.getElementById("problem").style.cursor = "auto";
document.getElementById("explanation").style.visibility = "visible";
}
//A function to reverse the number order of a number. Doesn't work past 16 digits
function reverseNumber(number) {
let reversedNumber = "";
while (number != 0) {
reversedNumber += number % 10;
number -= number % 10;
number /= 10;
}
return Number(reversedNumber);
}
//Function to check if a number is a Palindromic number, returns true or false
function isPalindrome(number) {
if (number === reverseNumber(number)) {
return true;
} else {
return false;
}
}
</script>
</html>