Created
July 5, 2016 09:08
-
-
Save wongsyrone/94c31841c93743042761acd0633419ee to your computer and use it in GitHub Desktop.
find sum of all divisors
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
public int divisorSum(int n){ | |
// n has no divisors other than itself | |
if(n == 1){ | |
return n; | |
} | |
// Find and sum divisors: | |
int half = n/2; | |
int sum = n; | |
do { | |
if(n % half == 0){ | |
sum += half; | |
} | |
} while( half-- > 1 ); | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment