Question : A fancy number is a number in the
sequence 1, 1, 5, 17, 61, … .Note
that first two fancy numbers are 1 and any fancy number other than the first two is sum
of thethree times previous one and two times the one before that.
See below:
1,
1,
3*1 +2*1 = 5
3*5 +2*1 = 17
3*17 + 2*5 = 61
Write a function named isFancy that returns 1 if its integer
argument is a Fancy number, otherwise it returns 0.
The signature of the function is
int
isFancy(int n)
Solution :
public static int
isFancy(int n)
{
int a = 1,
b = 1, c = 0;
if (n ==
1)
return 1;
while(c<n)
{
c = 3 * b + 2 * a;
a =b;
b =c;
}
if(c==n)
{
return 1;
}
else
{
return 0;
}


0 comments:
Post a Comment