Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
55
Task/Summarize-primes/C/summarize-primes.c
Normal file
55
Task/Summarize-primes/C/summarize-primes.c
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
bool is_prime(int n) {
|
||||
int i = 5;
|
||||
|
||||
if (n < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (n % 2 == 0) {
|
||||
return n == 2;
|
||||
}
|
||||
if (n % 3 == 0) {
|
||||
return n == 3;
|
||||
}
|
||||
|
||||
while (i * i <= n) {
|
||||
if (n % i == 0) {
|
||||
return false;
|
||||
}
|
||||
i += 2;
|
||||
|
||||
if (n % i == 0) {
|
||||
return false;
|
||||
}
|
||||
i += 4;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
const int start = 1;
|
||||
const int stop = 1000;
|
||||
|
||||
int sum = 0;
|
||||
int count = 0;
|
||||
int sc = 0;
|
||||
int p;
|
||||
|
||||
for (p = start; p < stop; p++) {
|
||||
if (is_prime(p)) {
|
||||
count++;
|
||||
sum += p;
|
||||
if (is_prime(sum)) {
|
||||
printf("The sum of %3d primes in [2, %3d] is %5d which is also prime\n", count, p, sum);
|
||||
sc++;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("There are %d summerized primes in [%d, %d)\n", sc, start, stop);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue