Skill Subsist Impulse Ltd Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
Here's a complete Java answer:
public class BasicRecursion
{
public static void main(String[] args)
{
System.out.println("Basic Recursion");
System.out.println("Write a recursive function to compute f(n)=n⁄2 when n is even; f(n)=f(3n+1)when n is odd.");
System.out.println("---");
java.util.Random r = new java.util.Random();
int input = r.nextInt(1000);
System.out.println("Input : " + input);
int answer = compute(input);
System.out.println("Answer : " + answer);
}
public static int compute(int n)
{
if(n % 2 == 0)
{
return n/2;
}
return compute(3*n + 1);
}
}
This is the famous Collatz sequence : en.wikipedia.org/wiki/Collatz_conjecture .
- NoOne October 10, 2016There are many papers on it.