Fibonacci #s and Prime #s Help please!

Hi im having problem with program it is not working correctly. Take a look im fine with choice 1 and 2 but currently stuck on choice 3 & 4.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <ctime>
using namespace std;
int main ()
{
	int num;
	int add=0;
	int add2=1;
	int sum=0;
	int choice;
	bool prime;
	int randnum;
	int count=0;
	int pcount=0;
	
	cout<<"Enter an integer between 1 and 1000: ";
	cin>>num;
	
	srand(unsigned(time(0)));
	
//do{
	cout<<"Choose one of the following:"
		<<"\n1. Display all Fibonacci numbers up to "<<num
		<<"\n2. Display all prime numbers up to "<<num
		<<"\n3. Display all Fibonacci numbers that are also prime numbers up to "<<num
		<<"\n4. Display 5 randomly generated prime numbers between 1 and 1000." 
		<<"\n5. Quit."
		<<"\n\nEnter your choice:";
	cin>>choice;

	if(choice==1)
	{
		cout<<"1";
		for(int i=1; sum<=num;i++)
		{
			sum=add+add2;
			if(sum<num)
			{
			cout<<","<<sum;
			add=add2;
			add2=sum;
			}
		}
	cout<<endl;
	} 
	if(choice==2)
	{
		
		for( int j=1; j<=num; j++)
		{
			prime=true;

			for(int div=2; div<=j/2 ;div++)
			{	
				if(j%div==0)
				{
					prime=false;
					break;
				}
				
			}
			if(prime==true)
					cout<<j<<",";
		}
		cout<<endl;
	}
	if(choice==3)
	{
		prime=true;
		for(int i=1; sum<=num;i++)
		{
			sum=add+add2;
			if(sum<num)
			{
				add=add2;
				add2=sum;
			
			
			for(int sumdiv=2; sumdiv<=sum/2 ;sumdiv++)
				{	
					if(sum%sumdiv==0)
					{
						prime=false;
						break;
					}
					
				}
			if(prime==true)
				cout<<sum<<",";
			}
		}
	}

	if(choice==4)
	{
		
		for(int z=1;count<=5;z++)
			
		{	
			randnum= 1+(rand()%1000);
			pcount++;\
			for(int div=2; div<=randnum/2 ;div++)
			{	
				randnum= 1+(rand()%1000);
				pcount++;
				if(randnum%div==0)
				{
					prime=false;
					break;
				}
				
			}
				if(prime==true)
				{	
					count++;
					cout<<randnum<<",";
				}
		}
	}
	if(choice==5)
	{
		system("pause");
		exit(0);
	}
//}while(1);
		
system("pause");
}

Last edited on
Basically choice 3 i need to get only the prime #s from the fibonacci numbers and also for choice 4 is to generate 5 random numbers that are prime numbers only and also to get the total count of how many times it had to generate to get 5 prime #s.


If you guys can help or give me hints it would be great.Thank you.
Hi,

First I personnaly would suggest to use a function "bool isPrime(int n)" to avoid code redundancy, I think using a "switch(choice)" would also be preferrable to the many "if" statements and make the code more readable.

Your choice 3 doesn't work because the reinitialisation of the boolean prime is not done for every prime number you want to test; thus the use of a primality check function would clarify this.

Concerning you choice 5, I guess it would be easier to compute all prime numbers, store them in a vector, then pick 5 random indices in [0;sizeof(vector)]; Or at least store previous picked number in a Set to avoid testing them many times.

I hope my explanations are clear enough
Topic archived. No new replies allowed.