Please help with prime number.

closed account (18RGNwbp)
Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
    // Function A: Numbers divisible by 5, 6, 9, and 10
	int k, Hold;
	k = 0;
	while ( k < 20 )
	{
		Hold = rand();
		if ( Hold > 101 && Hold < 3000 && Hold % 5 == 0 && Hold % 6 == 0 && Hold % 9 == 0 && Hold % 10 == 0 )
		{
			cout << setw(4) << Hold << endl;
			k = k+1;

		}

	}   
}

However, can someone please help me with code on how to calculate prime numbers. There must be two different functions: One must be a list of 20 random, prime numbers greater than 100 and less than 2000. The other function must be a list od 20, random, prime numbers greater than 200 and less than 5000.
Please try to use code, similar to the one shown above.
with this code you can calculate prime numbers between the range that you want. However i can't do it randomly!
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
#include<iostream>
using std::cout;
using std::cin;
using std::endl;

#include<cmath>
using std::sqrt;

bool prime(int number); //function prototype

int main(){

int numberOfPrimes=0;
int firstNumber;
int secondNumber;

cout<<"Find primes numbers from: ";
cin>>firstNumber;
cout<<"To(greater than the first number please!): ";
cin>>secondNumber;
cout<<'\n';

for(int number=firstNumber;number<=secondNumber;number++){
        if(prime(number)==true){
                ++numberOfPrimes;
                cout<<"["<<number<<"] is prime"<<endl;
        }//end if
}//end loop for
cout<<"\nFound: "<<numberOfPrimes<<endl;

return 0; //indicates success
}//end main

bool prime(int number){
        bool isPrime=false;
        int divisibleNumbers=1;
        int limit=sqrt(number);
        int notPrime=0;
        for(int counter=1;counter<=limit;counter++){
                if(number%counter==0)
                        if(counter==1){
                                divisibleNumbers++;
                        }else{
                                notPrime++;
                        }//end if...else
        }//end loop for
        if((divisibleNumbers==2&&notPrime==0)&&number!=1){
                isPrime=true;
        }//end if
        return isPrime;
}//end function prime 


Find primes numbers from: 2
To(greater than the first number please!): 10

[2] is prime
[3] is prime
[5] is prime
[7] is prime

Found: 4
Last edited on
Topic archived. No new replies allowed.