Consider the following three types of numbers: 2)Fibonacci numbers: the n th Fib
ID: 3794659 • Letter: C
Question
Consider the following three types of numbers:
2)Fibonacci numbers: the nth Fibonacci number is Fn = Fn-1 + Fn-2 , where Fo =0 and F1 =1
Write a program that reads a single number from the keyboard. Then display whether that number is any two of the listed types above.
Example 1:
Give me a number, HUMAN:
2
that is prime and fibonacci
that is prime and cullen
that is cullen and fibonacci
Example 2:
Give me a number, HUMAN:
3
that is prime and fibonacci
Example 3
Give me a number, HUMAN:
7
that is prime and cullen
Example 4
Give me a number, HUMAN:
21
that is cullen and fibonacci
Explanation / Answer
// C++ code
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
bool isPrime(int number)
{
bool result = true;
for(int i = 2; i <= number / 2; ++i)
{
if(number % i == 0)
{
result = false;
break;
}
}
return result;
}
bool checksquare(int number)
{
int value = sqrt(number);
return (value*value == number);
}
bool isFibonacci(int number)
{
return checksquare(5*number*number + 4) || checksquare(5*number*number - 4);
}
bool isCullen(int number)
{
int result = 0;
int n = 0;
while(true)
{
n++;
result = n*pow(2,n/2) + 1;
if(result == number)
{
return true;
}
if(result > number)
{
return false;
}
}
}
int main()
{
int number;
cout << "Give a number, HUMAN: ";
cin >> number;
if(isPrime(number) == true && isFibonacci(number) == true && isCullen(number) == true)
{
cout << "that is prime and fibonacci that is prime and cullen that is cullen and fibonacci ";
}
else if(isPrime(number) == true && isFibonacci(number))
{
cout << "that is prime and fibonacci ";
}
else if(isFibonacci(number) == true && isCullen(number) == true)
{
cout << "that is cullen and fibonacci ";
}
else if(isPrime(number) == true && isCullen(number) == true)
{
cout << "that is prime and cullen ";
}
else if(isPrime(number) == true)
{
cout << "that is prime only ";
}
else if(isFibonacci(number) == true)
{
cout << "that is fibonacci only ";
}
else if(isCullen(number) == true)
{
cout << "that is cullen only ";
}
return 0;
}
/*
output:
Give a number, HUMAN: 21
that is cullen and fibonacci
Give a number, HUMAN: 7
that is prime and cullen
Give a number, HUMAN: 3
that is prime and fibonacci
Give a number, HUMAN: 2
that is prime and fibonacci
that is prime and cullen
that is cullen and fibonacci
*/