-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ec34a04
Added LucasSeries
SpiderMath cc9f0bf
Added more tests and renamed function
SpiderMath d1a44d0
Merge branch 'TheAlgorithms:master' into master
SpiderMath a43a01e
Changed RangeError to TypeError
SpiderMath f5e1127
Merge branch 'TheAlgorithms:master' into master
SpiderMath f09aef1
Added Aliquot Sum and tests
SpiderMath 697b361
Merge branch 'TheAlgorithms:master' into master
SpiderMath e76aae2
Fix ALiquot tests, need to learn how to use Jest
SpiderMath f41d835
Merge branch 'TheAlgorithms:master' into master
SpiderMath 0551b72
Added some explanation for the Aliquot sum
SpiderMath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.