Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit18a9157

Browse files
authored
Added a new Maths algorithm to determine if two non-null integers are "friendly numbers" (TheAlgorithms#1267)
1 parentd6be3a4 commit18a9157

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+
exportconstFriendlyNumbers=(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+
thrownewError('The two parameters must be distinct, non-null integers')
15+
}
16+
17+
returnabundancyIndex(firstNumber)===abundancyIndex(secondNumber)
18+
}
19+
20+
functionabundancyIndex(number){
21+
returnsumDivisors(number)/number
22+
}
23+
24+
functionsumDivisors(number){
25+
letrunningSumDivisors=number
26+
for(leti=0;i<number/2;i++){
27+
if(Number.isInteger(number/i)){
28+
runningSumDivisors+=i
29+
}
30+
}
31+
returnrunningSumDivisors
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp