Skip to content

Commit 1848054

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] divisors tests standardized from "ts" project.
1 parent cc2ecb8 commit 1848054

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/projecteuler/helpers/divisors.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ export const primeFactors = (target) => {
112112
return { 'factors': factors, 'cycles': cycles };
113113
};
114114

115+
export const isPrime = (target) =>
116+
target !== 1 && target === nextPrimeFactor(target).factor;
117+
115118
export const abundance = (target) => {
116119
const theDivisors = properDivisors(target);
117120
const divSum = sum(theDivisors);

src/projecteuler/helpers/divisors.test.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { describe, expect, it } from '@jest/globals';
22
import {
3-
divisors,
43
abundance,
4+
divisors,
5+
isPrime,
56
nextPrimeFactor,
67
primeFactors,
8+
properDivisors,
79
___DIVISORS_DEFICIENT___,
810
___DIVISORS_PERFECT___,
911
___DIVISORS_ABUNDANT___
1012
} from './divisors.js';
1113

12-
describe('divisors of a number', () => {
14+
describe('divisors and prime numbers', () => {
1315
it('divisors of one', () => {
1416
expect.assertions(1);
1517

@@ -35,6 +37,16 @@ describe('divisors of a number', () => {
3537
]);
3638
});
3739

40+
it('proper divisors of a number', () => {
41+
expect.assertions(5);
42+
43+
expect(properDivisors(1)).toStrictEqual([]);
44+
expect(properDivisors(2)).toStrictEqual([1]);
45+
expect(properDivisors(8)).toStrictEqual([1, 2, 4]);
46+
expect(properDivisors(9)).toStrictEqual([1, 3]);
47+
expect(properDivisors(16)).toStrictEqual([1, 2, 4, 8]);
48+
});
49+
3850
it('next prime factor of a target number', () => {
3951
expect.assertions(5);
4052

@@ -81,6 +93,24 @@ describe('divisors of a number', () => {
8193
});
8294
});
8395

96+
it('some numbers are prime', () => {
97+
expect.assertions(4);
98+
99+
expect(isPrime(1)).toBe(false);
100+
expect(isPrime(2)).toBe(true);
101+
expect(isPrime(7)).toBe(true);
102+
expect(isPrime(13)).toBe(true);
103+
});
104+
105+
it('some numbers are not prime', () => {
106+
expect.assertions(4);
107+
108+
expect(isPrime(4)).toBe(false);
109+
expect(isPrime(10)).toBe(false);
110+
expect(isPrime(100)).toBe(false);
111+
expect(isPrime(3000)).toBe(false);
112+
});
113+
84114
it('abundance of a integer number', () => {
85115
expect.assertions(3);
86116

0 commit comments

Comments
 (0)