PROJECT EULER P4 cannot get the answer

David Jiang

Problem 4

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.

here is my code below. I try my code on the product of two 2-digit numbers, it prints 9009 (correct answer). but for the 3 digit numbers, the correct answer is 906609, my answer is 956459. I don't know what 's wrong with my code, please help. Thanks.

let num;//product of two numbers
let paNum=[];//array for palindrome number
for(i=100;i<1000;i++)
{
    for (j=100;j<1000;j++)
    {
        num = i*j;
        str= num.toString();//change the number to a string
        if (str.substr(0,1) == str.substr(-1,1) && str.substr(1,1) == str.substr(-2,1))
        {
            paNum.push(num);
        }
        else{};
    };

};
console.log(paNum);
console.log(Math.max(...paNum));
menaka_

I got the correct answer with the following modification.

for(i=100;i<1000;i++) {
    for (j=100;j<1000;j++){
        num = i*j;
        str= num.toString();//change the number to a string
        let len = str.length - 1;
        let checkedDigit = [];

        //Iterate the string from 0 to string length/2 and check whether it is a palindrom.
        for (k = 0; k < str.length/2; k++) {
            if (str.substr(k, 1) === str.substr(len - (len + 1 + k), 1)) {
                //If the two corresponding digits are equal, add a true. (Else false)
                checkedDigit[k] = true;
            } else {
                checkedDigit[k] = false;
            }
        }

  function checkDigit(val) {
    return val === true;
  }

  //Check whether all the elements in the checkedDigit array are true.
  if (checkedDigit.every(checkDigit)) {
    paNum.push(num);
  }

};
};
console.log(paNum);
console.log(Math.max(...paNum));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Project Euler #8, can't seem to get the correct answer

From Dev

Project Euler #23 Incorrect Answer

From Dev

Project Euler 17 incorrect answer

From Dev

Project Euler #17 wrong answer

From Dev

Where is my mistake in this answer to Project Euler #58?

From Dev

Project euler #10 getting wrong answer

From Dev

Project Euler #8 answer fails to be true

From Dev

Optimising F# answer for Euler #4

From Java

Project Euler problem 10, wrong answer but why (Java)

From Dev

Project Euler's Fibonacci - Why does this answer work?

From Dev

I have wrong answer for project Euler#8

From Dev

I have wrong answer for project Euler#8

From Dev

Project Euler 8: Getting Incorrect Answer From Code

From Dev

Ruby Project Euler 26, Unable to think of an answer to the problem

From Dev

Haskell-Project Euler 4

From Dev

Haskell-Project Euler 4

From Dev

Issues solving Project Euler #4

From Dev

Range out of bounds, Project Euler 4

From Dev

Possible optimizations for Project Euler #4 algorithm

From Dev

Project Euler - Largest Palindrome #4 Python

From Dev

Why isn't my javascript returning the correct answer to Project Euler's Number One?

From Dev

Project Euler #23 (Java). I can't figure out what's wrong. Answer is off by 64

From Dev

Project euler #8 in C++ getting wrong answer that is somewhat maximum uint_64 value

From Dev

Why isn't my Bash script returning the correct answer to this Project Euler?

From Dev

Project Euler 23 - No matter what I do, my answer is far too big

From Dev

Project Euler Problem 17, Python, I Am getting wrong Answer, No Idea why

From Dev

When I try to delete a workspace using p4 client -d, I get the message "client is locked, cannot be deleted"

From Dev

Get P4 vars in Java code

From Dev

Project Euler 549 - My functions are not returning the answer it's supposed to return and I don't know what's wrong

Related Related

  1. 1

    Project Euler #8, can't seem to get the correct answer

  2. 2

    Project Euler #23 Incorrect Answer

  3. 3

    Project Euler 17 incorrect answer

  4. 4

    Project Euler #17 wrong answer

  5. 5

    Where is my mistake in this answer to Project Euler #58?

  6. 6

    Project euler #10 getting wrong answer

  7. 7

    Project Euler #8 answer fails to be true

  8. 8

    Optimising F# answer for Euler #4

  9. 9

    Project Euler problem 10, wrong answer but why (Java)

  10. 10

    Project Euler's Fibonacci - Why does this answer work?

  11. 11

    I have wrong answer for project Euler#8

  12. 12

    I have wrong answer for project Euler#8

  13. 13

    Project Euler 8: Getting Incorrect Answer From Code

  14. 14

    Ruby Project Euler 26, Unable to think of an answer to the problem

  15. 15

    Haskell-Project Euler 4

  16. 16

    Haskell-Project Euler 4

  17. 17

    Issues solving Project Euler #4

  18. 18

    Range out of bounds, Project Euler 4

  19. 19

    Possible optimizations for Project Euler #4 algorithm

  20. 20

    Project Euler - Largest Palindrome #4 Python

  21. 21

    Why isn't my javascript returning the correct answer to Project Euler's Number One?

  22. 22

    Project Euler #23 (Java). I can't figure out what's wrong. Answer is off by 64

  23. 23

    Project euler #8 in C++ getting wrong answer that is somewhat maximum uint_64 value

  24. 24

    Why isn't my Bash script returning the correct answer to this Project Euler?

  25. 25

    Project Euler 23 - No matter what I do, my answer is far too big

  26. 26

    Project Euler Problem 17, Python, I Am getting wrong Answer, No Idea why

  27. 27

    When I try to delete a workspace using p4 client -d, I get the message "client is locked, cannot be deleted"

  28. 28

    Get P4 vars in Java code

  29. 29

    Project Euler 549 - My functions are not returning the answer it's supposed to return and I don't know what's wrong

HotTag

Archive