Content-Length: 301312 | pFad | http://github.com/devtarun/TheAlgorithms-JavaScript/commit/18a91573c3f0bcf891463009dd550a5dd8acf7a7

52 Added a new Maths algorithm to determine if two non-null integers are… · devtarun/TheAlgorithms-JavaScript@18a9157 · GitHub
Skip to content

Commit 18a9157

Browse files
authored
Added a new Maths algorithm to determine if two non-null integers are "friendly numbers" (TheAlgorithms#1267)
1 parent d6be3a4 commit 18a9157

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Maths/FriendlyNumbers.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
'In number theory, friendly numbers are two or more natural numbers with a common abundancy index, the
3+
ratio between the sum of divisors of a number and the number itself.'
4+
Source: https://en.wikipedia.org/wiki/Friendly_number
5+
See also: https://mathworld.wolfram.com/FriendlyNumber.html#:~:text=The%20numbers%20known%20to%20be,numbers%20have%20a%20positive%20density.
6+
*/
7+
8+
export const FriendlyNumbers = (firstNumber, secondNumber) => {
9+
// input: two integers
10+
// output: true if the two integers are friendly numbers, false if they are not friendly numbers
11+
12+
// First, check that the parameters are valid
13+
if (!Number.isInteger(firstNumber) || !Number.isInteger(secondNumber) || firstNumber === 0 || secondNumber === 0 || firstNumber === secondNumber) {
14+
throw new Error('The two parameters must be distinct, non-null integers')
15+
}
16+
17+
return abundancyIndex(firstNumber) === abundancyIndex(secondNumber)
18+
}
19+
20+
function abundancyIndex (number) {
21+
return sumDivisors(number) / number
22+
}
23+
24+
function sumDivisors (number) {
25+
let runningSumDivisors = number
26+
for (let i = 0; i < number / 2; i++) {
27+
if (Number.isInteger(number / i)) {
28+
runningSumDivisors += i
29+
}
30+
}
31+
return runningSumDivisors
32+
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/devtarun/TheAlgorithms-JavaScript/commit/18a91573c3f0bcf891463009dd550a5dd8acf7a7

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy