Skip to content

Commit 8eae2f6

Browse files
authored
Merge pull request #17 from sir-gon/feature/problem0004
Feature/problem0004
2 parents ed4af32 + c9eeb8f commit 8eae2f6

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

src/helpers/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
type nullable<T> = T | null | undefined;
2+
13
export { divisors } from './divisors';
24
export { isPrime } from './prime';
5+
export { isPalindrome } from './isPalindrome';
6+
export { nullable };

src/helpers/isPalindrome.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { isPalindrome } from './isPalindrome';
2+
3+
describe('number is Palindrome', () => {
4+
it('some numbers are palindrome', () => {
5+
expect.assertions(4);
6+
7+
expect(isPalindrome(1)).toBe(true);
8+
expect(isPalindrome(131)).toBe(true);
9+
expect(isPalindrome(1221)).toBe(true);
10+
expect(isPalindrome(123454321)).toBe(true);
11+
});
12+
});

src/helpers/isPalindrome.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const isPalindrome = (n: number): boolean =>
2+
n.toString().split('').reverse().join('') === n.toString();
3+
4+
export default { isPalindrome };

src/problem0004.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Largest palindrome product
3+
*
4+
* https://projecteuler.net/problem=4
5+
*
6+
* A palindromic number reads the same both ways.
7+
* The largest palindrome made from the product of two 2-digit
8+
* numbers is 9009 = 91 × 99.
9+
*
10+
* Find the largest palindrome made from the product of two 3-digit numbers.
11+
*/
12+
13+
import logger from './logger';
14+
15+
import { problem0004 } from './problem0004';
16+
17+
describe('problem 0004', () => {
18+
it('problem 0004 solution found', () => {
19+
expect.assertions(1);
20+
21+
const solutionFound = 906609;
22+
23+
const bottom = 111;
24+
const top = 999;
25+
26+
const calculated = problem0004(bottom, top);
27+
28+
logger.info(`PROBLEM 0004 solution found: ${calculated}`);
29+
30+
expect(calculated).toBe(solutionFound);
31+
});
32+
});

src/problem0004.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Largest palindrome product
3+
*
4+
* https://projecteuler.net/problem=4
5+
*
6+
* A palindromic number reads the same both ways.
7+
* The largest palindrome made from the product of two 2-digit
8+
* numbers is 9009 = 91 × 99.
9+
*
10+
* Find the largest palindrome made from the product of two 3-digit numbers.
11+
*/
12+
13+
/// ////////////////////////////////////////////////////////////////////////////
14+
// NOTES ABOUT THE SOLUTION:
15+
// 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.
16+
// I think there must be another optimal solution to avoid testing all cases
17+
// cutting the loop around the largest factor pair
18+
// That's why I thought about doing the loop from highest to lowest.
19+
/// ////////////////////////////////////////////////////////////////////////////
20+
21+
/// ////////////////////////////////////////////////////////////////////////////
22+
23+
import logger from './logger';
24+
25+
import { nullable, isPalindrome } from './helpers/index';
26+
27+
function problem0004(_bottom: number, _top: number): nullable<number> {
28+
let i;
29+
let j;
30+
let foundi;
31+
let foundj;
32+
let foundPalindrome;
33+
34+
// Find all cases
35+
let cycles = 0;
36+
37+
i = _top;
38+
while (i >= _bottom) {
39+
j = i;
40+
while (j >= _bottom && (!foundj || j >= foundj)) {
41+
cycles += 1;
42+
43+
if (isPalindrome(j * i)) {
44+
logger.debug(`FOUND: ${i} x ${j} = ${j * i} is Palindrome`);
45+
46+
if (!foundPalindrome || i * j > foundPalindrome) {
47+
foundi = i;
48+
foundj = j;
49+
foundPalindrome = i * j;
50+
}
51+
} else {
52+
// console.log(`FOUND: ${i} x ${j} = ${j * i} is NOT Palindrome`);
53+
}
54+
55+
j -= 1;
56+
}
57+
58+
i -= 1;
59+
}
60+
61+
logger.info(
62+
`Problem 0004 Largest Palindrome => ${foundi} 𝗑 ${foundj} = ${foundPalindrome} in ${cycles} cycles`
63+
);
64+
65+
return foundPalindrome;
66+
}
67+
68+
export default problem0004;
69+
export { problem0004 };

0 commit comments

Comments
 (0)