We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

what is wrong in my code.Iam not get output
public int divisorSum(int n)
{
int sum =0;
int i=0;
while( i<=n)
{
if(n%i==0)
sum=sum+i;
i++;
}
return sum;
}

You're right, if we were talking prime numbers, sqrt(n) would make sense, but not for divisors.

[Edit]: Let me correct myself, if you add i and n / i at the same step, you're adding the other number that is also a divisor in same step, take for example 8, if i is 2, you'll end up adding sum += 2 + 8/2 which traduces to sum += 2 + 4, both 2 & 4 are divisors of 8, keep doing that and you'll soon notice that this way the i variable only needs to go up to sqrt(n).

Here is a tip, a number's divisor will always be 1 and the number itself.
So you can initialize the sum as n+1 itself.
And the start the loop from i=2 till i<=n/2 because the greatest divisor of the number will not be greater than its half. Hence by this you shorten your for loop.
Also place a condition at start for n=1 then just return 1. Otherwise that test case wont be successful

This one worked for me..
class Calculator implements AdvancedArithmetic{
public int divisorSum(int n){
int sum=n;
for(int i=1;i<=n/2;i++){
if(n%i == 0){
sum=sum+i;
}
}
return sum;
}
}

## Day 19: Interfaces

You are viewing a single comment's thread. Return to all comments →

what is wrong in my code.Iam not get output public int divisorSum(int n) { int sum =0; int i=0; while( i<=n) { if(n%i==0) sum=sum+i; i++; } return sum; }

Your counter for the while loop starts at 0. It will compute the remainder modulo 0 in this first case, which does not make any sense.

change into int i = 1; insted of int i = 0;

Another point to note, is that After n/2, the only factor of n will be n itself. This reduces the running time of the whole algorithm

sqrt(n) will reduce it more.

just add i and n/i(if a pure int)

how is this correct for a number like 20? 10 is larger than sqrt(20), am confused here

You're right, if we were talking prime numbers,

`sqrt(n)`

would make sense, but not for divisors.[Edit]:Let me correct myself, if you add`i`

and`n / i`

at the same step, you're adding the other number that is also a divisor in same step, take for example`8`

, if`i`

is`2`

, you'll end up adding`sum += 2 + 8/2`

which traduces to`sum += 2 + 4`

, both`2`

&`4`

are divisors of`8`

, keep doing that and you'll soon notice that this way the`i`

variable only needs to go up to`sqrt(n)`

.yes i got it thank you very much

If n=1, then this would return 2, no? even though it should return 1.

Use proper format when u paste code.

Here is a tip, a number's divisor will always be 1 and the number itself. So you can initialize the sum as n+1 itself. And the start the loop from i=2 till i<=n/2 because the greatest divisor of the number will not be greater than its half. Hence by this you shorten your for loop. Also place a condition at start for n=1 then just return 1. Otherwise that test case wont be successful

i think this would fail for the case of 1, n+1 will be 2 where the correct answer is just 1

To account for a special case like 1, you one can leverage the ternary operator to assign value to sum like so:

This will mitigate logic errors in code later on.

thanks! this worked for me

This one worked for me.. class Calculator implements AdvancedArithmetic{ public int divisorSum(int n){ int sum=n; for(int i=1;i<=n/2;i++){ if(n%i == 0){ sum=sum+i; } } return sum; } }

put that i++ outside of if condition.

int sum=0; int i=1; while(i<=n) { if(n%i==0) { sum+=i; } i++; }