-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from sir-gon/feature/problem0004
Feature/problem0004
- Loading branch information
Showing
5 changed files
with
121 additions
and
0 deletions.
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 |
---|---|---|
@@ -1,2 +1,6 @@ | ||
type nullable<T> = T | null | undefined; | ||
|
||
export { divisors } from './divisors'; | ||
export { isPrime } from './prime'; | ||
export { isPalindrome } from './isPalindrome'; | ||
export { nullable }; |
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,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); | ||
}); | ||
}); |
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,4 @@ | ||
export const isPalindrome = (n: number): boolean => | ||
n.toString().split('').reverse().join('') === n.toString(); | ||
|
||
export default { isPalindrome }; |
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,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); | ||
}); | ||
}); |
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,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 }; |