# Thread: Need help with loop

1. Member Join Date
Aug 2012
Posts
5
Rep Power
0

## Need help with loop

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

2. ## 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  Reply With Quote

3. Member Join Date
Aug 2012
Posts
5
Rep Power
0

## 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 01:30 PM.  Reply With Quote

4. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Need help with loop

Um, you cannot get 4 from adding numbers from 1 consecutively.
1+2 = 3
1+2+3 = 6  Reply With Quote

5. Member Join Date
Aug 2012
Posts
5
Rep Power
0

## 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  Reply With Quote

6. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## 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.  Reply With Quote

7. ## Re: Need help with loop Originally Posted by Tolls 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  Reply With Quote

8. Member Join Date
Aug 2012
Posts
5
Rep Power
0

## 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 12:24 PM.  Reply With Quote

9. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Need help with loop Originally Posted by JosAH 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.  Reply With Quote

10. ## Re: Need help with loop Originally Posted by thegreatzo 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  Reply With Quote

11. ## Re: Need help with loop Originally Posted by thegreatzo ******* 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  Reply With Quote

12. ## 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+" ");
}

}
}```  Reply With Quote