Code:
using System;
public class Test
{
public static void Main()
{
int n = 4;
Console.WriteLine("Input: " + n);
Console.WriteLine("Output:");
new Test().PowerOf2(n);
}
public int PowerOf2(int n)
{
if (n <= 1)
{
Console.WriteLine(1);
return 1;
}
else
{
int value = 2 * PowerOf2(n/2);
Console.WriteLine(value);
return value;
}
}
}
Output:
using System;
public class Test
{
public static void Main()
{
int n = 4;
Console.WriteLine("Input: " + n);
Console.WriteLine("Output:");
new Test().PowerOf2(n);
}
public int PowerOf2(int n)
{
if (n <= 1)
{
Console.WriteLine(1);
return 1;
}
else
{
int value = 2 * PowerOf2(n/2);
Console.WriteLine(value);
return value;
}
}
}
Output:
Input: 7
Output:
1
2
4
Run Time: O(log n)
No comments:
Post a Comment