Saturday, 23 December 2017

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;
            }
          
        }   

1 comment:

  1. static int numFantasie(int nbr){
    boolean isFantasie=false;
    int isFan=0;
    int a=1,b=1,c=0;
    if(nbr==1)isFan=1;
    while(c<nbr){
    c=3*b+2*a;
    a=b;
    b=c;
    if(c==nbr){
    isFantasie=true;
    break;
    }
    }if(isFantasie)
    isFan=1;
    return isFan;
    }

    ReplyDelete

Powered by Blogger.

Followers

Translate

Currency Converter

Exchange Rate

Featured post

Interpolation in angular 5

When we want to communicate from component class to template ,we can use interpolation which use the { { expression }} to render the bound...

Popular Posts

My Facebook Page