|
| 1 | +# 0015 Largest Prime Palindrome ( L-A ) |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +Write a JavaScript function called largestPrimePalindrome that takes an integer **n** as input and returns the largest prime palindrome that is less than or equal to **n**. A prime palindrome is a number that is both prime and reads the same forward and backward. |
| 6 | + |
| 7 | +**Example** |
| 8 | + |
| 9 | +``` |
| 10 | +If the input n is 100, the function should return 7 because the largest prime palindrome less than or equal to 100 is 7. |
| 11 | + |
| 12 | +If the input n is 50, the function should return 7 because 7 is the largest prime palindrome less than or equal to 50. |
| 13 | + |
| 14 | +If the input n is 10, the function should return 7 because 7 is the largest prime palindrome less than or equal to 10. |
| 15 | +``` |
| 16 | + |
| 17 | +## Solutions |
| 18 | + |
| 19 | +```javascript |
| 20 | +function isPrime(num) { |
| 21 | + if (num <= 1) { |
| 22 | + return false; |
| 23 | + } |
| 24 | + if (num <= 3) { |
| 25 | + return true; |
| 26 | + } |
| 27 | + if (num % 2 === 0 || num % 3 === 0) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + let i = 5; |
| 31 | + while (i * i <= num) { |
| 32 | + if (num % i === 0 || num % (i + 2) === 0) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + i += 6; |
| 36 | + } |
| 37 | + return true; |
| 38 | +} |
| 39 | + |
| 40 | +function isPalindrome(num) { |
| 41 | + const numStr = num.toString(); |
| 42 | + const reversedStr = numStr.split("").reverse().join(""); |
| 43 | + return numStr === reversedStr; |
| 44 | +} |
| 45 | + |
| 46 | +function largestPrimePalindrome(n) { |
| 47 | + for (let i = n; i >= 2; i--) { |
| 48 | + if (isPrime(i) && isPalindrome(i)) { |
| 49 | + return i; |
| 50 | + } |
| 51 | + } |
| 52 | + return null; // No prime palindrome found in the range |
| 53 | +} |
| 54 | + |
| 55 | +// Test cases |
| 56 | +console.log(largestPrimePalindrome(100)); // 7 |
| 57 | +console.log(largestPrimePalindrome(50)); // 7 |
| 58 | +console.log(largestPrimePalindrome(10)); // 7 |
| 59 | +``` |
| 60 | + |
| 61 | +## How it works |
| 62 | + |
| 63 | +1. The isPrime function checks whether a given number is prime using a primality test that efficiently eliminates multiples of 2 and 3 and then checks for divisibility by numbers of the form 6k ± 1. |
| 64 | + |
| 65 | +2. The isPalindrome function checks whether a given number is a palindrome by converting it to a string, reversing the string, and comparing it to the original string. |
| 66 | + |
| 67 | +3. The largestPrimePalindrome function starts from the given number n and counts down to 2, checking each number in reverse order. It uses the isPrime and isPalindrome functions to find the largest prime palindrome in the given range. |
| 68 | + |
| 69 | +## References |
| 70 | + |
| 71 | +- [ChatGPT](https://chat.openai.com/) |
| 72 | + |
| 73 | +## Problem Added By |
| 74 | + |
| 75 | +- [Tipchan](https://github.com/tsongtheng) |
| 76 | + |
| 77 | +## Contributing |
| 78 | + |
| 79 | +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. |
| 80 | + |
| 81 | +Please make sure to update tests as appropriate. |
0 commit comments