CalmoSoft primes: Difference between revisions

Content added Content deleted
(Added Go)
(Added C)
Line 6: Line 6:
Find and show here the longest sequence of CalmoSoft primes.
Find and show here the longest sequence of CalmoSoft primes.
<br><br>
<br><br>
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdbool.h>
#include <string.h>

bool isPrime(int n) {
if (n < 2) return false;
if (n%2 == 0) return n == 2;
if (n%3 == 0) return n == 3;
int d = 5;
while (d*d <= n) {
if (n%d == 0) return false;
d += 2;
if (n%d == 0) return false;
d += 4;
}
return true;
}

int main() {
int primes[30] = {2}, sIndices[5], eIndices[5], sums[5];
int i, j, k, temp, sum, si, ei, pc = 1, longest = 0, count = 0;
for (i = 3; i < 100; i += 2) {
if (isPrime(i)) primes[pc++] = i;
}
for (i = 0; i < pc; ++i) {
for (j = pc-1; j >= i; --j) {
temp = j - i + 1;
if (temp < longest) break;
sum = 0;
for (k = i; k <= j; ++k) sum += primes[k];
if (isPrime(sum)) {
if (temp > longest) {
longest = temp;
count = 0;
}
sIndices[count] = i;
eIndices[count] = j;
sums[count] = sum;
++count;
break;
}
}
}
printf("The longest sequence(s) of CalmoSoft primes having a length of %d is/are:\n\n", longest);
for (i = 0; i < count; ++i) {
si = sIndices[i];
ei = eIndices[i];
sum = sums[i];
for (j = si; j <= ei; ++j) printf("%d + ", primes[j]);
printf("\b\b= %d which is prime\n", sum);
if (i < count - 1) printf("\n");
}
return 0;
}</syntaxhighlight>

{{out}}
<pre>
The longest sequence(s) of CalmoSoft primes having a length of 21 is/are:

7 + 11 + 13 + 17 + 19 + 23 + 29 + 31 + 37 + 41 + 43 + 47 + 53 + 59 + 61 + 67 + 71 + 73 + 79 + 83 + 89 = 953 which is prime
</pre>

=={{header|Go}}==
=={{header|Go}}==
{{trans|Wren}}
{{trans|Wren}}