Results 1 to 12 of 12
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By DarrylBurke

Thread: Need help with loop

  1. #1
    thegreatzo is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Post Need help with loop

    ******* Solved: Thanks for your help mods *******
    Last edited by thegreatzo; 08-09-2012 at 02:31 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help with loop

    If a number 'A' is the sum of the first 'n' numbers then n == (Math.sqrt(1+8*A)-1)/2. Do your math.

    kind regards,

    Joss
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    thegreatzo is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default Re: Need help with loop

    Thanks for the formula, it kinda takes me a while to wrap my head around the math. I tried implementing it in, but it doesn't recognise that 4 isn't an exact sum for an example. When you input 4, the (k == 0) part should take over, instead it returns 1 to 2. :/ Did I not put it in properly?
    Last edited by thegreatzo; 08-09-2012 at 02:30 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Need help with loop

    Um, you cannot get 4 from adding numbers from 1 consecutively.
    1+2 = 3
    1+2+3 = 6
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    thegreatzo is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default Re: Need help with loop

    Yes I know that, but the problem is the program is not recognising that. It's supposed to work out that if you can't get the sum of n by adding numbers from 1 consecutively then it returns 0 and the message "the number n is not an exact sum". How can I get it to do that?
    Testing it I tried:

    Type an integer:
    3
    The number 3 is the sum of integers from 1 to 2 - this is correct

    Type an integer:
    4
    The number 4 is the sum of integers from 1 to 2 - this is incorrect

    Type an integer:
    5
    The number 5 is the sum of integers from 1 to 2 - this is incorrect

    Type an integer:
    6
    The number 6 is the sum of integers from 1 to 3 - this is correct

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Need help with loop

    That's because Jos' formula has one important criteria you are not taking into account:
    "If a number 'A' is the sum of the first 'n' numbers ..."

    You will need to calculate 'n' and then do a sum to check whether it is actually correct.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help with loop

    Quote Originally Posted by Tolls View Post
    That's because Jos' formula has one important criteria you are not taking into account:
    "If a number 'A' is the sum of the first 'n' numbers ..."

    You will need to calculate 'n' and then do a sum to check whether it is actually correct.
    No need for the sum thingy; you only have to check whether or not the value of that formula is (represents) an int number.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    thegreatzo is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default Re: Need help with loop

    How do i do that lol?

    Do you reckon an if statement for suffice for that?
    something like:
    If (the sum of k does not equal n) {
    k = 0;
    }
    Last edited by thegreatzo; 08-09-2012 at 01:24 PM.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Need help with loop

    Quote Originally Posted by JosAH View Post
    No need for the sum thingy; you only have to check whether or not the value of that formula is (represents) an int number.

    kind regards,

    Jos
    Ah yes.
    Thought there had to be an easier way...didn't notice they were casting to int.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help with loop

    Quote Originally Posted by thegreatzo View Post
    How do i do that lol?

    Do you reckon an if statement for suffice for that?
    something like:
    If (the sum of k does not equal n) {
    k = 0;
    }
    I'm not going to tell you because you didn't do the math and you're going to end up with a bit of code you don't understand but magically works; so it'll be a miracle to you and computer science has nothing to do with miracles or magic wands.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help with loop

    Quote Originally Posted by thegreatzo View Post
    ******* Solved: Thanks for your help mods *******
    Please don't edit your original post such that it doesn't contain your original question anymore. Now this entire thread is useless for others who might have googled for this particular question or went looking for it otherwise.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Need help with loop

    I had a copy in a cached page, so here it is. Any more removal of relevant content and the member will be banned.

    db
    Hey there,
    I've got a program which I need to test whether an integer n is the exact sum of k consecutive integer numbers starting from 1.
    eg. 6 is 1 + 2 + 3 and therefore k = 3 and if I put in 4 then it returns false. But what I've only able to achieve so far is to get the program to add numbers 1 to n together, so if I enter 6 I get 21 because 1 + 2 + 3 + 4 + 5 + 6 = 21, but this isn't what I need the program to do. I'm kinda stuck, I've tried a few other things but they aren't working. How can I get the loop to increment up to n then return that last number. eg. 6 returns 3 as mentioned above.

    Java Code:
     import java.util.Scanner;
     public class ExactSum {
    
     /**
     * @param args
     */
     public static void main(String[] args) {
     Scanner keyboard = new Scanner(System.in);
     System.out.println("Type an integer:");
     int n = keyboard.nextInt();
     int k = 0; 
     for(int i = 1;i<=n;i++) {
     k = k + i; 
     }
     if (k == 0) {
     System.out.println("The number "+n+" is not an exact sum");
     } 
     else {System.out.println("The number "+n+" is the sum of integers from 1 to "+k+" ");
     }
    
     }
     }
    Thanks for your help!
    HelloWorld1234 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 03:25 AM
  2. Replies: 1
    Last Post: 10-01-2011, 03:12 AM
  3. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 06:46 PM
  4. Replies: 5
    Last Post: 04-30-2010, 06:05 PM
  5. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •