******* Solved: Thanks for your help mods *******

Printable View

- 08-09-2012, 11:18 AMthegreatzoNeed help with loop
******* Solved: Thanks for your help mods *******

- 08-09-2012, 11:31 AMJosAHRe: 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 AMthegreatzoRe: 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 PMTollsRe: 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 PMthegreatzoRe: 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 PMTollsRe: 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 PMJosAHRe: Need help with loop
- 08-09-2012, 01:18 PMthegreatzoRe: 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 PMTollsRe: Need help with loop
- 08-09-2012, 01:31 PMJosAHRe: Need help with loop
- 08-09-2012, 09:04 PMJosAHRe: Need help with loop
- 08-09-2012, 10:51 PMDarrylBurkeRe: 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+" ");

}

}

}