#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int *primes, tblSize = 10, idx = 0, n = 2, maxFrom, maxStep, maxLength = 0;
void makePrimes();
void makePrimes()
{
int i, j;
for (i = primes[idx - 1] + 2; i < INT_MAX / 10; i += 2) {
for ( j = 0; (i % primes[j]) && (i > primes[j]*primes[j]); ++j);
if (i % primes[j])
primes[idx++] = i;
if (idx == tblSize) {
tblSize *= 2;
primes
= (int *)realloc(primes
, sizeof(int) * tblSize
); if (primes == NULL) {
printf("prime table size expanded error\n"); } else {
printf("prime table size expanded to %d\n", tblSize
); }
}
}
}
int seqLen(int i, int j)
{
int len = 2;
int step = primes[j] - primes[i];
int n = primes[j] + step;
while (1) {
while (n > primes[j])j++;
if (n == primes[j]) {
len++;
n += step;
} else break;
}
return len;
}
int main(void)
{
int i, j, t;
primes
= (int *)malloc(sizeof(int) * tblSize
); primes[idx++] = 2;
primes[idx++] = 3;
makePrimes();
printf("number of primes = %d\n", idx
); for ( i = 0; i < idx; ++i) {
for ( j = i + 1; j < idx; ++j ) {
t = seqLen(i, j);
// printf("i j = %d %d %d\n", i,j,t);
if (maxLength < t) {
maxFrom = primes[i];
maxStep = primes[j] - primes[i];
maxLength = t;
printf("from step length = %d %d %d\n", maxFrom
, maxStep
, maxLength
); }
}
}
// printf("from step length = %d %d %d\n", maxFrom, maxStep, maxLength);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPGxpbWl0cy5oPgoKaW50ICpwcmltZXMsIHRibFNpemUgPSAxMCwgaWR4ID0gMCwgbiA9IDIsIG1heEZyb20sIG1heFN0ZXAsIG1heExlbmd0aCA9IDA7Cgp2b2lkIG1ha2VQcmltZXMoKTsKCnZvaWQgbWFrZVByaW1lcygpCnsKICBpbnQgaSwgajsKICBmb3IgKGkgPSBwcmltZXNbaWR4IC0gMV0gKyAyOyBpIDwgSU5UX01BWCAvIDEwOyBpICs9IDIpIHsKICAgIGZvciAoIGogPSAwOyAoaSAlIHByaW1lc1tqXSkgJiYgKGkgPiBwcmltZXNbal0qcHJpbWVzW2pdKTsgKytqKTsKICAgIGlmIChpICUgcHJpbWVzW2pdKQogICAgICBwcmltZXNbaWR4KytdID0gaTsKICAgIGlmIChpZHggPT0gdGJsU2l6ZSkgewogICAgICB0YmxTaXplICo9IDI7CiAgICAgIHByaW1lcyA9IChpbnQgKilyZWFsbG9jKHByaW1lcywgc2l6ZW9mKGludCkgKiB0YmxTaXplKTsKICAgICAgaWYgKHByaW1lcyA9PSBOVUxMKSB7CiAgICAgICAgcHJpbnRmKCJwcmltZSB0YWJsZSBzaXplIGV4cGFuZGVkIGVycm9yXG4iKTsKICAgICAgICBleGl0KDApOwogICAgICB9IGVsc2UgewogICAgICAgIHByaW50ZigicHJpbWUgdGFibGUgc2l6ZSBleHBhbmRlZCB0byAlZFxuIiwgdGJsU2l6ZSk7CiAgICAgIH0KICAgIH0KICB9Cn0KCmludCBzZXFMZW4oaW50IGksIGludCBqKQp7CiAgaW50IGxlbiA9IDI7CiAgaW50IHN0ZXAgPSBwcmltZXNbal0gLSBwcmltZXNbaV07CiAgaW50IG4gPSBwcmltZXNbal0gKyBzdGVwOwogIHdoaWxlICgxKSB7CiAgICB3aGlsZSAobiA+IHByaW1lc1tqXSlqKys7CiAgICBpZiAobiA9PSBwcmltZXNbal0pIHsKICAgICAgbGVuKys7CiAgICAgIG4gKz0gc3RlcDsKICAgIH0gZWxzZSBicmVhazsKICB9CiAgcmV0dXJuIGxlbjsKfQoKaW50IG1haW4odm9pZCkKewogIGludCBpLCBqLCB0OwogIHByaW1lcyA9IChpbnQgKiltYWxsb2Moc2l6ZW9mKGludCkgKiB0YmxTaXplKTsKICBwcmltZXNbaWR4KytdID0gMjsKICBwcmltZXNbaWR4KytdID0gMzsKICBtYWtlUHJpbWVzKCk7CiAgcHJpbnRmKCJudW1iZXIgb2YgcHJpbWVzID0gJWRcbiIsIGlkeCk7CiAgcHJpbnRmKCJjYWxjdWxhdGluZy4uLlxuIik7CiAgZm9yICggaSA9IDA7IGkgPCBpZHg7ICsraSkgewogICAgZm9yICggaiA9IGkgKyAxOyBqICA8IGlkeDsgKytqICApIHsKICAgICAgdCA9IHNlcUxlbihpLCBqKTsKICAgICAgLy8gcHJpbnRmKCJpIGogPSAlZCAlZCAlZFxuIiwgaSxqLHQpOwogICAgICBpZiAobWF4TGVuZ3RoIDwgdCkgewogICAgICAgIG1heEZyb20gPSBwcmltZXNbaV07CiAgICAgICAgbWF4U3RlcCA9IHByaW1lc1tqXSAtIHByaW1lc1tpXTsKICAgICAgICBtYXhMZW5ndGggPSB0OwogICAgICAgIHByaW50ZigiZnJvbSBzdGVwIGxlbmd0aCA9ICVkICVkICVkXG4iLCBtYXhGcm9tLCBtYXhTdGVwLCBtYXhMZW5ndGgpOwogICAgICB9CiAgICB9CiAgfQogIC8vIHByaW50ZigiZnJvbSBzdGVwIGxlbmd0aCA9ICVkICVkICVkXG4iLCBtYXhGcm9tLCBtYXhTdGVwLCBtYXhMZW5ndGgpOwoKICByZXR1cm4gMDsKfQo=