-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ largest palindrome from product of x numbers
- Loading branch information
Showing
3 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export const isPalindrome = (number: number): boolean => { | ||
const numberToString = number.toString(); | ||
return numberToString === numberToString.split('').reverse().join(''); | ||
}; | ||
|
||
export const largestPalindromeFromProductOfXNumbers = (x: number): number => { | ||
if (x < 1) { | ||
throw new Error('x must be greater than 0'); | ||
} | ||
const palindromeRange = Number(new Array(x).fill(9).join().replace(/,/g, '')); | ||
const isPalindromeLowestBound = Number( | ||
'1' + palindromeRange.toString().slice(1, -1) | ||
); | ||
let largestPalindrome = 0; | ||
for (let i = palindromeRange; i >= isPalindromeLowestBound; i--) { | ||
for (let j = i; j >= isPalindromeLowestBound; j--) { | ||
const product = i * j; | ||
if (isPalindrome(product) && product > largestPalindrome) { | ||
largestPalindrome = product; | ||
} | ||
} | ||
} | ||
return largestPalindrome; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { largestPalindromeFromProductOfXNumbers } from '../problems/palindrome-product'; | ||
|
||
describe('Largest palindrome from product of three numbers', () => { | ||
it('should return a Palindrome of 9009 when x = 2', () => { | ||
// Warning Slow Process beyond 3 numbers which is equivalent to inverse looping from 999 to 99 | ||
expect(largestPalindromeFromProductOfXNumbers(2)).toBe(9009); | ||
}); | ||
it('should return a Palindrome of 906609 when x = 3', () => { | ||
// Warning Slow Process beyond 3 numbers which is equivalent to inverse looping from 999 to 99 | ||
expect(largestPalindromeFromProductOfXNumbers(3)).toBe(906609); | ||
}); | ||
it.skip('should return a Palindrome of 99000099 when x = 4', () => { | ||
// Warning Slow Process beyond 3 numbers which is equivalent to inverse looping from 999 to 99 | ||
expect(largestPalindromeFromProductOfXNumbers(4)).toBe(99000099); | ||
}); | ||
}); |