Euler 004

Javascript code posted
created at 30 Jun 15:45

Edit | Back
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
// A palindromic number reads the same both ways. The largest palindrome made
// from the product of two 2-digit numbers is 9009 = 91 x 99.

// Find the largest palindrome made from the product of two 3-digit numbers.

(function euler004() {
  // start with the largest number from two 3-digit numbers
  var n = 999 * 999;
  var nStr;
  var nStrReversed;
  var sqrt;
  var divisor;

  while (n > 200000) {
    // check if n is a palindrome
    // convert to string
    nStr = n + '';
    // reverse string
    nStrReversed = nStr.split('').reverse().join('');

    // if they are identical, then n is a palindrome
    if (nStr === nStrReversed) {
      // find the sqrt
      // one of the factors has to be less than the sqrt
      // so this reduces the number of iterations needed
      sqrt = Math.sqrt(n);
      // make it an integer
      divisor = Math.floor(sqrt);

      // while n / divisor leaves a remainder
      // and the divisor greater than 3-digits
      // and the other factor less than 4-digits
      while (n % divisor !== 0 && divisor >= 100 && n / divisor <= 999) {
        // reduce divisor by 1
        divisor--;
      }

      // if n / divisor leaves no remainder
      // and the divisor is greater than 3-digits
      // and thh other factor is less than 4-digits
      if (n % divisor === 0 && divisor >= 100 && n / divisor <= 999) {
        // return the answer
          
        console.log(n);
        return n;
      }
    }

    n--;
  }
}());
1.5 KB in 3 ms with coderay