tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
21
Task/Perfect-numbers/JavaScript/perfect-numbers.js
Normal file
21
Task/Perfect-numbers/JavaScript/perfect-numbers.js
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
function is_perfect(n)
|
||||
{
|
||||
var sum = 1, i, sqrt=Math.floor(Math.sqrt(n));
|
||||
for (i = sqrt-1; i>1; i--)
|
||||
{
|
||||
if (n % i == 0) {
|
||||
sum += i + n/i;
|
||||
}
|
||||
}
|
||||
if(n % sqrt == 0)
|
||||
sum += sqrt + (sqrt*sqrt == n ? 0 : n/sqrt);
|
||||
return sum === n;
|
||||
}
|
||||
|
||||
|
||||
var i;
|
||||
for (i = 1; i < 10000; i++)
|
||||
{
|
||||
if (is_perfect(i))
|
||||
print(i);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue