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

Add Problem4 (Largest Palindrome Product) #751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fmotaf wants to merge 6 commits into TheAlgorithms:master from fmotaf:master
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 22 additions & 38 deletions Project-Euler/Problem4.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,46 +1,30 @@
// https://projecteuler.net/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.
*/
const largestPalindromic = (digits) => {
let i
let n
let m
let d
let limit
let number = 0
function isPalindrome (number) {
Copy link
Member

@raklaptudirm raklaptudirm Oct 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments with a link to the problem.

// just 2 random names for variables!
const look = String(number)
let hear = false

for (i = 1; i < digits; i++) {
number = 10 * number + 9
}
const inf = number // highest (digits - 1) number, in this example highest 2 digit number
const sup = 10 * number + 9 // highest (digits) number, in this example highest 3 digit number

const isPalindromic = (n) => {
let p = 0
const q = n
let r
while (n > 0) {
r = n % 10
p = 10 * p + r
n = Math.floor(n / 10)
for (let i = 0; i < look.length; i++) {
if (look[i] === look[(look.length) - (i + 1)]) {
hear = true
} else {
return false
}
return p === q // returning whether the number is palindromic or not
}
return hear
}

function largestPalindromeProduct (n) {
let product = 0
let largestPalindrome = 11

for (n = sup * sup, m = inf * inf; n > m; n--) {
if (isPalindromic(n)) {
limit = Math.ceil(Math.sqrt(n))
d = sup
while (d >= limit) {
if (n % d === 0 && n / d > inf) {
return n
}
d -= 1
}
for (let i = 10 ** (n - 1); i < 10 ** n; i++) {
for (let j = 10 ** (n - 1); j < 10 ** n; j++) {
product = i * j
if (isPalindrome(product) && product > largestPalindrome) { largestPalindrome = product }
}
}
return NaN// returning not a number, if any such case arise
return largestPalindrome
}

console.log(largestPalindromic(3))
console.log(isPalindrome(9009))
Copy link
Member

@raklaptudirm raklaptudirm Oct 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the original function console logged, but it will be better if you remove the console logs and add jest tests instead.

console.log(largestPalindromeProduct(2))

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