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

Consider the following three types of numbers: Prime numbers: a number is prime

ID: 3794905 • Letter: C

Question

Consider the following three types of numbers: Prime numbers: a number is prime if x|p x = p or x = 1. Fibonacci numbers: the n^th Fibonacci number is F_n = F_n_1 + Fn_2, where F_0 = 0 and F_1 = 1. Cullen numbers (modified): C_n = n middot 2^[n/2] + 1. (Note the floor function in the modified Cullen numbers). Write a program that reads a single number from the keyboard. Then display whether that number is any two of the listed types above. Please cout in the same order shown in example 1. Example 1 (user input is underlined): Give me a number, HUMAN: 2 that is prime and fibonacci that is prime and cullen that is cullen and fibonacci Example 2 (user input is underlined): Give me a number, HUMAN: 3 that is prime and fibonacci Give me a number, HUMAN: 7 that is prime and cullen

Explanation / Answer

Please run it in Dev C++

#include<iostream>
using namespace std;
#include<math.h>

bool isprime(int x)
{
   int i,flag=0;
   for(i=2; i<=x/2; ++i)
{
// condition for nonprime number
if(x%i==0)
{
flag=1;
break;
}
}
if(flag==0) return 1;
else return 0;
}

int getnthfibnumber(int n)
{
   if(n<0) return -1;
   if(n==0) return 0;
   else if(n==1) return 1;
   else
   {
   return getnthfibnumber(n-1) + getnthfibnumber(n-2);
}
}

int getcullen(int n)
{
   int temp;
   temp=n*pow(2,floor(n/2))+1;
   return temp;
}

bool iscullen(int x)
{
   int i=0;
   for(i=0;getcullen(i)<=x;i++)
   {
       if(x==getcullen(i))
       return 1;
   }
   return 0;
}

bool isfib(int x)
{
   int i=0;
   for(i=0;getnthfibnumber(i)<=x;i++)
   {
       if(x==getnthfibnumber(i))
       return 1;
   }
   return 0;
}

int main()
{
   int x;
   cout<<"Enter a number: ";
   cin>>x;
   if(isprime(x) && isfib(x))
   {
       cout<<" Number is prime and fib. ";
   }
   if(isprime(x) && iscullen(x))
   {
       cout<<" Number is prime and cullen. ";
   }
   if(iscullen(x) && isfib(x))
   {
       cout<<" Number is prime and fib. ";
   }
   return 0;
}