Skip to content

Commit

Permalink
New helper: prime factors
Browse files Browse the repository at this point in the history
  • Loading branch information
sir-gon committed May 18, 2023
1 parent a2d7ee7 commit 102f707
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 3 deletions.
54 changes: 53 additions & 1 deletion src/helpers/divisors.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { divisors } from './divisors';
import { divisors, nextPrimeFactor, primeFactors } from './divisors';

describe('divisors of a number', () => {
it('divisors of one', () => {
expect.assertions(1);

expect(divisors(1)).toStrictEqual([1]);
});

it('divisors of a number', () => {
expect.assertions(5);

Expand All @@ -10,4 +16,50 @@ describe('divisors of a number', () => {
expect(divisors(9)).toStrictEqual([1, 3, 9]);
expect(divisors(16)).toStrictEqual([1, 2, 4, 8, 16]);
});

it('next prime factor of a target number', () => {
expect.assertions(5);

expect(nextPrimeFactor(1)).toStrictEqual({
factor: 1,
carry: 1,
cycles: 0
});
expect(nextPrimeFactor(2)).toStrictEqual({
factor: 2,
carry: 1,
cycles: 1
});
expect(nextPrimeFactor(4)).toStrictEqual({
factor: 2,
carry: 2,
cycles: 1
});
expect(nextPrimeFactor(9)).toStrictEqual({
factor: 3,
carry: 3,
cycles: 2
});
expect(nextPrimeFactor(7)).toStrictEqual({
factor: 7,
carry: 1,
cycles: 6
});
});

it('prime factors of number', () => {
expect.assertions(5);

expect(primeFactors(1)).toStrictEqual({ factors: [1], cycles: 1 });
expect(primeFactors(2)).toStrictEqual({ factors: [2], cycles: 1 });
expect(primeFactors(6)).toStrictEqual({ factors: [2, 3], cycles: 3 });
expect(primeFactors(12)).toStrictEqual({
factors: [2, 2, 3],
cycles: 4
});
expect(primeFactors(120)).toStrictEqual({
factors: [2, 2, 2, 3, 5],
cycles: 9
});
});
});
67 changes: 66 additions & 1 deletion src/helpers/divisors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,71 @@ export const divisors = (target: number): number[] => {
return divs;
};

export interface PrimeFactor {
factor: number;
carry: number;
cycles: number;
}

export const nextPrimeFactor = (_target: number): PrimeFactor => {
const top = Math.abs(_target);
let cycles = 0;

if (top === 1) {
return {
factor: 1,
carry: 1,
cycles: cycles
};
}

let i = 2;
while (i < top) {
cycles += 1;
if (top % i === 0) {
return {
factor: i,
carry: top / i,
cycles: cycles
};
}
i += 1;
}

return {
factor: i,
carry: top / i,
cycles: cycles + 1
};
};

export interface PrimeFactors {
factors: number[];
cycles: number;
}

export const primeFactors = (target: number): PrimeFactors => {
const factors = [];
let cycles = 0;

if (target === 1) {
return { factors: [1], cycles: 1 };
}

let factor = target;
while (factor !== 1) {
const partial = nextPrimeFactor(factor);
cycles += partial.cycles;

factors.push(partial.factor);
factor = partial.carry;
}

return { factors: factors, cycles: cycles };
};

export default {
divisors
divisors,
primeFactors,
nextPrimeFactor
};
8 changes: 7 additions & 1 deletion src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
type nullable<T> = T | null | undefined;

export { divisors } from './divisors';
export {
divisors,
primeFactors,
nextPrimeFactor,
PrimeFactor,
PrimeFactors
} from './divisors';
export { isPrime } from './prime';
export { isPalindrome } from './isPalindrome';
export { nullable };

0 comments on commit 102f707

Please sign in to comment.