find_sum(3, 3); function find_sum($x, $n) { find_sum_recurse($x, $x, $n, array()); } function find_sum_recurse($x, $z, $n, $list) { if ($n == 0) { if ($z == 0) { var_dump($list); } } else { for ($i = 0; $i <= $z; $i++) { array_push($list, $i); find_sum_recurse($x, $z - $i, $n - 1, $list); array_pop($list); } } }
def upRight(node, depth, numlist): if(node[0] < 0): return #endif numlist.append(node) newnode = (node[0] -1, node[1] + 1, node[2]) upRight(newnode, numlist) #enddef def right(node, numlist): if(node[0] < 0): return #endif upright(node, numlist) right(node,numlist) #enddef def getAllNumbers(startnum): numlist = list() firstnode = tuple(startnum, 0, 0) right(firstnode, numlist)
import java.util.Stack; class Algorithm { public static void main(String[] args) { Algorithm a = new Algorithm(); a.findSum(3, 3); } public Algorithm() { } public void findSum(int x, int n) { findSumRecurse(x, n, new Stack()); } private void findSumRecurse(int x, int n, Stack stack) { if (n == 0) { if (x == 0) { System.out.println(stack); } } else { for (int i = 0; i <= x; i++) { stack.push(i); findSumRecurse(x - i, n - 1, stack); stack.pop(); } } } }
roughan wrote: » no i cat follow the syntax of the examples as im not familiar with them
function stepY(x, y, z): if x < 0: return endif visit(x, y, z) stepY(x-1, y+1, z) endfun function stepZ(x, y, z): if x < 0: return endif # We don't visit this node because stepY will do it for you stepY(x, y, z) endfun