Skip to content

Commit 502aec2

Browse files
authored
merge: Added Aliquot Sum Implementation (#810)
* Added LucasSeries * Added more tests and renamed function * Changed RangeError to TypeError * Added Aliquot Sum and tests * Fix ALiquot tests, need to learn how to use Jest * Added some explanation for the Aliquot sum
1 parent 4fb0809 commit 502aec2

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

Maths/AliquotSum.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
A program to calculate the Aliquot Sum of a number.
3+
The aliquot sum of a number n, is the sum of all the proper divisors of n apart from n itself
4+
For example, for the number 6
5+
The divisors are 1, 2, 3 (we don't consider 6), so its aliquot sum is 1 + 2 + 3 = 6
6+
1 is the only number whose aliquot sum is 0 (since its only divisor is 1 and aliquot sum of a number couldn't have itself)
7+
For all prime numbers, the aliquot sum is 1, since their only divisor apart from themselves is 1
8+
Article on Aliquot Sum: https://en.wikipedia.org/wiki/Aliquot_sum
9+
*/
10+
11+
/**
12+
* @param {Number} input The number whose aliquot sum you want to calculate
13+
*/
14+
function aliquotSum (input) {
15+
// input can't be negative
16+
if (input < 0) throw new TypeError('Input cannot be Negative')
17+
18+
// input can't be a decimal
19+
if (Math.floor(input) !== input) throw new TypeError('Input cannot be a Decimal')
20+
21+
// Dealing with 1, which isn't a prime
22+
if (input === 1) return 0
23+
24+
let sum = 0
25+
for (let i = 1; i <= (input / 2); i++) {
26+
if (input % i === 0) sum += i
27+
}
28+
29+
return sum
30+
}
31+
32+
export { aliquotSum }

Maths/test/AliquotSum.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { aliquotSum } from '../AliquotSum'
2+
3+
describe('Aliquot Sum of a Number', () => {
4+
it('Aliquot Sum of 6', () => {
5+
expect(aliquotSum(6)).toBe(6)
6+
})
7+
8+
it('Aliquot Sum of 1', () => {
9+
expect(aliquotSum(1)).toBe(0)
10+
})
11+
12+
it('Aliquot Sum of 28', () => {
13+
expect(aliquotSum(28)).toBe(28)
14+
})
15+
})

0 commit comments

Comments
 (0)