这段代码在 100000 之前运行良好,但如果您输入 1000000,它就会开始出现错误C++ 0xC0000094: Integer division by zero
。我确信这与浮点有关。我尝试了 (/fp:precise)、(/fp:strict)、(/fp:except) 和 (/fp:except-) 的所有组合,但没有得到肯定的结果。
#include "stdafx.h"
#include "time.h"
#include "math.h"
#include "iostream"
#define unlikely(x)(x)
int main()
{
using namespace std;
begin:
int k;
cout<<"Please enter the nth prime you want: ";
cin>>k;
int cloc=clock();
int*p;p=new int [k];
int i,j,v,n=0;
for(p[0]=2,i=3;n<k-1;i+=2)
for(j=1;unlikely((v=p[j],pow(v,2)>i))?!(p[++n]=i):(i%v);++j);
cout <<"The "<<k<<"th prime is "<<p[n]<<"\nIt took me "<<clock()-cloc<<" milliseconds to find your prime.\n";
goto begin;
}