JAVA: Find Consecutive Int who's sum is a number

| Posted in , , , | Posted on Friday, December 04, 2009

UPDATE: The internet is awesome. I was able to find two threads (here and here) where people were working on the same problem. I will post the final code once I test it
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

For my java class I'm writing a program. The assignment is this: have java get a number (int) from the user, then write a method that has java find ALL sets of consecutive integers who's sum is the users number.

At first I had no idea where to begin and though I've made progress, I'm still stuck.

I began by creating a few variables, initialized to 0, 0+1, 0+2, ETC and set up a loop that would execute if the variable sum was not equal to the users number. Each time the loop executed each variable would be incremented by one and thus java would run calculations over and over until it found an answer. This worked, but only for a set number of variables. Also java would only display that set and wouldnt go on to find any other fitting sets.

At this point, I'm not sure what direction to go with this. Perhaps an array of integers would be the best way to go, but how would you get java to loop that to find different sets? I know how to set it up to find a set of two, but how could you write a loop that would find the set of two (or not) then increment the set so java looked for consecutive sets of three?

Any suggestions (specific or conceptual) would be greatly appreciated.

I have a question posted about thid on yahoo answers here.

Below is my current code.


import java.util.Scanner;
import java.util.Random;


public class Extracredit1NiceNumbers
{
public static void main(String [] args)
{
double user; //Number to be entered by user
Scanner keyin = new Scanner(System.in);

//Get users number
System.out.println("Please enter a number");
user = keyin.nextInt();

//Call to method getSum which will calculate the 4 consecutive integers
getSum(user);

}

public static void getSum(double user) //Send this method the users #
{

float sum;
double count = 0.00; //Create variable to count how many times the loop initiates
double ranNum1; //These ranNums were for storing random numbers generated by java
double ranNum2;
double ranNum3; //Use doubles so java can calculate decimal numbers for answers
double ranNum4;
double ranSum; //For storing the sum of all the random numbers and compairing the random sum to the users number.

//Random random = new Random(); //Grab random number. I was trying to get java to pick 4 random numbers between 0 and 30 and compairing their sum to the user entereed number.
//Issue is it grabs any random numbers. Need to find a way for it to use consecutive random numbers.

//Gets random value for ranNum1 and then incriment that number by 1 to get consecutive integers. This will always execute at least once.
// This must be executed outside the while loop to intilize ranSum
ranNum1 = 0/*random.nextInt(user)*/;
ranNum2= ranNum1 + 1.00;
ranNum3= ranNum2+ 1.00;
ranNum4 = ranNum3+ 1.00;
ranSum = ranNum1 + ranNum2 + ranNum3 +ranNum1;


//This while loop will repeat so long as the random sum is not equal to the users number
while (ranSum != user)
{
ranNum1++;/*random.nextInt(user)*/;
ranNum2++;
ranNum3++;
ranNum4++;

ranSum = ranNum1 + ranNum2 + ranNum3 +ranNum4;

count++; //This is for programming purposes so I can tell if java is executing this while loop
System.out.println("The count is " + count);
System.out.println("The integers are: " + ranNum1 + " " + ranNum2 + " " + ranNum3 + " " + ranNum4 + " the sum is " + ranSum);
}

System.out.println("The consecutive integers whos sum is " + user + " are " + ranNum1 + " " + ranNum2 + " " + ranNum3 + " " + ranNum4);



}



}

_____________________

When you run the program and enter the number '10' t he output is as follows:

--------------------Configuration: --------------------
Please enter a number
10
The count is 1.0
The integers are: 1.0 2.0 3.0 4.0 the sum is 10.0
The consecutive integers whos sum is 10.0 are 1.0 2.0 3.0 4.0

Process completed.

_______________________________________________

UPDATE:

I figured out how to do it (kinda). I just copied the while loop and added a variable to each one so java would calculate a set of two, three, ETC. The disadvantage is that the program is limited (it'll only search for sets you actually coded in) but for now its okay. I assume using an array would be the better way to go, but I'm so unfamiliar with them I'm not sure how you'd get java to increment the # of variables per set or how to get it to display only the first and last numbers of each set. I'm sure theres a way to do it, its just beyond my expertise to do on my own.

Comments (0)