0

I am working on a lab for a class where a user inputs a number and it recursively prints out a number pattern. For example,

The base case is if they enter 1, it will print: 1

If they enter 2 it will print: 1 2 1

If 3, it will print: 1 2 1 3 1 2 1

and then for something bigger, if they enter 7, it will print:

1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1

I'm a little stuck on what the number pattern is to be able to complete this problem. Does anyone have any ideas?

1
  • 3
    If it isn't defined in the homework, it becomes a sort of guessing game (instead of a programming problem). It certainly looks like you can join the number with the results of the previous number to the left and right, but who knows... Commented Dec 6, 2014 at 21:56

2 Answers 2

1

So you need to write a recursive function. Something of this form:

private String pattern(int num) {
    // ...
}

The most important part is finding the right exit condition that should stop the recursion. In this case, that's when num == 1.

From the description, it looks like for a number k, the output is pattern(k - 1) + k + pattern(k - 1).

I already spoiled too much. You might need to improve the efficiency of this. For example, realize that you don't need to run pattern(k - 1) twice, it's enough to do it once.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm a little stuck on what the number pattern is to be able to complete this problem.

Lets try to analyse the sequence using some function f
f(1) = 1 (Total digits = 1)
f(2) = 1 2 1 ( Total digits = 3)
f(3) = 121 3 121 (Total digits = 7)
f(4) = 1213121 4 1213121 (Total digits = 15)
f(5) = 121312141213121 5 121312141213121 (Total digits = 31)

So as you can observe total digits sequence looks like 1,3,7,15,31,....2^n-1
Now we can express this logic as mentioned below(Note : in order to help you to better understand how the program works i am printing sequence at every level)

public class SequenceGenerator {
    public static void main(String[] args) {
        generate(7);
    }

    static void generate(int depth) {
        recursiveGenerator(1, null, depth);
    }

    static void recursiveGenerator(int num, String prev, int limit) {
        if (num <= limit) {
            if (prev != null) {
                System.out.println();
            }
            if (prev != null) {
                System.out.printf("%s %d %s", prev, num, prev);

            } else {
                prev = "";
                System.out.printf("%d", num);
            }
            if (prev.equals("")) {
                prev += num + prev;
            } else {
                prev += " " + num + " " + prev;
            }
            recursiveGenerator(++num, prev, limit);
        }
    }
}

Outputs

1
1 2 1
1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.