Showing posts with label Recursion. Show all posts
Showing posts with label Recursion. Show all posts

Wednesday, April 8, 2009

Puzzle 13 - Solution

class Pattern{

void printPattern(int number){
//The trival case
if (number == 1){
System.out.print(number);
return;
}

printPattern(number
-1);
System.out.print(number);
printPattern(number
-1);
}

public static void main(String args[]){
int number = Integer.parseInt(args[0]);
Pattern pattern
= new Pattern();
pattern.printPattern(number);
}
}


Sorry no correct solutions this week!

Sunday, April 5, 2009

Puzzle 13 – Recursion

Language – Java | Type – Problem (Code) | Last date 08-Apr-2009 9:00 p.m. IST | Points 5

My little brother (a charming character usually) came to me the other day and said, “Don’t you think this pattern is amazing. I bet you computer folks could never make something like this”. “Ha” I scoffed “that’s simple recursion”. Simple recursion – was I out of my mind – when did recursion become simple? So here I am folks stuck on another problem. Any help?

When n = 1; output 1
When n = 2; output 121
When n = 3; output 1213121
When n = 4; output 121312141213121
Program must work for all positive integers ‘n’

Got an answer? Why don’t you leave it here.