Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Can't find mistake in my code

  1. #1
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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:

    /**
    *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!


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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,

          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);

  3. #3
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Can't find mistake in my code

    Quote Originally Posted by curmudgeon View Post
    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.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Can't find mistake in my code

    see edit above.

  5. #5
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Red face 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:


    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:

    /**
     *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!

Similar Threads

  1. Please, find my mistake
    By Daler in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2012, 11:17 PM
  2. where located my mistake
    By erdy_rezki in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 26th, 2012, 10:31 AM
  3. I am making a silly mistake but I cant find where! PLEASE HELP!
    By akmi5 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 14th, 2012, 01:44 AM
  4. Can't Find what is wrong with my Code
    By Raptorman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 04:12 PM
  5. I can't find out what is wrong with my code. Please Help!
    By hallor618 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2010, 02:44 PM