Files
JavaScript/Maths/AliquotSum.js
Roland Hummel 86d333ee94 feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest

* chore: migrate to vitest mock functions

* chore: code style (switch to prettier)

* test: re-enable long-running test

Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime!

see #1193

* chore: code style

* chore: fix failing tests

* Updated Documentation in README.md

* Update contribution guidelines to state usage of Prettier

* fix: set prettier printWidth back to 80

* chore: apply updated code style automatically

* fix: set prettier line endings to lf again

* chore: apply updated code style automatically

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
2023-10-04 02:38:19 +05:30

34 lines
1.1 KiB
JavaScript

/*
A program to calculate the Aliquot Sum of a number.
The aliquot sum of a number n, is the sum of all the proper divisors of n apart from n itself
For example, for the number 6
The divisors are 1, 2, 3 (we don't consider 6), so its aliquot sum is 1 + 2 + 3 = 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)
For all prime numbers, the aliquot sum is 1, since their only divisor apart from themselves is 1
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 }