素数密度(循环用sqrt)

This commit is contained in:
James 2020-04-27 11:43:46 +08:00
parent b318babe9b
commit 1b9fc62755
9 changed files with 57 additions and 34 deletions

Binary file not shown.

View File

@ -0,0 +1,45 @@
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
bool pan[10000001] = { 0 };
int su[100001] = { 0 };
bool zhi[100001] = { 0 };
int main()
{
int begn, ends;
int now = 0;
cin >> begn >> ends;
if(begn<2){
begn=2;
}
//cout << begn << ";" << ends << endl;
int len = sqrt(ends);
for (int i = 2; i <= len; i++) {
//cout << i << "/";
if (zhi[i] == 0) {
su[++now] = i;
}
for (int j = 1; j <= now && i * su[j]<= len; j++) {
zhi[i * su[j]] = 1;
if (i % su[j] == 0) {
break;
}
}
}
for (int u = 1; u <= now; u++) {
int i = su[u];
//cout << i << " ";
for (int j = ends / i; i * j >= begn && j != 1; j--) {
pan[i * j - begn] = 1;
}
}
int ans = 0;
for (int i = 0; i <= ends - begn; i++) {
if (pan[i] == 0) {
ans++;
//cout<<begn+i<<" ";
}
}
cout << ans;
}

View File

@ -1,22 +0,0 @@
#include <iostream>
#include <algorithm>
using namespace std;
bool pan[1000001]={0};
int main()
{
int begn, ends;
cin >> begn >> ends;
for (int i = 2; i * i <= ends; i++) {
for(int j=ends/i;i*j>=begn&&j!=1;j--){
pan[i*j-begn]=1;
}
}
int ans=0;
for(int i=0;i<=ends-begn;i++){
if(pan[i]==0){
ans++;
//cout<<begn+i<<" ";
}
}
cout<<ans;
}

Binary file not shown.

View File

@ -1 +1 @@
2 11
2146483647 2147483647

View File

@ -1,18 +1,18 @@
## z:\Chao\src\3069_includesushu\test\in.txt
2020/04/24 周五 18:49:17.02
5
2020/04/27 周一 11:21:02.03
46603
-----------------------------------------------
Process exited after 160 ms with return value 0
Process exited after 180 ms with return value 0
## z:\Chao\src\3069_includesushu\test\in2.txt
2020/04/24 周五 18:49:17.02
69
2020/04/27 周一 11:21:02.03
664579
-----------------------------------------------
Process exited after 420 ms with return value 0
## z:\Chao\src\3069_includesushu\test\in3.txt
2020/04/27 周一 11:21:02.03
0
-----------------------------------------------
Process exited after 80 ms with return value 0
## z:\Chao\src\3069_includesushu\test\in3.txt
2020/04/24 周五 18:49:17.02
705
-----------------------------------------------
Process exited after 90 ms with return value 0