Advertisement
oeisfan-phodd

Generate a CSV of OEIS A075085

Mar 7th, 2024 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <math.h>
  6.  
  7. /* Generate a CSV of the first MAX (see below) terms of OEIS A075085 */
  8. /* March 2024 - oeisfan(at)phodd.net */
  9.  
  10. typedef  unsigned long long  Integer;
  11. typedef  unsigned long       Int;
  12. typedef  unsigned short int  usi;
  13.  
  14. #define FALSE 0
  15. #define TRUE  1
  16.  
  17. usi jump[8] = {4,2,4,2,4,6,2,6}; /* pattern of primes, mod 30 */
  18.  
  19. Integer isPrime(Integer x) {
  20.  
  21.    Int p, sqrtx;
  22.    register usi ji;
  23.    static Integer prev = 0;
  24.    static usi isPrimePrev = FALSE;
  25.  
  26.    if (x==prev) return isPrimePrev;
  27.    prev = x;
  28.  
  29.    if (x<2) return isPrimePrev=FALSE;
  30.    if (x==2   ||x==3   ||x==5   ||x==7   ) return isPrimePrev=TRUE;
  31.    if (x %2==0||x %3==0||x %5==0||x %7==0) return isPrimePrev=FALSE;
  32.  
  33.    sqrtx = sqrt(x);
  34.    for (p = 7, ji = 7; p <= sqrtx; p += jump[ji=(ji+1)&7]) { /* generate a candidate */
  35.      if (x % p == 0) return isPrimePrev=FALSE;
  36.    }
  37.  
  38.    return isPrimePrev=TRUE;
  39. }
  40.  
  41. Integer nextPrime(Integer x){
  42.   if (x<2) return 2;
  43.   if (x==2) return 3;
  44.   for (x+=1+(x%2); !isPrime(x); x+=2);
  45.   return x;
  46. }
  47.  
  48. #define MAX 80000000 /* 80 million */
  49.  
  50.  
  51. int main() {
  52.   /* original gnu-bc code; nextprime() not supplied */
  53.   /* scale=0;s=a[n=1]=p=2;print 1,",";2;for(n=2;n<=1000000;++n){
  54.        p=nextprime(p);f=1;
  55.        for(a=p-s%p;f;a+=p){f=0;for(m=1;m<n;m++)if(a[m]==a){f=1;break};if(!f)break};
  56.        a[n]=a;s+=a;print n,",";a};-1
  57.   */
  58.  
  59.   Integer prevsum, sum, m, n, prime, a_candidate;
  60.   uint64_t *v;
  61.   uint64_t vec_pow;
  62.   Integer vec_index;
  63.   bool found;
  64.  
  65.   /* Use a bit vector to keep track of which values have been found in the
  66.      sequence. Does not store in order of finding. Does not need to.
  67.   */
  68.   Integer vmax = 26*MAX;
  69.     /* 64*MAX or greater would be better.
  70.        26*MAX skirts compilation errors and happens to work for MAX=80 million
  71.     */
  72.   Integer vvmax = vmax / sizeof(uint64_t);
  73.   v = calloc(1 + vvmax, sizeof(uint64_t)); for(n = 0; n <= vvmax; n++)v[n]=0;
  74.   if(v == NULL){
  75.     fprintf(stderr, "Not enough memory to allocate space!\n");
  76.     return 1;
  77.   }
  78.  
  79.   sum = prime = 2;
  80.   v[vec_index = 0] = vec_pow = (uint64_t)1 << prime;
  81.   printf("1,2\n"); fflush(stdout);
  82.  
  83.   for(n = 2; n <= MAX; ++n){
  84.     prime = nextPrime(prime);
  85.     found = TRUE;
  86.     for(a_candidate = prime - sum%prime; found; a_candidate += prime) {
  87.       vec_index = a_candidate >> 6;
  88.       vec_pow = (uint64_t)1 << (a_candidate & 63);
  89.       if(vec_index > vmax) {
  90.         fprintf(stderr, "Not enough space allocated to store flag!\n");
  91.         return 1;
  92.       }
  93.       found = (
  94.         (0 != (v[vec_index] & vec_pow)) ? TRUE : FALSE
  95.       );
  96.       if(!found)break;
  97.     }
  98.     v[vec_index] |= vec_pow;
  99.     prevsum = sum;
  100.     sum += a_candidate;
  101.     if(prevsum > sum){
  102.       fprintf(stderr, "Sum overflow detected!\n");
  103.       return 2;
  104.     }
  105.     printf("%llu,%llu\n", n, a_candidate); fflush(stdout);
  106.   }
  107.   return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement