C++阿特金的筛子返回了几种复合材料
C++ Sieve of Atkin Returning Several Composites
>我已经在C++年实现了我自己的阿特金筛子,它生成素数很好,直到大约 860,000,000。在那里和更高的地方,程序开始返回几个复合物,至少我认为是这样。我在程序内有一个变量,用于计算找到的素数的数量,并且在 ~860,000,000 处,计数比应有的多。我根据埃拉托色尼筛子的类似程序和几个互联网资源检查了我的计数。我是编程新手,所以这可能是一个愚蠢的错误。
无论如何,这里是:
#include <iostream>
#include <math.h>
#include <time.h>
int main(int argc, const char * argv[])
{
long double limit;
unsigned long long int term,term2,x,y,multiple,count=2;
printf("Limit: ");
scanf("%Lf",&limit);
int root=sqrt(limit);
int *numbers=(int*)calloc(limit+1, sizeof(int));
clock_t time;
//Starts Stopwatch
time=clock();
for (x=1; x<root; x++) {
for (y=1; y<root; y++) {
term2=4*x*x+y*y;
if ((term2<=limit) && (term2%12==1 || term2%12==5)){
numbers[term2]=!numbers[term2];
}
term2=3*x*x+y*y;
if ((term2<=limit) && (term2%12==7)) {
numbers[term2]=!numbers[term2];
}
term2=3*x*x-y*y;
if ((term2<=limit) && (x>y) && (term2%12==11)) {
numbers[term2]=!numbers[term2];
}
}
}
//Print 2,3
printf("2 3 ");
//Sieves Non-Primes That Managed to Get Through
for (term=5; term<=root; term++) {
if (numbers[term]==true) {
multiple=1;
while (term*term*multiple<limit){
numbers[term*term*multiple]=false;
multiple++;
}
}
}
time=clock()-time;
for (term=5; term<limit; term++) {
if (numbers[term]==true) {
printf("%llu ",term);
count++;
}
}
printf("nFound %llu Primes Between 1 & %Lf in %lu Nanosecondsn",count,limit,time);
return 0;
}
来自维基百科 ,
The following is pseudocode for a straightforward version of the algorithm:
// arbitrary search limit
limit ← 1000000
// initialize the sieve
for i in [5, limit]: is_prime(i) ← false
// put in candidate primes:
// integers which have an odd number of
// representations by certain quadratic forms
for (x, y) in [1, √limit] × [1, √limit]:
n ← 4x²+y²
if (n ≤ limit) and (n mod 12 = 1 or n mod 12 = 5):
is_prime(n) ← ¬is_prime(n)
n ← 3x²+y²
if (n ≤ limit) and (n mod 12 = 7):
is_prime(n) ← ¬is_prime(n)
n ← 3x²-y²
if (x > y) and (n ≤ limit) and (n mod 12 = 11):
is_prime(n) ← ¬is_prime(n)
// eliminate composites by sieving
for n in [5, √limit]:
if is_prime(n):
// n is prime, omit multiples of its square; this is
// sufficient because composites which managed to get
// on the list cannot be square-free
for k in {n², 2n², 3n², ..., limit}:
is_prime(k) ← false
print 2, 3
for n in [5, limit]:
if is_prime(n): print n
对于 [1, √限制] × [1, √限制] 中的 (x, y):是你的问题。
您使用了 :
for (x=1; x<root; x++)
for (y=1; y<root; y++)
而是使用 :
for (x=1; x<=root; x++)
for (y=1; y<=root; y++)
希望这有帮助!
相关文章:
- 在C++中,如何通过几种类型从元组中选择多个元素
- 如何在同一个模板功能上专门化几种类型?
- 在 C 中并行化嵌套循环的几种方法之间的差异,C++使用 OpenMP
- Qt - 在没有正则表达式的情况下,将 QString 拆分为几种类型的空格
- 从文件中读取未签名字符的几种不同方法
- C++阿特金的筛子返回了几种复合材料
- 最简单的方法来封装几个语句中的几种语句
- 一些应用程序是如何用几种编程语言制作的
- 在 C++Amp 中并行执行的几种算术运算
- 链表的几种实现 - C++
- 几种特定方法或一种通用方法
- Qt-拆分一个QString,使用几种类型的空白作为分隔符
- 将 void* 强制转换为仅在运行时已知的几种类型
- C ++中的几种分配内存方法有什么区别
- LNK2019错误-未找到外部符号.尝试了几种解决方案
- 声明空/默认构造函数的几种方法之间的差异
- 以几种组合替换
- 将几种类型的数据读入数组
- 用指针写条件的几种方式的区别
- 如何将std::string转换为构造函数中几种类型中的任何一种