c语言 统计素数之和

    科技2024-08-03  29

    c语言 统计素数之和

    #include<stdio.h> int main() { int m, n; printf("您想求哪两个数之间的素数之和,请依次输入它们:"); scanf_s("%d %d", &m, &n); int i; int cnt = 0; int sum = 0; if (m==1) { m = 2; } for ( i = m; i <= n; i++) { int isPrime = 1; int k; for ( k = 2; k <=i-1; k++) { if (i%k==0) { isPrime = 0; break; } } if (isPrime) { cnt++; sum += i; } } printf("%d %d\n", cnt, sum); return 0; }

    Processed: 0.010, SQL: 8