Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <math.h>
- /* Generate a CSV of the first MAX (see below) terms of OEIS A075085 */
- /* March 2024 - oeisfan(at)phodd.net */
- typedef unsigned long long Integer;
- typedef unsigned long Int;
- typedef unsigned short int usi;
- #define FALSE 0
- #define TRUE 1
- usi jump[8] = {4,2,4,2,4,6,2,6}; /* pattern of primes, mod 30 */
- Integer isPrime(Integer x) {
- Int p, sqrtx;
- register usi ji;
- static Integer prev = 0;
- static usi isPrimePrev = FALSE;
- if (x==prev) return isPrimePrev;
- prev = x;
- if (x<2) return isPrimePrev=FALSE;
- if (x==2 ||x==3 ||x==5 ||x==7 ) return isPrimePrev=TRUE;
- if (x %2==0||x %3==0||x %5==0||x %7==0) return isPrimePrev=FALSE;
- sqrtx = sqrt(x);
- for (p = 7, ji = 7; p <= sqrtx; p += jump[ji=(ji+1)&7]) { /* generate a candidate */
- if (x % p == 0) return isPrimePrev=FALSE;
- }
- return isPrimePrev=TRUE;
- }
- Integer nextPrime(Integer x){
- if (x<2) return 2;
- if (x==2) return 3;
- for (x+=1+(x%2); !isPrime(x); x+=2);
- return x;
- }
- #define MAX 80000000 /* 80 million */
- int main() {
- /* original gnu-bc code; nextprime() not supplied */
- /* scale=0;s=a[n=1]=p=2;print 1,",";2;for(n=2;n<=1000000;++n){
- p=nextprime(p);f=1;
- 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};
- a[n]=a;s+=a;print n,",";a};-1
- */
- Integer prevsum, sum, m, n, prime, a_candidate;
- uint64_t *v;
- uint64_t vec_pow;
- Integer vec_index;
- bool found;
- /* Use a bit vector to keep track of which values have been found in the
- sequence. Does not store in order of finding. Does not need to.
- */
- Integer vmax = 26*MAX;
- /* 64*MAX or greater would be better.
- 26*MAX skirts compilation errors and happens to work for MAX=80 million
- */
- Integer vvmax = vmax / sizeof(uint64_t);
- v = calloc(1 + vvmax, sizeof(uint64_t)); for(n = 0; n <= vvmax; n++)v[n]=0;
- if(v == NULL){
- fprintf(stderr, "Not enough memory to allocate space!\n");
- return 1;
- }
- sum = prime = 2;
- v[vec_index = 0] = vec_pow = (uint64_t)1 << prime;
- printf("1,2\n"); fflush(stdout);
- for(n = 2; n <= MAX; ++n){
- prime = nextPrime(prime);
- found = TRUE;
- for(a_candidate = prime - sum%prime; found; a_candidate += prime) {
- vec_index = a_candidate >> 6;
- vec_pow = (uint64_t)1 << (a_candidate & 63);
- if(vec_index > vmax) {
- fprintf(stderr, "Not enough space allocated to store flag!\n");
- return 1;
- }
- found = (
- (0 != (v[vec_index] & vec_pow)) ? TRUE : FALSE
- );
- if(!found)break;
- }
- v[vec_index] |= vec_pow;
- prevsum = sum;
- sum += a_candidate;
- if(prevsum > sum){
- fprintf(stderr, "Sum overflow detected!\n");
- return 2;
- }
- printf("%llu,%llu\n", n, a_candidate); fflush(stdout);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement