-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Faster Project Euler 005. #1863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,20 +1,38 @@ | ||
| /* | ||
| Smallest multiple | ||
| import { PrimeFactors } from '../Maths/PrimeFactors.js' | ||
|
|
||
| 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. | ||
| What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? | ||
| */ | ||
| /** | ||
| * Smallest Multiple | ||
| * @link https://projecteuler.net/problem=5 | ||
| * | ||
| * 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? | ||
| * | ||
| * Method: unique factorization | ||
| * @link https://en.wikipedia.org/wiki/Least_common_multiple#Using_prime_factorization | ||
| * The method used below calculates the Least Common Multiple (LCM) by multiplying the largest powers of the prime factors of the divisors. For example, 8 is the LCM of 2, 4, and 8. The prime factors of these numbers are 2^1, 2^2, and 2^3. We can discard 2^1 and 2^2 since 2^3 is the largest power of 2, leaving us with a result of 2^3 (8). | ||
| */ | ||
|
|
||
| export const findSmallestMultiple = (maxDivisor) => { | ||
| const divisors = Array.from({ length: maxDivisor }, (_, i) => i + 1) | ||
| let num = maxDivisor + 1 | ||
| let result | ||
| export function findSmallestMultiple(maxDivisor) { | ||
| const maxPowers = {} | ||
| for (let divisor = 2; divisor <= maxDivisor; divisor++) { | ||
| const factors = PrimeFactors(divisor) | ||
|
|
||
| while (!result) { | ||
| const isDivisibleByAll = divisors.every((divisor) => num % divisor === 0) | ||
| if (isDivisibleByAll) result = num | ||
| else num++ | ||
| // combine/count prime factors | ||
| let powers = {} | ||
| for (const factor of factors) { | ||
| powers[factor] = (powers[factor] ?? 0) + 1 | ||
| } | ||
|
|
||
| // save largest factors | ||
| for (const factor in powers) { | ||
| if (powers[factor] > (maxPowers[factor] ?? 0)) { | ||
| maxPowers[factor] = powers[factor] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| // multiply all primes | ||
| return Object.entries(maxPowers).reduce( | ||
| (product, [prime, power]) => product * Math.pow(prime, power), | ||
| 1 | ||
| ) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.