Core iTOps Tube

Sunday, 6 May 2012

Sum a number in different ways.

Hello, I need some help to correct my algorithm steps to sum a number. I am confused... please input some steps



Example:

Let's say a number 5.

Now we can get a list of numbers: 1, 2, 3, 4 (less than 5 in sorted order).

so the sum could be: 1+4; 2+3 ( 1+1+1+1+1 or 1+2+2 is not a solution).



Example2: number=7, soultion=1+6; 1+2+4; 2+5; 3+4




Code:


My Algorithm Steps:

num=n

start_num=1

ending_num=num-1

arr= @(1 to num-1)



result = first_element + next_element



if result = num then print the first_element + next_element;

 else result = result + third_element



print " all solutions ":






No comments:

Post a Comment