4 Replies - 1296 Views - Last Post: 26 February 2011 - 05:46 PM Rate Topic: ***** 1 Votes

#1 LethalLima  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 25-February 11

Make a function that prints prime numbers that are not part of Fibonac

Posted 25 February 2011 - 07:11 AM

I don't seem to understand what the problem is... It makes sense to me, but it won't run right. Here's what I got...
First is the main menu requesting the selection function, then once the selection is chosen, it will ask for the lower and upper limits.
I already have functions option1 through option3 completed. Function four is supposed to be print prime numbers that are not part of Fibonacci. When I enter lower limit as "1" and upper limit as "20." It should print only numbers: 7, 11, 17, 19, but instead prints all prime numbers from 1 through 20. Not sure what I am doing wrong at all...

#include <iostream>
#include <conio.h>

using namespace std;
int print_nums();
bool isPrime();
int option1(int, int);
int option2(int, int);
int option3(int, int);
int option4(int, int);

void main()
{
	int selection, lower, upper, count;

	
	do
	{
		selection = print_nums();
		
		if(selection == 6)
			break;
		
		cout << "Enter lower and upper limits: ";
		cin >> lower >> upper;

		if(selection == 1)
			count = option1(lower, upper); 
		if(selection == 2)
			count = option2(lower, upper);
		if(selection == 3)
			count = option3(lower, upper);
		if(selection == 4)
			count = option4(lower, upper);

		cout << count << " numbers met the criteria." << endl;
			
			

	}while(selection < 6);

	cout << "\nThank you for using my program. -Jeff Lima.";
	_getch();
}

bool isPrime(int n)
{
	for(int i = 2; i < n / 2 + 1; i++)
		if(n % i == 0)
			return false;
	return true;
}


int print_nums()
{
	int selection;
	do
	{
		cout << "\nChoose one of the following by entering the corresponding number:"
			 << "\n1. Prime numbers."
			 << "\n2. Fibonacci numbers."
			 << "\n3. Prime numbers that are also part of the Fibonacci series."
			 << "\n4. Prime numbers that are not part of Fibonacci series."
			 << "\n5. Fibonacci numbers that are not prime numbers."
			 << "\n6. Quit."
			 << "\nEnter selection: ";
		cin >> selection;
	}while(selection < 1 || selection > 6);

	return selection;
}

int option1(int lower, int upper)
{
	int count = 0;

	for(int i = lower; i <= upper; i++)
	{
		if(isPrime(i))
		{
			cout << i << ", ";
			count++;
		}
	}
	
	cout << "\b\b." << endl;

	return count;
}

int option2(int lower, int upper)
{
	int count = 0, num1 = 0, num2 = 1, sum = num1 + num2;
   
	if(lower <= num1)
	{
	    cout << num1 << ", " << num2 << ", ";
		count = 2;
	}
	else if(lower <= num2)
	{
		cout << num2 << ", ";
		count = 1;
	}
	
	do
    {
		if(sum >= lower)
		{
			cout << sum << ", ";
			count++;
		}

		num1 = num2;
        num2 = sum;
        sum = num1 + num2;
   
    }while(sum <= upper);
	
	cout << "\b\b." << endl;

	return count;
}

int option3(int lower, int upper)
{
	int count = 0, num1 = 0, num2 = 1, sum = num1 + num2;

	if(lower <= num1 && isPrime(num1))
	{
	    cout << num1 << ", " << num2 << ", ";
		count = 2;
	}
	else if(lower <= num2 && isPrime(num2))
	{
		cout << num2 << ", ";
		count = 1;
	}
	
	do
    {
		if(sum >= lower && isPrime(sum))
		{
			cout << sum << ", ";
			count++;
		}

		num1 = num2;
        num2 = sum;
        sum = num1 + num2;
   
    }while(sum <= upper);

	cout << "\b\b." << endl;

	return count;
}

int option4(int lower, int upper)
{
	int count = 0, num1 = 0, num2 = 1, sum = num1 + num2;	
		

	
	for(int i = lower; i <= upper; i++)
	{
		if(isPrime(i))	
		{	
			do
			{
				
				if(sum != i)	
				{	
					cout << i << ", ";
					count++;
				}

				num1 = num2;
				num2 = sum;
				sum = num1 + num2;
			}while(sum <= upper);
		}
	}	
			
	cout << "\b\b." << endl;

	return count;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Make a function that prints prime numbers that are not part of Fibonac

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3848
  • View blog
  • Posts: 11,775
  • Joined: 25-December 09

Re: Make a function that prints prime numbers that are not part of Fibonac

Posted 25 February 2011 - 08:01 AM

Quote

Function four is supposed to be print prime numbers that are not part of Fibonacci.


Where in your option4() function do you check to see if the number is part of the Fibonacci sequence?


Jim
Was This Post Helpful? 0
  • +
  • -

#3 LethalLima  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 25-February 11

Re: Make a function that prints prime numbers that are not part of Fibonac

Posted 25 February 2011 - 05:01 PM

Hello,

In option4,

When the for loop begins it's trying to find every integer that is prime (isPrime(i)) and if it's true it will start a do loop which has the Fibonacci sequenced embedded in with an if statement that says if " i != sum " true then it will print out the prime number that is not a Fibonacci number " i. "

Jeff
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 3848
  • View blog
  • Posts: 11,775
  • Joined: 25-December 09

Re: Make a function that prints prime numbers that are not part of Fibonac

Posted 25 February 2011 - 10:06 PM

Since sum is always increasing your if statement if(sum != i) will execute for each prime, (plus it print several 1 the first time through the loop)

You will probably need to define a method that will detect if the current prime is a Fibonacci number. See this link and study the "Recognizing Fibonacci numbers" section. In this section it gives a mathematical equation to determine if a particular number is a Fibonacci number.

Quote

Alternatively, a positive integer z is a Fibonacci number if and only if one of 5z^2 + 4 or 5z^2 − 4 is a perfect square
.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 LethalLima  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 25-February 11

Re: Make a function that prints prime numbers that are not part of Fibonac

Posted 26 February 2011 - 05:46 PM

Hello,

I was able to make a function that checked to see if an integer was a Fibonacci number:

bool isFib(int upper, int n)
{
	int count = 0, num1 = 0, num2 = 1, sum = num1 + num2;
   
	if(n == num1)
		return false;
	
	if(n == num2)
		return false;
	
	do
    {
		if(n == sum)
			return false;
		num1 = num2;
        num2 = sum;
        sum = num1 + num2;
   
    }while(sum <= upper);
	
	return true;
}



And I adjusted the option4 function:


int option4(int lower, int upper)
{
	int count = 0;	
		
	for(int i = lower; i <= upper; i++)
	{
		if(isPrime(i))	
		{	

				if(isFib(upper,i))
				{
					cout << i << ", ";
					count++;
				}
		}
	}	
			
	cout << "\b\b." << endl;

	return count;
}



Everything runs well now. Thank you for the help.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1