Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

problem 2 The Fibonacci sequence ofnumber is defined as: Fn n-2 n-1 ,n 1 In this

ID: 3820217 • Letter: P

Question

problem 2

The Fibonacci sequence ofnumber is defined as: Fn n-2 n-1 ,n 1 In this question we examine a property of this sequence a) Write a C function definition with header int fib (int a int n) which generates an array, a of the first n Fibonacci numbers. Hint: You do not have to write this recursively. You just have to generate each amay entry from the previous two entries) b) Two numbers are said to be coprime ifthey have no divisors in common. For example 16 and 81 are coprime numbers. Write a C function definition with header int coprime (int a, int b) which returns 1 (true) if a and b are coprime and 0 lse) otherwise. c A property of Fibonacci numbers is that any two consecutive numbers in the sequence are coprime. Write a C program to verify this property. Your program should read an input value N, generate an amay of the first N Fibonacci numbers, and then test them to verify that each pair of consecutive numbers is coprime. It should retum a message indicating "Success" if that is the case or "Fail" if there is a pair of consecutive numbers which is not coprime. Question 33 Prog Write a Matlab function definition that takes a parameter n and retums an vector consisting of the first m Fibonacci numbers. (See the previous question for the definition of Fibonacci numbers.)

Explanation / Answer

1.

int fib(int a[],int n)

{

   int i;

  /* 0th and 1st number of the series are 0 and 1*/

  f[0] = 0;

  f[1] = 1;

for (i = 2; i <= n; i++)

  {

      /* Add the previous 2 numbers in the series

         and store it */

      f[i] = f[i-1] + f[i-2];

  }

  return f[n];

}

2.Two no are coprime if there higest common factor is one

//Highest common factor
int hcf(int a, int h)
{
    int temp;
    while(1)
    {
        temp = a%h;
        if(temp==0)
        return h;
        a = h;
        h = temp;
    }


//Highest common factor
int hcf(int a, int h)
{
    int temp;
    while(1)
    {
        temp = a%h;
        if(temp==0)
        return h;
        a = h;
        h = temp;
    }
}

int coprime(int a,int b)
{
int gcd=hcf(a,b);

if(gcd==1)
    {
       return 1;
    }
    else
    {
        return 0;
    }
}

int fib(int n)
{
/* Declare an array to store Fibonacci numbers. */
int f[n+1];
int i,flag;

/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;
flag=coprime(f[0],[1]);
if(flag==0)
{
   printf("Fail ");
   exit(0);
}

for (i = 2; i <= n; i++)
{
      /* Add the previous 2 numbers in the series
         and store it */
      f[i] = f[i-1] + f[i-2];
      flag=coprime(f[i-1],[i]);
      if(flag==0)
      {
        printf("Fail ");
        exit(0);
      }
}
printf("Success ")
}
int main ()
{
int n = 9;
printf("%d", fib(n));
getchar();
return 0;
}

}

int coprime(int a,int b)
{
int gcd=hcf(a,b);

if(gcd==1)
    {
       return 1;
    }
    else
    {
        return 0;
    }
}

3.


//Highest common factor
int hcf(int a, int h)
{
    int temp;
    while(1)
    {
        temp = a%h;
        if(temp==0)
        return h;
        a = h;
        h = temp;
    }
}

int coprime(int a,int b)
{
int gcd=hcf(a,b);

if(gcd==1)
    {
       return 1;
    }
    else
    {
        return 0;
    }
}

int fib(int n)
{
/* Declare an array to store Fibonacci numbers. */
int f[n+1];
int i,flag;

/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;
flag=coprime(f[0],[1]);
if(flag==0)
{
   printf("Fail ");
   exit(0);
}

for (i = 2; i <= n; i++)
{
      /* Add the previous 2 numbers in the series
         and store it */
      f[i] = f[i-1] + f[i-2];
      flag=coprime(f[i-1],[i]);
      if(flag==0)
      {
        printf("Fail ");
        exit(0);
      }
}
printf("Success ")
}
int main ()
{
int n = 9;
printf("%d", fib(n));
getchar();
return 0;
}