Created
March 3, 2014 21:16
-
-
Save aratak/9334783 to your computer and use it in GitHub Desktop.
perfect number
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
isDivider = (n, d)-> n % d is 0 | |
maxDivider = (i)-> parseInt(Math.sqrt(i)) | |
sumOfDividers = (n)-> | |
sum = 1; | |
i = 2 | |
while i <= maxDivider(n) | |
if isDivider(n, i) | |
sum += i | |
sum += reverse if (reverse = parseInt(n / i)) isnt i | |
i++ | |
sum | |
isPerfect = (i)-> | |
return false if i < 6 | |
return sumOfDividers(i) is i; | |
next = (i)-> i+1 | |
findPerfect = (number, i=0)-> | |
return 6 if number is 1 | |
while true | |
if isPerfect(i) | |
number-- | |
return i if number is 0 | |
i=next(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment