ISourceCode

Make the frequent cases fast and the rare case correct

Algorithm that shows a Number (if possible) can be represented as sum of atleast two consecutive numbers

Back to writing. course projects have kept me busy, meanwhile my friend raised this problem and did some reading of his own and has given me code and material to put on this blog. Thanks to him.

The problem is simple :
Algorithm to compute many different ways an input number may be
written as a sum of a sequence of at least two consecutive positive integers.

This forum has a good solution to this problem.

//AUTHOR: KR 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.io.*;
public class ConsecutiveSumFinder 
{

	public static void main(String[] args)
	{
		
		Scanner myscan = new Scanner(System.in);
		System.out.print("Please enter a number: ");
		int number = myscan.nextInt();
	
		ConsecutiveSumFinder csf = new ConsecutiveSumFinder();
		// returns all the odd factors of the input number
		ArrayList factors = csf.findFactor(number);

		// if the input doesnt have odd factors then the input
		//cannot be decomposed to sum of consecutive numbers
		if(factors.size()>0)
		{
			System.out.println(number+": Can be represented as consecutive sum of");
			Iterator factorsIterate = factors.iterator();
			while(factorsIterate.hasNext())
			{
				int factor = Integer.parseInt(factorsIterate.next().toString());
				int noOfno = (2*number)/factor;
				int temp = noOfno;
				int diff = temp - factor;
				temp = Math.abs(diff);
				temp = temp+1;
				temp = temp/2;
				System.out.print(number+"=");
				if(noOfno > factor)
				{
					noOfno = factor;
				}
				for(int j=0;j<noOfno;j++)
				{
					System.out.print(temp+"+");
					temp++;
				}
				System.out.println("");
			}
		}
		else
		{
			System.out.println(number+": Cannot be represented as sum of consecutive numbers");
		}
	}

	private ArrayList findFactor(int number)
	{
		int factor = 0;
		ArrayList factors = new ArrayList();
		for (int i = 1; i <= number; i++)
		{
			if(number % i == 0)
			{
				factor = number / i;
				if(factor % 2 != 0)
				{
					if(factor != 1)
					{
						factors.add(factor);
					}
				}
			}
		}
		return factors;
	}
}

OUTPUT:
labuser@ubuntu:~$ java ConsecutiveSumFinder
Please enter a number: 99
99: Can be represented as consecutive sum of
99=49+50+
99=14+15+16+17+18+19+
99=4+5+6+7+8+9+10+11+12+13+14+
99=7+8+9+10+11+12+13+14+15+
99=32+33+34+

labuser@ubuntu:~$ java ConsecutiveSumFinder
Please enter a number: 8
8: Cannot be represented as sum of consecutive numbers

The Total number of possible sequences can be calculated in the following manner:

import java.util.Scanner;
public class SeqSum
{
	public static void main(String[] args)
	{
		System.out.println("Enter the number");	
		Scanner in=new Scanner(System.in);
		int a=in.nextInt();
		int count=0;
		int t=1;
		for(int i=2;i< (Math.sqrt(2*a)+1);i++)
		{
			t+=i;
	   		if((a-t)%i==0&&a>=t) count++;
	  	}
		if(count !=0)
	 	{
			System.out.println("Possible number of sequence are :"+count);
		}
		else
		{
			System.out.println("No Sequence possible");
		}
	}
}

OUPUT:

labuser@ubuntu:~$ java SeqSum
Enter the number
99
Possible number of sequence are :5

labuser@ubuntu:~$ java SeqSum
Enter the number
8
No Sequence possible
labuser@ubuntu:~$

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: