Results 1 to 12 of 12
Thread: Need help with loop
 08092012, 10:18 AM #1Member
 Join Date
 Aug 2012
 Posts
 5
 Rep Power
 0
 08092012, 10:31 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,372
 Blog Entries
 7
 Rep Power
 25
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,
JossThe only person who got everything done by Friday was Robinson Crusoe.
 08092012, 10:53 AM #3Member
 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; 08092012 at 01:30 PM.
 08092012, 11:08 AM #4Moderator
 Join Date
 Apr 2009
 Posts
 13,062
 Rep Power
 23
Re: Need help with loop
Um, you cannot get 4 from adding numbers from 1 consecutively.
1+2 = 3
1+2+3 = 6Please do not ask for code as refusal often offends.
** This space for rent **
 08092012, 11:23 AM #5Member
 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
 08092012, 11:30 AM #6Moderator
 Join Date
 Apr 2009
 Posts
 13,062
 Rep Power
 23
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 **
 08092012, 11:38 AM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,372
 Blog Entries
 7
 Rep Power
 25
 08092012, 12:18 PM #8Member
 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; 08092012 at 12:24 PM.
 08092012, 12:19 PM #9Moderator
 Join Date
 Apr 2009
 Posts
 13,062
 Rep Power
 23
 08092012, 12:31 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,372
 Blog Entries
 7
 Rep Power
 25
Re: Need help with loop
The only person who got everything done by Friday was Robinson Crusoe.
 08092012, 08:04 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,372
 Blog Entries
 7
 Rep Power
 25
Re: Need help with loop
The only person who got everything done by Friday was Robinson Crusoe.
 08092012, 09:51 PM #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+" "); } } }
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads

Problem with while loop, assigning a variable with a different value every loop? Help
By JavaProg in forum New To JavaReplies: 2Last Post: 11072011, 03:25 AM 
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10012011, 02:12 AM 
JTextField loop 2x forloop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02162011, 06:46 PM 
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04302010, 05:05 PM 
whileloop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06262009, 08:46 PM
Bookmarks