Noobie programmer here. Trying to practice recursion and I looked up some examples. I'm trying to do one at the moment but I can't figure out a quick way to do it (especially not with recursion, which is the aim).

What I want to do is, given an array of ints of whatever length, check if an inputted number can be made from the addition of any combination of numbers from the integer array (assuming each number in the array can only be used once).

For example, given the array {1,2,5,8,10} if someone input 17 it would return true (2+5+10) but if someone entered 4 it would return false because no combination of those numbers can add up to 4.

Can this be done with recursion? The example question I was looking at only had an array with 3 numbers, I want to create a method that can work with an array of any length.

Thanks for your time,

Dinipants ]]>

For example - given the set { a, b, c, d}

the sequential combinations would be

{ {a}, {b}, {c}, {d} }

{ {a, b}, {c}, {d}}

{ {a, b, c}, {d} }

{ {a, b, c, d} }

{ {a}, {b, c}, {d} }

{ {a}, {b, c, d} }

{ {a}, {b}, {c, d} }

{ {a, b}, {c, d} }

{ {a}, {b, c}, {d}}

the set is stored in arraylist

ArrayList<MyData>

I want result in

ArrayList<ArrayList<MyData>>

Any ideas? ]]>

Code :

private static long steps(final int chainLength)
{
int steps = 0;
final LinkedList<Integer> toDo = new LinkedList<Integer>();
toDo.add(0);
while (!toDo.isEmpty())
{
steps++;
final int index = toDo.removeFirst();
if (index < chainLength - 1)
{
for (int i = 0; i < 4; i++)
toDo.add(index + 1);
}
}
return steps;
}

How can I write this in a way that isn't pathetically slow? ]]>

thanx ]]>

so i'm making a game where i use tiles to make the "ground"

basically i have this image:

grass_tilee.png

"grass tile" which i then draw ten times to the screen

like so:

Code Java:

for(int x = 0; x < tile.getWidth()*10; x += tile.getWidth())
for(int y = 0; y < tile.getHeight()*10; y+= tile.getHeight())
g.drawImage(tile,x,y,null);

which draws this one the screen:

working_algo.jpg

and everything is working perfect.....but now i want to change the grass tile to this image:

grass_tile_2.png

and now it draws this on the screen:

failed_algo.jpg

basically its still working, it just need to draw the tiles over each other.....which im not really sure how to do....

i tried this:

Code Java:

for(int x = 0; x < tile.getWidth()*10; x += tile.getWidth()-21)
for(int y = 0; y < tile.getHeight()*10; y+= tile.getHeight()-12)
g.drawImage(tile,x,y,null);

but obviously it didnt work ^_^

sooo.....can someone help me with an algorithm that draws the new tiles correctly on the screen?

like so:

abc.png

I am new to the forum as this is my first post. I am trying to complete part of an assignment, and while having experience in c++, java is rather new to me. I have been trying to solve this problem, multiple times I have come close but can't seem to quite understand how to complete the task at hand.

Write a method, printFirstKMultiples(int n, int k), that prints out the first k integer multiples of n (starting with 0). For example, printFirstKMultiples(5, 7); should print:

0, 5, 10, 15, 20, 25, 30,

Now this would be simple using a loop, but I am required to complete this task using recursion.

Any help or hints will be much appreciated. ]]>