forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFriendlyNumbers.js
More file actions
Latest commit
38 lines (33 loc) · 1.21 KB
/
Copy pathFriendlyNumbers.js
File metadata and controls
38 lines (33 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
'In number theory, friendly numbers are two or more natural numbers with a common abundancy index, the
ratio between the sum of divisors of a number and the number itself.'
Source: https://en.wikipedia.org/wiki/Friendly_number
See also: https://mathworld.wolfram.com/FriendlyNumber.html#:~:text=The%20numbers%20known%20to%20be,numbers%20have%20a%20positive%20density.
*/
exportconstFriendlyNumbers=(firstNumber,secondNumber)=>{
// input: two integers
// output: true if the two integers are friendly numbers, false if they are not friendly numbers
// First, check that the parameters are valid
if(
!Number.isInteger(firstNumber)||
!Number.isInteger(secondNumber)||
firstNumber===0||
secondNumber===0||
firstNumber===secondNumber
){
thrownewError('The two parameters must be distinct, non-null integers')
}
returnabundancyIndex(firstNumber)===abundancyIndex(secondNumber)
}
functionabundancyIndex(number){
returnsumDivisors(number)/number
}
functionsumDivisors(number){
letrunningSumDivisors=number
for(leti=0;i<number/2;i++){
if(Number.isInteger(number/i)){
runningSumDivisors+=i
}
}
returnrunningSumDivisors
}