Skip to content

Added Aliquot Sum Implementation #810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 26, 2021
27 changes: 27 additions & 0 deletions Maths/AliquotSum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
A program to calculate the Aliquot Sum of a number.
Article on Aliquot Sum: https://en.wikipedia.org/wiki/Aliquot_sum
*/

/**
* @param {Number} input The number whose aliquot sum you want to calculate
*/
function aliquotSum (input) {
// input can't be negative
if (input < 0) throw new TypeError('Input cannot be Negative')

// input can't be a decimal
if (Math.floor(input) !== input) throw new TypeError('Input cannot be a Decimal')

// Dealing with 1, which isn't a prime
if (input === 1) return 0

let sum = 0
for (let i = 1; i <= (input / 2); i++) {
if (input % i === 0) sum += i
}

return sum
}

export { aliquotSum }
15 changes: 15 additions & 0 deletions Maths/test/AliquotSum.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { aliquotSum } from '../AliquotSum'

describe('Aliquot Sum of a Number', () => {
it('Aliquot Sum of 6', () => {
expect(aliquotSum(6)).toBe(6)
})

it('Aliquot Sum of 1', () => {
expect(aliquotSum(1)).toBe(0)
})

it('Aliquot Sum of 28', () => {
expect(aliquotSum(28)).toBe(28)
})
})