Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is a stripped-down version of my code, such that it is the minimum necessary to reproduce the error.
- #include <math.h>
- #define FIRST_PRIME 2
- #define PRIME 1
- #define COMPOSITE 0
- #define MAX_PRIMES 16
- #define numberToIndex(x) ( (x) - FIRST_PRIME )
- #define indexToNumber(x) ( (x) + FIRST_PRIME )
- #define CANDIDATE_PRIMES n - FIRST_PRIME
- #define intArraySize(x) ( sizeof(x) / sizeof(int) )
- int getCandidateFactors(unsigned long n, int output[], int outputLength)
- {
- printf("Initializing sieve...\n");
- if (n < FIRST_PRIME) return 0;
- if (n == FIRST_PRIME) return n;
- int candidatePrimes[CANDIDATE_PRIMES];
- // the set of all integers from FIRST_PRIME to n.
- unsigned long prime;
- unsigned long composite;
- // the prime number being considered and its multiples being discarded
- unsigned long i;
- int j;
- // array indexes
- // Assume all candidates are prime unless shown to be
- // composite.
- // This for loop appears to be the offending piece of code.
- for (i=0;i<1;i++) // normally i<CANDIDATE_PRIMES
- {
- printf("i: %d\n", i);
- //candidatePrimes[i] = PRIME;
- }
- /*
- // Begin with the first prime (2).
- prime = FIRST_PRIME;
- // Algorithm logic
- // Eliminate go through all the primes in candidatePrimes
- // because the first possible multiple a prime can eliminate as
- // a candidate is prime**2, we only need to go up to sqrt(n)
- // to find our primes.
- printf("Starting sieve...\n");
- while (prime <= sqrt(n))
- {
- printf("Prime: %d\n", prime);
- // Discard multiples of prime
- composite = prime * prime;
- // first multiple of prime that won't (or may not have) been eliminated
- // by smaller primes
- while (numberToIndex(composite) < CANDIDATE_PRIMES)
- {
- // discard composite, get next multiple
- candidatePrimes[numberToIndex(composite)] = COMPOSITE;
- composite += prime;
- }
- // Find the prime
- prime++;
- while (candidatePrimes[numberToIndex(prime)] != PRIME &&
- prime < sqrt(n)) prime++;
- }
- // Output
- j = 0;
- i = 0;
- while (i < CANDIDATE_PRIMES && j < outputLength)
- {
- while (candidatePrimes[i] != PRIME && i < CANDIDATE_PRIMES) i++;
- if (i >= CANDIDATE_PRIMES)
- {
- break;
- }
- output[j] = indexToNumber(i);
- j++;
- i++;
- }
- return j;
- // number of ints written to output
- */
- }
- unsigned long calculateLCG_a(unsigned long LCG_m)
- {
- printf("Calculating LCG_a...\n");
- printf("LCG_m = %ld\n", LCG_m);
- unsigned long output;
- int primes[MAX_PRIMES];
- printf("sizeof: %d\n", sizeof(primes));
- int factors = getCandidateFactors(LCG_m, primes, MAX_PRIMES);
- /*
- printf("Got factors...\n");
- int p = 1;
- if (factors > 0)
- {
- int i;
- for(i=0;i<factors;i++)
- {
- if (LCG_m % primes[i] == 0) p *= primes[i];
- }
- }
- else
- {
- return 0;
- }
- printf("Checked factors...\n");
- printf("Calculating output...\n");
- if (LCG_m % 4 == 0)
- {
- output = 2*p + 1;
- }
- else
- {
- output = p + 1;
- }
- printf("Returning.\n");
- if (0 < output < LCG_m) return output;
- else return 0; // error*/
- }
- unsigned long readPositiveLong(char delimiter)
- {
- unsigned long output = 0;
- char c;
- c = getchar();
- while (c != delimiter)
- {
- if ('0' <= c && c <= '9') // c is a number
- {
- if ((unsigned long long) (output * 10 + c - '0') >
- (unsigned long) ~(0))
- {
- return -1; // error
- }
- output *= 10;
- output += c - '0';
- c = getchar();
- }
- else
- {
- return -1; // error
- }
- }
- return output;
- }
- void main(void)
- {
- printf("%d\n", calculateLCG_a(readPositiveLong('\n')));
- // Offending input: 961168601842738797
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement