herrDeng網內搜尋

自訂搜尋

Ads

2017年6月13日 星期二

C++利用篩法算到10^9有幾個質數

C++利用篩法算到10^9有幾個質數

26 則留言:

B10333086 陳晏堂 提到...

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <cstring>
#include <ctime>
#include <vector> //
using namespace std;

void Eratosthenes_sieve(vector<bool>& isPrime, int n)//
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector<bool>& isPrime, int n, vector<int>& prime)//
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
count++;
// prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector<int> prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector<bool> isPrime( n+1,1);

Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");

B10333064王韻菱 提到...

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <cstring>
#include <ctime>
#include <vector>
using namespace std;

void Eratosthenes_sieve(vector<bool>& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector<bool>& isPrime, int n, vector<int>& prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector<int> prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector<bool> isPrime( n+1,1);

Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

B10333106 劉郁芃 提到...

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <cstring>
#include <ctime>
#include <vector>>
using namespace std;

void Eratosthenes_sieve(vector<bool>& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector<bool>& isPrime, int n, vector<int>& prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector<int> prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector<bool> isPrime(n+1,1);

Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

B10333107-曾詠浩 提到...

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector& isPrime, int n, vector prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime(n + 1,1);
cout<<isPrime.max_size()<<endl;
//memset(isPrime, 1, n + 1);
Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

B10333097胡聖恩 提到...

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <cstring>
#include <ctime>
using namespace std;

void Eratosthenes_sieve(vector<bool>& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector<bool>& isPrime, int n, vector<int> prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector<int> prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector<bool> isPrime(n + 1,1);
cout<<isPrime.max_size()<<endl;

Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

b10333080王育文 提到...

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <cstring>
#include <ctime>
using namespace std;

void Eratosthenes_sieve(vector<bool>& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector<bool> & isPrime, int n, vector<int> prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector<int> prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector<bool> isPrime(n + 1,1);
cout<<isPrime.max_size()<<endl;


Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

B10333094 提到...

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector& isPrime, int n, vector &prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime(n+1,1);


Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

B10333055 翁恩義 提到...

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <vector>
using namespace std;

void Eratosthenes_sieve(vector<bool>& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector<bool>& isPrime, int n, vector<int> prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector<int> prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector<bool> isPrime(n+1, 1);
cout<<isPrime.max_size()<<endl;

Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

B10333079 葉禮魁 提到...

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <cstring>
#include <ctime>
using namespace std;

void Eratosthenes_sieve(vector<bool> & isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector<bool> & isPrime, int n, vector<int> prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector<int> prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector<bool> isPrime(n + 1,1);
cout<<isPrime.max_size()<<endl;
// memset(isPrime, 1, n + 1);
Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

B10333053 梁竣翔 提到...

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector& isPrime, int n, vector& prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime( n+1,1);

Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

b10233032 謝翔宇 提到...

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector& isPrime, int n, vector prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime(n+1, 1);
cout<<isPrime.max_size()<<endl;

Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

B10333072 黃玟茜 提到...

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector& isPrime, int n, vector prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime(n + 1,1);
cout<

b10133187 楊鈞文 提到...

#include
#include
#include
#include
#include
#include
#include
#include
#include //
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)//
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector& isPrime, int n, vector& prime)//
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
count++;
// prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime( n+1,1);

Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");

b10133187 楊鈞文 提到...

#include
#include
#include
#include
#include
#include
#include
#include
#include //
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)//
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector& isPrime, int n, vector& prime)//
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
count++;
// prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime( n+1,1);

Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");

B10333072 黃玟茜 提到...

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector& isPrime, int n, vector prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime(n + 1,1);
cout<

b10133187 楊鈞文 提到...

#include
#include
#include
#include
#include
#include
#include
#include
#include //
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)//
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector& isPrime, int n, vector& prime)//
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
count++;
// prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime( n+1,1);

Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");

b10233032 謝翔宇 提到...

#include
#include
#include
#include
#include
#include
#include
#include
#include //
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)//
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector& isPrime, int n, vector& prime)//
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
count++;
// prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime( n+1,1);

Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");

B10333027 毛冠霖 提到...

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector isPrime, int n, vector prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
// prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime (n+1, 1) ;
Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

b10333083周琬芸 提到...

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <cstring>
#include <ctime>
#include<vector>
using namespace std;

void Eratosthenes_sieve(vector<bool>& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector<bool>& isPrime, int n, vector<int> &prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
count ++;
// prime[count++]=i;
// ff << i << endl; //
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector<int> prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;

vector<bool> isPrime( n+1,1);
Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

b10433038詹恭煒 提到...

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector isPrime, int n, vector prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
// prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime (n+1, 1) ;
Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

b10433053謝肇安 提到...

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector isPrime, int n, vector prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
// prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime (n+1, 1) ;
Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

b10433193林佩縈 提到...

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector isPrime, int n, vector prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
// prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime (n+1, 1) ;
Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

b10433194謝旻君 提到...

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector isPrime, int n, vector prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
// prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime (n+1, 1) ;
Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

B10333063羅胤銓 提到...

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <cstring>
#include <ctime>
using namespace std;

void Eratosthenes_sieve(vector<bool>& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector<bool> isPrime, int n, vector<int> prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
count++;
// prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e8;
cout << n << endl;
vector<int> prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector<bool> isPrime( n+1,1);
cout<<isPrime.max_size()<<endl;
Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

B10333005 陳於農 提到...

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector& isPrime, int n, vector prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime(n + 1,1);
cout<<isPrime.max_size()<<endl;
//memset(isPrime, 1, n + 1);
Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

B10333070 徐承瑋 提到...

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void Eratosthenes_sieve(vector& isPrime, int n)
{
isPrime[1] = 0;
int n_sqrt = (int)(sqrt(n));
for (int i = 2; i <= n_sqrt; i++)
{
if (isPrime[i])
for (int j = i*i; j <= n; j += i)
isPrime[j] = 0;
}
}

void listPrime(vector& isPrime, int n, vector prime)
{
fstream ff("prime.csv", ios::out);
int count = 0;
for (int i = 1; i <= n; i++)
if (isPrime[i])
{
prime[count++]=i;
// ff << i << endl;
}
ff << "Prime_Pi=# of primes=" << count << endl;
cout<< "Prime_Pi=# of primes=" << count << endl;
}

int main()
{
cout << "Find the primes below the integer n:";
int n = (int)1e9;
cout << n << endl;
vector prime((int)(1.125*n / log(n)+1.5));//Use Chebyshev's prime theorem
int s = clock(), e;
vector isPrime(n + 1,1);
cout<<isPrime.max_size()<<endl;
//memset(isPrime, 1, n + 1);
Eratosthenes_sieve(isPrime, n);
e = clock() - s;
cout <<(double) e /CLOCKS_PER_SEC<< " sec\n";
listPrime(isPrime, n, prime);
system("Pause");
return 0;
}

Related Posts Plugin for WordPress, Blogger...

熱門文章