Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7743307

Browse files
Add Largest Prime Palindrome to (L-A).
1 parent ab7436a commit 7743307

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function isPrime(num) {
2+
if (num <= 1) {
3+
return false;
4+
}
5+
if (num <= 3) {
6+
return true;
7+
}
8+
if (num % 2 === 0 || num % 3 === 0) {
9+
return false;
10+
}
11+
let i = 5;
12+
while (i * i <= num) {
13+
if (num % i === 0 || num % (i + 2) === 0) {
14+
return false;
15+
}
16+
i += 6;
17+
}
18+
return true;
19+
}
20+
21+
function isPalindrome(num) {
22+
const numStr = num.toString();
23+
const reversedStr = numStr.split("").reverse().join("");
24+
return numStr === reversedStr;
25+
}
26+
27+
function largestPrimePalindrome(n) {
28+
for (let i = n; i >= 2; i--) {
29+
if (isPrime(i) && isPalindrome(i)) {
30+
return i;
31+
}
32+
}
33+
return null; // No prime palindrome found in the range
34+
}
35+
36+
// Test cases
37+
console.log(largestPrimePalindrome(100)); // 7
38+
console.log(largestPrimePalindrome(50)); // 7
39+
console.log(largestPrimePalindrome(10)); // 7
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /