Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
type nullable<T> = T | null | undefined;

export { divisors } from './divisors';
export { isPrime } from './prime';
export { isPalindrome } from './isPalindrome';
export { nullable };
12 changes: 12 additions & 0 deletions src/helpers/isPalindrome.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { isPalindrome } from './isPalindrome';

describe('number is Palindrome', () => {
it('some numbers are palindrome', () => {
expect.assertions(4);

expect(isPalindrome(1)).toBe(true);
expect(isPalindrome(131)).toBe(true);
expect(isPalindrome(1221)).toBe(true);
expect(isPalindrome(123454321)).toBe(true);
});
});
4 changes: 4 additions & 0 deletions src/helpers/isPalindrome.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const isPalindrome = (n: number): boolean =>
n.toString().split('').reverse().join('') === n.toString();

export default { isPalindrome };
32 changes: 32 additions & 0 deletions src/problem0004.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Largest palindrome product
*
* 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.
*/

import logger from './logger';

import { problem0004 } from './problem0004';

describe('problem 0004', () => {
it('problem 0004 solution found', () => {
expect.assertions(1);

const solutionFound = 906609;

const bottom = 111;
const top = 999;

const calculated = problem0004(bottom, top);

logger.info(`PROBLEM 0004 solution found: ${calculated}`);

expect(calculated).toBe(solutionFound);
});
});
69 changes: 69 additions & 0 deletions src/problem0004.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Largest palindrome product
*
* 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.
*/

/// ////////////////////////////////////////////////////////////////////////////
// NOTES ABOUT THE SOLUTION:
// This solution cycles to test all pairs of factors between 111 and 999 that meet the condition of generating a palindrome and saves the largest found.
// I think there must be another optimal solution to avoid testing all cases
// cutting the loop around the largest factor pair
// That's why I thought about doing the loop from highest to lowest.
/// ////////////////////////////////////////////////////////////////////////////

/// ////////////////////////////////////////////////////////////////////////////

import logger from './logger';

import { nullable, isPalindrome } from './helpers/index';

function problem0004(_bottom: number, _top: number): nullable<number> {
let i;
let j;
let foundi;
let foundj;
let foundPalindrome;

// Find all cases
let cycles = 0;

i = _top;
while (i >= _bottom) {
j = i;
while (j >= _bottom && (!foundj || j >= foundj)) {
cycles += 1;

if (isPalindrome(j * i)) {
logger.debug(`FOUND: ${i} x ${j} = ${j * i} is Palindrome`);

if (!foundPalindrome || i * j > foundPalindrome) {
foundi = i;
foundj = j;
foundPalindrome = i * j;
}
} else {
// console.log(`FOUND: ${i} x ${j} = ${j * i} is NOT Palindrome`);
}

j -= 1;
}

i -= 1;
}

logger.info(
`Problem 0004 Largest Palindrome => ${foundi} 𝗑 ${foundj} = ${foundPalindrome} in ${cycles} cycles`
);

return foundPalindrome;
}

export default problem0004;
export { problem0004 };