Advertisement
Guest User

busError

a guest
Nov 6th, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.99 KB | None | 0 0
  1. // This is a stripped-down version of my code, such that it is the minimum necessary to reproduce the error.
  2.  
  3. #include <math.h>
  4.  
  5. #define FIRST_PRIME 2
  6. #define PRIME 1
  7. #define COMPOSITE 0
  8.  
  9. #define MAX_PRIMES 16
  10.  
  11. #define numberToIndex(x) ( (x) - FIRST_PRIME )
  12. #define indexToNumber(x) ( (x) + FIRST_PRIME )
  13.  
  14. #define CANDIDATE_PRIMES n - FIRST_PRIME
  15.  
  16. #define intArraySize(x) ( sizeof(x) / sizeof(int) )
  17.  
  18. int getCandidateFactors(unsigned long n, int output[], int outputLength)
  19. {
  20.  
  21.   printf("Initializing sieve...\n");
  22.  
  23.   if (n < FIRST_PRIME) return 0;
  24.   if (n == FIRST_PRIME) return n;
  25.  
  26.   int candidatePrimes[CANDIDATE_PRIMES];
  27.   // the set of all integers from FIRST_PRIME to n.
  28.  
  29.   unsigned long prime;
  30.   unsigned long composite;
  31.   // the prime number being considered and its multiples being discarded
  32.  
  33.   unsigned long i;
  34.   int j;
  35.   // array indexes
  36.  
  37.   // Assume all candidates are prime unless shown to be
  38.   // composite.
  39.  
  40.   // This for loop appears to be the offending piece of code.
  41.  
  42.   for (i=0;i<1;i++) // normally i<CANDIDATE_PRIMES
  43.   {
  44.  
  45.     printf("i: %d\n", i);
  46.  
  47.     //candidatePrimes[i] = PRIME;
  48.  
  49.   }
  50.   /*
  51.   // Begin with the first prime (2).
  52.  
  53.   prime = FIRST_PRIME;
  54.  
  55.   // Algorithm logic
  56.  
  57.   // Eliminate go through all the primes in candidatePrimes
  58.   // because the first possible multiple a prime can eliminate as
  59.   // a candidate is prime**2, we only need to go up to sqrt(n)
  60.   // to find our primes.
  61.  
  62.   printf("Starting sieve...\n");
  63.  
  64.   while (prime <= sqrt(n))
  65.   {
  66.  
  67.     printf("Prime: %d\n", prime);
  68.  
  69.     // Discard multiples of prime
  70.  
  71.     composite = prime * prime;
  72.     // first multiple of prime that won't (or may not have) been eliminated
  73.     // by smaller primes
  74.     while (numberToIndex(composite) < CANDIDATE_PRIMES)
  75.     {
  76.    
  77.       // discard composite, get next multiple
  78.    
  79.       candidatePrimes[numberToIndex(composite)] = COMPOSITE;
  80.       composite += prime;
  81.    
  82.     }
  83.      
  84.     // Find the prime
  85.  
  86.     prime++;
  87.     while (candidatePrimes[numberToIndex(prime)] != PRIME &&
  88.           prime < sqrt(n)) prime++;
  89.  
  90.   }
  91.  
  92.   // Output
  93.   j = 0;
  94.   i = 0;
  95.  
  96.   while (i < CANDIDATE_PRIMES && j < outputLength)
  97.   {
  98.  
  99.     while (candidatePrimes[i] != PRIME && i < CANDIDATE_PRIMES) i++;
  100.     if (i >= CANDIDATE_PRIMES)
  101.     {
  102.    
  103.       break;
  104.    
  105.     }
  106.    
  107.     output[j] = indexToNumber(i);
  108.    
  109.     j++;
  110.     i++;
  111.  
  112.   }
  113.  
  114.   return j;
  115.   // number of ints written to output
  116. */
  117. }
  118.  
  119. unsigned long calculateLCG_a(unsigned long LCG_m)
  120. {
  121.  
  122.   printf("Calculating LCG_a...\n");
  123.   printf("LCG_m = %ld\n", LCG_m);
  124.  
  125.   unsigned long output;
  126.  
  127.  
  128.   int primes[MAX_PRIMES];
  129.  
  130.   printf("sizeof: %d\n", sizeof(primes));
  131.  
  132.   int factors = getCandidateFactors(LCG_m, primes, MAX_PRIMES);
  133.   /*
  134.   printf("Got factors...\n");
  135.  
  136.   int p = 1;
  137.   if (factors > 0)
  138.   {
  139.  
  140.     int i;
  141.     for(i=0;i<factors;i++)
  142.     {
  143.    
  144.       if (LCG_m % primes[i] == 0) p *= primes[i];
  145.    
  146.     }
  147.  
  148.   }
  149.  
  150.   else
  151.   {
  152.  
  153.     return 0;
  154.  
  155.   }
  156.  
  157.     printf("Checked factors...\n");
  158.  
  159.   printf("Calculating output...\n");
  160.  
  161.   if (LCG_m % 4 == 0)
  162.   {
  163.  
  164.     output = 2*p + 1;
  165.  
  166.   }
  167.  
  168.   else
  169.   {
  170.  
  171.     output = p + 1;
  172.  
  173.   }
  174.  
  175.   printf("Returning.\n");
  176.  
  177.   if (0 < output < LCG_m) return output;
  178.   else return 0; // error*/
  179.  
  180. }
  181.  
  182. unsigned long readPositiveLong(char delimiter)
  183. {
  184.  
  185.   unsigned long output = 0;
  186.   char c;
  187.   c = getchar();
  188.  
  189.   while (c != delimiter)
  190.   {
  191.    
  192.     if ('0' <= c && c <= '9') // c is a number
  193.     {
  194.    
  195.       if ((unsigned long long) (output * 10 + c - '0') >
  196.          (unsigned long) ~(0))
  197.       {
  198.         return -1; // error
  199.       }
  200.    
  201.       output *= 10;
  202.       output += c - '0';
  203.    
  204.       c = getchar();
  205.    
  206.     }
  207.    
  208.     else
  209.     {
  210.      
  211.       return -1; // error
  212.      
  213.     }
  214.    
  215.   }
  216.  
  217.   return output;
  218.  
  219. }
  220.  
  221. void main(void)
  222. {
  223.  
  224.   printf("%d\n", calculateLCG_a(readPositiveLong('\n')));
  225.   // Offending input: 961168601842738797
  226.  
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement