Skip to content

Commit

Permalink
feat: ✨ largest palindrome from product of x numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
fauzulkc committed May 15, 2024
1 parent 351afa4 commit ae0dc1b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
24 changes: 24 additions & 0 deletions apps/euler-project/src/problems/palindrome-product.ts
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;
};
1 change: 0 additions & 1 deletion apps/euler-project/src/specs/largest-prime-factor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { checkLargestPrimeFactor } from '../problems/largest-prime-factor';

describe('largest prime factor', () => {
it('should return largest prime factor', () => {
console.log(checkLargestPrimeFactor(13195));
expect(checkLargestPrimeFactor(13195)).toEqual(29);
expect(checkLargestPrimeFactor(600851475143)).toEqual(6857);
});
Expand Down
21 changes: 21 additions & 0 deletions apps/euler-project/src/specs/palindrome-product.spec.ts
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);
});
});

0 comments on commit ae0dc1b

Please sign in to comment.