Download code

Jump to: navigation, search

Back to Trial_division_(C)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

trial_division.c

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Trial_division_(C)?oldid=19222
 14 */
 15 
 16 #include <math.h>  /* For sqrt() */
 17 
 18 #include <stdlib.h>   /* for malloc, calloc, free */
 19 #include <string.h>
 20 #include <stdio.h>
 21 
 22 #define IS_PRIME 0
 23 
 24 unsigned int trial_division_sqrt(unsigned int n) {
 25     unsigned int sqrt_n = (unsigned int)sqrt((double)n);
 26     unsigned int x;
 27     for(x=2; x <= sqrt_n; x++) {
 28         if ((n % x) == 0) {
 29             return x;
 30         }
 31     }
 32     return IS_PRIME;
 33 }
 34 
 35 
 36 unsigned int trial_division_squaring(unsigned int n) {
 37     unsigned int x, x_squared;
 38     for(x=2, x_squared=4;
 39         x_squared > 2*x - 1 && x_squared <= n;
 40         x++, x_squared += 2*x - 1)
 41     {
 42         if ((n % x) == 0) {
 43             return x;
 44         }
 45     }
 46     return IS_PRIME;
 47 }
 48 
 49 unsigned int trial_division_odd(unsigned int n) {
 50     unsigned int sqrt_n = (unsigned int)sqrt((double)n);
 51     unsigned int x;
 52     if ((n % 2) == 0) return 2;
 53     for(x=3; x <= sqrt_n; x+=2) {
 54         if ((n % x) == 0) {
 55             return x;
 56         }
 57     }
 58     return IS_PRIME;
 59 }
 60 
 61 typedef unsigned short u16;
 62 typedef unsigned int u32;
 63 
 64 u16* primes;
 65 
 66 void generate_prime_list(int max) {
 67     unsigned char* is_not_prime = (unsigned char*)calloc(max+1, 1);
 68     int i, j, num_primes = 1;
 69     for(i = 2; i <= max; i++) {
 70         if (is_not_prime[i]) continue;
 71         num_primes++;
 72         for(j = i + i; j <= max; j += i) {
 73             is_not_prime[j] = 1;
 74         }
 75     }
 76     primes = (u16*)malloc(sizeof(u16) * (num_primes + 1));
 77     j = 0;
 78     for(i = 2; i <= max; i++) {
 79         if (!is_not_prime[i]) {
 80             primes[j] = i;
 81             j++;
 82         }
 83     }
 84     primes[j] = 0;
 85     free(is_not_prime);
 86 }
 87 
 88 u32 trial_division_primes(u32 n) {
 89     u32 sqrt_n = (u32)sqrt((double)n);
 90     u16* prime = primes;
 91     while (*prime != 0 && *prime <= sqrt_n) {
 92         if ((n % *prime) == 0) {
 93             return *prime;
 94         }
 95         prime++;
 96     }
 97     return IS_PRIME;
 98 }
 99 
100 int main(int argc, char* argv[]) {
101     int i;
102     unsigned int n = atoi(argv[2]);
103 
104     if (strcmp(argv[1], "trial_division_sqrt") == 0) {
105         for(i=0; i<10000 - 1; i++) trial_division_sqrt(n);
106         printf("%u\n", trial_division_sqrt(n));
107     } else if (strcmp(argv[1], "trial_division_squaring") == 0) {
108         for(i=0; i<10000 - 1; i++) trial_division_squaring(n);
109         printf("%u\n", trial_division_squaring(n));
110     } else if (strcmp(argv[1], "trial_division_odd") == 0) {
111         for(i=0; i<10000 - 1; i++) trial_division_odd(n);
112         printf("%u\n", trial_division_odd(n));
113     } else if (strcmp(argv[1], "trial_division_primes") == 0) {
114         generate_prime_list(65536);
115         for(i=0; i<10000 - 1; i++) trial_division_primes(n);
116         printf("%u\n", trial_division_primes(n));
117     } else {
118         printf("Invalid algorithm selection.\n");
119     }
120     return 0;
121 }


hijacker
hijacker
hijacker
hijacker