Skip to content

Commit

Permalink
refactoring primeFactors
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Nov 2, 2021
1 parent b93e931 commit 2742c60
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 44 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prime-lib",
"version": "0.4.3",
"version": "0.4.4",
"description": "Prime number library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
75 changes: 37 additions & 38 deletions src/prime-factors.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,70 @@
/**
* Calculates prime factorization of a number.
*
* Works up to the maximum of 9007199254740991
* Works up to 2^53 - 1 = 9_007_199_254_740_991
*
* This is a draft for high-performance calculation that passes all tests.
* It just needs a bit of good refactoring.
* TODO: This is a draft for high-performance calculation.
* It still needs a bit of refactoring + tests.
*/

let numLeft: number;

const my = {
FArr: new Array<number>(),
MAX: 9007199254740991,
lowPrimes: getLowPrimes(),
lowPrimeN: 100
};

export function primeFactors(TheNum: number) {
my.FArr = [];
if (TheNum > my.MAX) {
return my.FArr;
export function primeFactors(x: number): number[] {
cfg.result = [];
if (x > cfg.max) {
return cfg.result;
}
numLeft = TheNum;
if (numLeft === 0 || numLeft === 1) {
return my.FArr;
cfg.numLeft = x;
if (cfg.numLeft === 0 || cfg.numLeft === 1) {
return cfg.result;
}
let doneQ = false;
for (var p = 0; p < my.lowPrimeN; p++) {
if (!testFact(my.lowPrimes[p])) {
let doneQ = false, p = 0;
for (p; p < cfg.lowPrimeN; p++) {
if (!testFact(cfg.lowPrimes[p])) {
doneQ = true;
break;
}
}
if (!doneQ) {
let fact = (((my.lowPrimes[p - 1] + 5) / 6) << 0) * 6 - 1;
let fact = (((cfg.lowPrimes[p - 1] + 5) / 6) << 0) * 6 - 1;
while (true) {
if (!testFact(fact)) break;
if (!testFact(fact)) {
break;
}
fact += 2;
if (!testFact(fact)) break;
if (!testFact(fact)) {
break;
}
fact += 4;
}
}
if (numLeft !== 1) addFact(numLeft, 1);

return my.FArr;
if (cfg.numLeft !== 1) {
addFact(cfg.numLeft, 1);
}
return cfg.result;
}

function testFact(fact: number): boolean {
let power = 0;
while (numLeft % fact === 0) {
while (cfg.numLeft % fact === 0) {
power++;
numLeft = numLeft / fact;
cfg.numLeft = cfg.numLeft / fact;
}
if (power !== 0) {
addFact(fact, power);
}
return numLeft / fact > fact;
return cfg.numLeft / fact > fact;
}

function addFact(fact: number, power: number) {
function addFact(fact: number, power: number): void {
for (let i = 0; i < power; i++) {
my.FArr.push(fact);
cfg.result.push(fact);
}
}

function getLowPrimes() {
return [
const cfg = {
result: [] as number[],
max: 9007199254740991, // = 2^53 - 1
numLeft: 0,
lowPrimeN: 100,
lowPrimes: [
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109,
113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,
241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379,
Expand All @@ -74,5 +73,5 @@ function getLowPrimes() {
673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827,
829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991,
997, 1009, 1013, 1019, 1021, 1031, 1033, 1039
];
}
]
};
5 changes: 0 additions & 5 deletions test/prime-factors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ describe('primeFactors', () => {
expect(primeFactors(600_851_475_143)).to.eql([71, 839, 1471, 6857]);
});
it('must handle large complex numbers', () => {
// TODO: This doesn't work currently (dead slow)
// 1125899906842622 = 2 ** 50 - 2
// calculators where it works well:
// - https://www.mathsisfun.com/numbers/prime-factorization-tool.html
// - https://www.omnicalculator.com/math/prime-factorization
expect(primeFactors(1_125_899_906_842_622)).to.eql([2, 127, 4432676798593]);
});
});

0 comments on commit 2742c60

Please sign in to comment.