Skip to content

Commit e22e33b

Browse files
committed
New helper: prime factors
1 parent a2d7ee7 commit e22e33b

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

src/helpers/divisors.test.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { divisors } from './divisors';
1+
import { divisors, nextPrimeFactor, primeFactors } from './divisors';
22

33
describe('divisors of a number', () => {
4+
it('divisors of one', () => {
5+
expect.assertions(1);
6+
7+
expect(divisors(1)).toStrictEqual([1]);
8+
});
9+
410
it('divisors of a number', () => {
511
expect.assertions(5);
612

@@ -10,4 +16,50 @@ describe('divisors of a number', () => {
1016
expect(divisors(9)).toStrictEqual([1, 3, 9]);
1117
expect(divisors(16)).toStrictEqual([1, 2, 4, 8, 16]);
1218
});
19+
20+
it('next prime factor of a target number', () => {
21+
expect.assertions(5);
22+
23+
expect(nextPrimeFactor(1)).toStrictEqual({
24+
factor: 1,
25+
carry: 1,
26+
cycles: 0
27+
});
28+
expect(nextPrimeFactor(2)).toStrictEqual({
29+
factor: 2,
30+
carry: 1,
31+
cycles: 1
32+
});
33+
expect(nextPrimeFactor(4)).toStrictEqual({
34+
factor: 2,
35+
carry: 2,
36+
cycles: 1
37+
});
38+
expect(nextPrimeFactor(9)).toStrictEqual({
39+
factor: 3,
40+
carry: 3,
41+
cycles: 2
42+
});
43+
expect(nextPrimeFactor(7)).toStrictEqual({
44+
factor: 7,
45+
carry: 1,
46+
cycles: 6
47+
});
48+
});
49+
50+
it('prime factors of number', () => {
51+
expect.assertions(5);
52+
53+
expect(primeFactors(1)).toStrictEqual({ factors: [1], cycles: 1 });
54+
expect(primeFactors(2)).toStrictEqual({ factors: [2], cycles: 1 });
55+
expect(primeFactors(6)).toStrictEqual({ factors: [2, 3], cycles: 3 });
56+
expect(primeFactors(12)).toStrictEqual({
57+
factors: [2, 2, 3],
58+
cycles: 4
59+
});
60+
expect(primeFactors(120)).toStrictEqual({
61+
factors: [2, 2, 2, 3, 5],
62+
cycles: 9
63+
});
64+
});
1365
});

src/helpers/divisors.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,66 @@ export const divisors = (target: number): number[] => {
3838
return divs;
3939
};
4040

41+
export interface PrimeFactor {
42+
factor: number;
43+
carry: number;
44+
cycles: number;
45+
}
46+
47+
export const nextPrimeFactor = (_target: number): PrimeFactor => {
48+
const top = Math.abs(_target);
49+
let cycles = 0;
50+
51+
if (top === 1) {
52+
return {
53+
factor: 1,
54+
carry: 1,
55+
cycles: cycles
56+
};
57+
}
58+
59+
let i = 2;
60+
while (i < top) {
61+
cycles += 1;
62+
if (top % i === 0) {
63+
return {
64+
factor: i,
65+
carry: top / i,
66+
cycles: cycles
67+
};
68+
}
69+
i += 1;
70+
}
71+
72+
return {
73+
factor: i,
74+
carry: top / i,
75+
cycles: cycles + 1
76+
};
77+
};
78+
79+
export const primeFactors = (target: number) => {
80+
const factors = [];
81+
let cycles = 0;
82+
83+
if (target === 1) {
84+
return { factors: [1], cycles: 1 };
85+
}
86+
87+
let factor = target;
88+
while (factor !== 1) {
89+
const partial = nextPrimeFactor(factor);
90+
cycles += partial.cycles;
91+
92+
factors.push(partial.factor);
93+
factor = partial.carry;
94+
}
95+
96+
return { factors: factors, cycles: cycles };
97+
};
98+
4199
export default {
42-
divisors
100+
divisors,
101+
primeFactors,
102+
nextPrimeFactor
43103
};

src/helpers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
type nullable<T> = T | null | undefined;
22

3-
export { divisors } from './divisors';
3+
export { divisors, primeFactors, nextPrimeFactor } from './divisors';
44
export { isPrime } from './prime';
55
export { isPalindrome } from './isPalindrome';
66
export { nullable };

0 commit comments

Comments
 (0)