Skip to content

Commit

Permalink
[Hacker Rank]: Project Euler #3: Largest prime factor. Optimized.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonzalo Diaz committed Sep 27, 2024
1 parent c598b58 commit f75ad03
Showing 1 changed file with 0 additions and 6 deletions.
6 changes: 0 additions & 6 deletions src/hackerrank/projecteuler/euler003.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,18 @@ export function primeFactor(n: bigint): bigint {

let divisor: bigint = n;
let maxPrimeFactor: bigint = divisor;
let mpfInitialized = false;

let i = 2n;

while (i <= BigIntMath.sqrt(divisor)) {
if (divisor % i === 0n) {
divisor /= i;
maxPrimeFactor = divisor;
mpfInitialized = true;
} else {
i += 1n;
}
}

if (!mpfInitialized) {
return n;
}

return maxPrimeFactor;
}

Expand Down

0 comments on commit f75ad03

Please sign in to comment.