# Need help with loop

• 08-09-2012, 11:18 AM
thegreatzo
Need help with loop
******* Solved: Thanks for your help mods *******
• 08-09-2012, 11:31 AM
JosAH
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
• 08-09-2012, 11:53 AM
thegreatzo
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?
• 08-09-2012, 12:08 PM
Tolls
Re: Need help with loop
Um, you cannot get 4 from adding numbers from 1 consecutively.
1+2 = 3
1+2+3 = 6
• 08-09-2012, 12:23 PM
thegreatzo
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
• 08-09-2012, 12:30 PM
Tolls
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.
• 08-09-2012, 12:38 PM
JosAH
Re: Need help with loop
Quote:

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
• 08-09-2012, 01:18 PM
thegreatzo
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;
}
• 08-09-2012, 01:19 PM
Tolls
Re: Need help with loop
Quote:

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.
• 08-09-2012, 01:31 PM
JosAH
Re: Need help with loop
Quote:

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
• 08-09-2012, 09:04 PM
JosAH
Re: Need help with loop
Quote:

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
• 08-09-2012, 10:51 PM
DarrylBurke
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
Quote:

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. :s: 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.

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+" ");  }  }  }```