问题描述
我有下面的代码并且有注释。它基本上基于“埃拉托色尼筛法”。我正在修改它以在代码的最后一个 for 循环中打印和计算剩余的素数。但是,输出是“1There are 1 primes”。
#include <stdio.h>
#define SIZE 50000
int main(void) {
int i,mult,count,count2;
int flag[SIZE + 1];
count = 0;
count2 = 0;
//sets up all numbers
for (i = 1; i <= SIZE; i++) {
flag[i] = 1;
//printf("%d",flag[i]);
}
//step 1: selects the prime number
for (i = 2; i <= SIZE; ++i) {
if (flag[i] == 1)
++count;
mult = i;
//step 2: filters out numbers multiple of that prime number in step 1
while (mult <= SIZE) {
flag[mult] = 0;
mult = mult + i;
}
}
//Now that the non-prime numbers have been filtered,this then prints the number and counts the prime numbers
for (i = 1; i <= SIZE; i++) {
if (flag[i] == 1) {
++count2;
printf("%d",i);
}
}
printf("There are %d primes. \n",count2);
return 0;
}
解决方法
在您的第二个 for 循环中,您开始:
var result = db.HRMS_EmpMst_TR
.Where(empMst => empMst.Name == ...) // or use some other filter,just an example
.AsEnumerable()
.Select(empMst => new
{
Id = empMst.Id,Name = empMst.Name,EmpCode = empMst.GetEmpCode(),...
});
然后在您设置后的 while 中:
mult = i;
本质上你说这个数不是质数。
如果将 mult = i 行替换为:
flag[mult] = 0;
那么你的程序就可以运行了。