fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4.  
  5. int *primes, tblSize = 10, idx = 0, n = 2, maxFrom, maxStep, maxLength = 0;
  6.  
  7. void makePrimes();
  8.  
  9. void makePrimes()
  10. {
  11. int i, j;
  12. for (i = primes[idx - 1] + 2; i < INT_MAX / 10; i += 2) {
  13. for ( j = 0; (i % primes[j]) && (i > primes[j]*primes[j]); ++j);
  14. if (i % primes[j])
  15. primes[idx++] = i;
  16. if (idx == tblSize) {
  17. tblSize *= 2;
  18. primes = (int *)realloc(primes, sizeof(int) * tblSize);
  19. if (primes == NULL) {
  20. printf("prime table size expanded error\n");
  21. exit(0);
  22. } else {
  23. printf("prime table size expanded to %d\n", tblSize);
  24. }
  25. }
  26. }
  27. }
  28.  
  29. int seqLen(int i, int j)
  30. {
  31. int len = 2;
  32. int step = primes[j] - primes[i];
  33. int n = primes[j] + step;
  34. while (1) {
  35. while (n > primes[j])j++;
  36. if (n == primes[j]) {
  37. len++;
  38. n += step;
  39. } else break;
  40. }
  41. return len;
  42. }
  43.  
  44. int main(void)
  45. {
  46. int i, j, t;
  47. primes = (int *)malloc(sizeof(int) * tblSize);
  48. primes[idx++] = 2;
  49. primes[idx++] = 3;
  50. makePrimes();
  51. printf("number of primes = %d\n", idx);
  52. printf("calculating...\n");
  53. for ( i = 0; i < idx; ++i) {
  54. for ( j = i + 1; j < idx; ++j ) {
  55. t = seqLen(i, j);
  56. // printf("i j = %d %d %d\n", i,j,t);
  57. if (maxLength < t) {
  58. maxFrom = primes[i];
  59. maxStep = primes[j] - primes[i];
  60. maxLength = t;
  61. printf("from step length = %d %d %d\n", maxFrom, maxStep, maxLength);
  62. }
  63. }
  64. }
  65. // printf("from step length = %d %d %d\n", maxFrom, maxStep, maxLength);
  66.  
  67. return 0;
  68. }
  69.  
Time limit exceeded #stdin #stdout 5s 4372KB
stdin
Standard input is empty
stdout
Standard output is empty