Skip to content

Commit

Permalink
📝 Adapt decompPrime to be compatible with CodeSandbox (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Apr 14, 2020
1 parent f54ed4c commit 740bc94
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions example/001-simple/decompPrime/src/decompPrime.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
export const decompPrime = (n: number): number[] => {
// Quick implementation: the maximal number supported is 2**31-1
const stop = Math.sqrt(n);
for (let i = 2; i <= stop; ++i) {
if (n % i === 0) {
return [i, ...decompPrime(Math.floor(n / i))];
let done = false;
const factors: number[] = [];
while (!done) {
done = true;
const stop = Math.sqrt(n);
for (let i = 2; i <= stop; ++i) {
if (n % i === 0) {
factors.push(i);
n = Math.floor(n / i);
done = false;
break;
}
}
}
return [n];
return [...factors, n];
};

0 comments on commit 740bc94

Please sign in to comment.