So I have this assignment in my computer algorithm class:

Write a recursive algorithm that, given a positive integer n >= 1, prints all

sequences of numbers

k >= 1 and 1 <= i1 < i2 <…< ik <= n.

For example: if n=3, then the output will be

1

1,2

1,2,3

1,3

2

2,3

3

I was trying to write the recursive code for this assignment in Java but I do not know how to approach this problem. I understand the basics of recursion but I have trouble writing recursive code by myself.

this is what I have right now:

```
public class question4
{
public static void main(String arg[]){
int x = 10;
printSequence(x);
}
public static int printSequence(int n){
if (n == 1){
System.out.println(n);
return n;
}
else{
int result = printSequence(n-1) + 1;
System.out.println(result);
return result;
}
}
}
```

It only prints 1,2,3,4,5,6,7,8,9,10

Please help me!

Thank you in advance!

### More Related Questions

- How to avoid StackOverflowError caused by the number of recursion calls in Java? I want to solve this problem: https://www.hackerrank.com/challenges/find-median ,i.e. find the median element in unsorted array. To do this I perform quickselect algorithm. My program […]
- all solutions to change making with dynamic programming I was reviewing my handouts for our algorithm class and I started to think about this question:
Given different types of coins with different values, find all coin configurations to add […]
- Trace a recursive coin change algorithm I am trying to trace this recursive algorithm of the coin change problem with M=10, c={5,3,1} and d=3. M is the value of money for which change is required, c is the different coin values […]
- how to print this horizontally? Hey guys if you run this code with the given input you will get a vertical ruler
i'm trying to get a horizontal ruler using the given recursive functions any idea how to get there or […]
- Determining the complexities given codes Given a snipplet of code, how will you determine the complexities in general. I find myself getting very confused with Big O questions. For example, a very simple question:
for (int i = […]
- Rewriting a recursive function without using recursion I'm rewriting some existing code in a setting where recursive calls are not easily implemented nor desired. (And in Fortran 77, if you must know.) I've thought about making a stack from […]
- Recursive function taking too long to run when value of "n" is large I am trying to get the value for a n-th series. such that f(n)=2014*f(n-1) + 69*f(n-2) for (n>2) and f(n)=1 for n<=2. I am using BigInteger as it is my requirement. While running […]
- What is the most efficient/elegant way to parse a flat table into a tree? Assume you have a flat table that stores an ordered tree hierarchy:
Id Name ParentId Order
1 'Node 1' 0 10
2 'Node 1.1' 1 10
3 'Node 2' […]
- Binary Tree implementation using a recursive approach Project: "Create an implementation of a binary tree using the recursive approach introduced in the chapter. In this approach, each node is a binary tree. Thus a binary tree contains a […]
- Why does method change the boolean array parameter? I have a class for finding a path through a basic maze. A maze is passed to path along with starting and ending positions. I use recursion to find a path between the points. The path is […]