-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem4.js
57 lines (48 loc) · 1.41 KB
/
problem4.js
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
// 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.
function isPalindrome(number) {
let strNumber = number + ''
let first, last, difference
while (true) {
first = parseInt(strNumber[0])
last = parseInt(strNumber[strNumber.length - 1])
difference = first - last
if (strNumber.length <= 1) {
return true
}
else if (difference === 0) {
strNumber = strNumber.slice(1, strNumber.length - 1) + ''
}
else if (difference !== 0) {
return false
}
}
}
function findPalindrome() {
let upperLimit = 999 * 999, lowerLimit = 100 * 100
let unknownFactor, sqrt;
while (lowerLimit < upperLimit) {
if (isPalindrome(upperLimit)) {
sqrt = Math.ceil(Math.sqrt(upperLimit))
for (let i = sqrt; i >= 100; i--) {
if (upperLimit % i === 0) {
unknownFactor = upperLimit / i
if (`${unknownFactor}`.length === 3) {
console.log(`${i} * ${unknownFactor} = ${upperLimit}`)
return
}
else {
continue
}
}
}
}
upperLimit -= 1
}
}
const startTime = Date.now()
findPalindrome()
const endTime = Date.now()
const runningTime = endTime - startTime
console.log(`Running time (s): ${runningTime / 1000}`)