Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('max_array_sum', () => {
expect(answer).toStrictEqual(test.expected);
});

expect(TEST_CASES).toHaveLength(3);
expect(ALL_TEST_CASES).toHaveLength(4);
});

it('maxSubsetSum edge case zero', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ describe('ctci_recursive_staircase', () => {
});
});

expect(TEST_CASES).toHaveLength(3);
expect(TEST_CASES_GENERALIZED).toHaveLength(3);
});
});
3 changes: 3 additions & 0 deletions src/projecteuler/helpers/divisors.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ export const primeFactors = (target) => {
return { 'factors': factors, 'cycles': cycles };
};

export const isPrime = (target) =>
target !== 1 && target === nextPrimeFactor(target).factor;

export const abundance = (target) => {
const theDivisors = properDivisors(target);
const divSum = sum(theDivisors);
Expand Down
34 changes: 32 additions & 2 deletions src/projecteuler/helpers/divisors.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { describe, expect, it } from '@jest/globals';
import {
divisors,
abundance,
divisors,
isPrime,
nextPrimeFactor,
primeFactors,
properDivisors,
___DIVISORS_DEFICIENT___,
___DIVISORS_PERFECT___,
___DIVISORS_ABUNDANT___
} from './divisors.js';

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

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

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

expect(properDivisors(1)).toStrictEqual([]);
expect(properDivisors(2)).toStrictEqual([1]);
expect(properDivisors(8)).toStrictEqual([1, 2, 4]);
expect(properDivisors(9)).toStrictEqual([1, 3]);
expect(properDivisors(16)).toStrictEqual([1, 2, 4, 8]);
});

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

Expand Down Expand Up @@ -81,6 +93,24 @@ describe('divisors of a number', () => {
});
});

it('some numbers are prime', () => {
expect.assertions(4);

expect(isPrime(1)).toBe(false);
expect(isPrime(2)).toBe(true);
expect(isPrime(7)).toBe(true);
expect(isPrime(13)).toBe(true);
});

it('some numbers are not prime', () => {
expect.assertions(4);

expect(isPrime(4)).toBe(false);
expect(isPrime(10)).toBe(false);
expect(isPrime(100)).toBe(false);
expect(isPrime(3000)).toBe(false);
});

it('abundance of a integer number', () => {
expect.assertions(3);

Expand Down