# Can't find mistake in my code

• November 18th, 2012, 10:27 PM
Daler
Can't find mistake in my code
Hey guys,

I have a little problem here. The program runs and everything is fine except the actual result. The question is:

Design Java code to calculate and print ∑ (n! + 5) / (n+1)! ; n is an integer from 1 to 10.

I have written the code and I it's here:

Code java:

```/** *Question #2 */   public class Sum { public static void main (String [] args) { long a = 1; long b = 2 * a; double sum = (a + 5.0) / b;   long n = 3; while(n<=11) { sum = sum + (double)(a + 5.0) / b; b = n * b; a = a * (n - 1); //probably mistake here n++; }//end of a loop   System.out.println("The answer is " + sum); } }```

I checked on my calculator and the result is 5.611286476

But my output is 8.520377259700176

Please, guys. I don't know where should I go. Help me find my mistake!
• November 18th, 2012, 11:00 PM
curmudgeon
Re: Can't find mistake in my code
note the difference between (n - 1) and (n + 1). It's a critical distinction.

In pseudo code, I've done something like,

Code :

``` declare int variable, maxI and set it to 10. This will be upper limit of for loop. declare int variable, n, and set it to 0. declare long (or probably could be int) variable, nPerm, that represents n! and set it to 1. declare long (or probably could be int) variable, nPlus1Perm, that represents (n + 1)! and set it to 1. declare a double sum variable and set to 0. for loop with int i going from 0 to less than maxI (it will loop 10 times) set n to i + 1. set nPerm to hold the current value of nPlus1Perm; advance nPlus1Perm by multiplying it by (n + 1) and assigning the value back into the nPlus1Perm variable   calculate a double prod variable as per the given equastion. add the prod to the sum   end of for loop System.out.println("sum: " + sum);```
• November 18th, 2012, 11:07 PM
Daler
Re: Can't find mistake in my code
Quote:

Originally Posted by curmudgeon
note the difference between (n - 1) and (n + 1). It's a critical distinction.

Yes, sir. I've tried it too, but it doesn't give the right answer either. I can't quite understand whether I should add something else to it.
Sorry.
• November 18th, 2012, 11:11 PM
curmudgeon
Re: Can't find mistake in my code
see edit above.
• November 19th, 2012, 12:19 AM
Daler
Re: Can't find mistake in my code
Thanks for very clear algorithm. The answer is correct when I don't put factorial "!" into my calculator(18.07950938), but when I put "!" in the calculator it gives me a different answer with the code you helped me out with.
Does the "!" play role?

Code:

Quote:

public class AnotherTry
{
public static void main (String [] args)
{
int maxI = 10;
int n = 0;
long nPerm = 1;//n
long nPlus1Perm = 1;//(n+1)
double sum = 0;

for (int i = 0; i < maxI; i++)
{
n = i + 1;
nPerm = nPlus1Perm;
nPlus1Perm = nPlus1Perm *(n+1);
double prod = (n + 5.0) / (n+1.0);
sum += prod;
}
System.out.println("sum: " + sum);
}
}
Thanks a lot for your support!!! I am just a little bit confused here

--- Update ---

I've finally got where I have made a mistake! My code was correct but the instructions in the loop weren't ordered on the right place.
Here is a code:

Code :

```/** *Question #2 */   public class Sum { public static void main (String [] args) { long a = 1; long b = 2 * a; double sum = (a + 5.0) / b; long n = 3;   while(n<=11) { a = a * (n - 1); b = n * b; sum = sum + (double)(a + 5.0) / b; n++; }//end of a loop   System.out.println("The answer is " + sum); } }```

The output is 5.611286475869809 as it should be.

Thanks for the effort anyway!