Skip to content

Commit

Permalink
Problem 0004: solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
sir-gon committed May 18, 2023
1 parent aa24660 commit c9eeb8f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
type nullable<T> = T | null | undefined;

export { divisors } from './divisors';
export { isPrime } from './prime';
export { isPalindrome } from './isPalindrome';
export { nullable };
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 };

0 comments on commit c9eeb8f

Please sign in to comment.