counting primes between 1 to x
Apr 6, 2018 at 9:43pm UTC
Trying to get pi(x)=number of primes from 1 to x but i am totally lost after getting up to 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 <cmath>
using namespace std;
bool isPrime;
typedef unsigned long long ull;
ull pi(ull x){
// pi(x)=number of prime between 1 to x
}
bool prime(int x){
isPrime=true ;
}
return isPrime;
}
int main(){
int x;
cout<<"Input a number: " ;
cin>>x;
}
}
Last edited on Apr 6, 2018 at 10:46pm UTC
Apr 6, 2018 at 9:46pm UTC
Move most of your logic in main to pi. Have a counter (ex:
int counter = 0;
) and every time prime(d) is true, increment that counter. Then, return the count after the for loop.
In main, having something like:
1 2 3 4 5 6 7 8
int main()
{
ull x;
cout << "Input a number: " ;
cin >> x;
ull n = pi(x);
cout << n << endl;
}
Last edited on Apr 6, 2018 at 9:49pm UTC
Apr 6, 2018 at 10:06pm UTC
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
#include <iostream>
//#include <cmath>
using namespace std;
// bool isPrime;
typedef unsigned long long ull;
bool isPrime(int x) {
if (x == 4) {
return false ;
}
for (int i = 2; i < x / 2; ++i) {
if (x % i == 0) {
return false ;
break ;
}
}
return true ;
}
ull primeNumbers(int x) {
ull primeCount = 0;
for (int d = 2; d <= x; ++d) {
if (isPrime(d)) {
primeCount++;
}
}
return primeCount;
}
int main() {
int limit;
cout << "Input a number: " ;
cin >> limit;
ull primeCount = primeNumbers(limit);
cout << "Number of prime numbers between 1 and "
<< limit << " : " << primeCount << endl;
}
NB : Think of how
x
can be validated so you get a valid
integer
before passing into your
primeNumbers
Last edited on Apr 6, 2018 at 10:07pm UTC
Apr 6, 2018 at 10:43pm UTC
I changed it to something like that but the output was always 0, now i know what i did wrong ty.
Apr 6, 2018 at 11:05pm UTC
We cannot point/explain your errors unless you don't post your latest version.
PS. Do not erase content from your earlier posts.
Apr 7, 2018 at 12:28am UTC
sorry removed it because prof doesn't want classmates copying each others code.
but the first code that i posted before basically printed the prime from 1 to x, i took that code and changed it to something like this which wasn't working properly but i fixed it.
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
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime;
typedef unsigned long long ull;
bool prime(int x){
isPrime=true ;
if (x==4){
isPrime=false ;
}
for (int i=2; i<x/2; ++i){
if (x%i==0){
isPrime=false ;
break ;
}
}
return isPrime;
}
ull pi(ull x) {
ull count=0;
for (int d=2; d<=x; ++d){
if (isPrime==true ) {
count=1;
cout<<count;
}
}
}
int main(){
int x;
cout<<"Input a number: " ;
cin>>x;
cout<<pi(x);
}
the code was something like this before i fixed it, i had some more stuff on it but don't remember.
Last edited on Apr 7, 2018 at 12:43am UTC
Apr 7, 2018 at 2:49pm UTC
You should probably increment the count, not just assign 1 to it. (count++ instead of count=1).
Topic archived. No new replies allowed.