NoPaste

factorize.c

von paedubucher
SNIPPET_DESC:
Primzahlfaktorisierung in C
SNIPPET_CREATION_TIME:
09.12.2023 20:41:07
SNIPPET_PRUNE_TIME:
Unendlich

SNIPPET_TEXT:
  1. #include <math.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. bool is_prime(int);
  8. int *primes_up_to(int);
  9. int *factorize(int);
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.     int lower = 0, upper = 0, i = 0, j = 0;
  14.  
  15.     if (argc < 3) {
  16.         fprintf(stderr, "usage: %s [lower] [upper]\n", argv[0]);
  17.         return 1;
  18.     }
  19.  
  20.     lower = atoi(argv[1]);
  21.     upper = atoi(argv[2]);
  22.     if (lower > upper) {
  23.         fprintf(stderr, "[lower] must be <= [upper], was %d > %d\n", lower, upper);
  24.         return 1;
  25.     }
  26.  
  27.     for (i = lower; i <= upper; i++) {
  28.         printf("%d: ", i);
  29.         int *factors = factorize(i);
  30.         for (j = 0; factors[j] != 0; j++) {
  31.             printf("%d ", factors[j]);
  32.         }
  33.         free(factors);
  34.         putchar('\n');
  35.     }
  36.  
  37.     return 0;
  38. }
  39.  
  40. int *factorize(int x)
  41. {
  42.     int i = 0, j = 0, n = 0;
  43.     int *primes = NULL, *factors = NULL;
  44.  
  45.     if (x < 1) {
  46.         factors = (int *)malloc(sizeof(int));
  47.         factors[0] = 0;
  48.         return factors;
  49.     }
  50.  
  51.     n = log2(x) + 1; // heuristic: log2(16) = 4, fits "worst" case  [2, 2, 2, 2]
  52.     primes = primes_up_to(sqrt(x));
  53.     factors = (int *)malloc(sizeof(int) * (n + 1));
  54.     memset(factors, 0, n + 1);
  55.  
  56.     for (i = 0, j = 0; primes[i] != 0 && x > 1;) {
  57.         if (x % primes[i] == 0) {
  58.             factors[j++] = primes[i];
  59.             x /= primes[i];
  60.         } else {
  61.             i++;
  62.         }
  63.     }
  64.     if (x > 1) {
  65.         factors[j] = x;
  66.     }
  67.     free(primes);
  68.  
  69.     return factors;
  70. }
  71.  
  72. int *primes_up_to(int x)
  73. {
  74.     int i = 0, j = 0;
  75.     int *primes = NULL;
  76.  
  77.     if (x < 2) {
  78.         primes = (int *)malloc(sizeof(int));
  79.         primes[0] = 0;
  80.         return primes;
  81.     }
  82.  
  83.     primes = (int *)malloc(sizeof(int) * x);
  84.     memset(primes, 0, x);
  85.  
  86.     for (i = 2, j = 0; i <= x; i++) {
  87.         if (is_prime(i)) {
  88.             primes[j++] = i;
  89.         }
  90.     }
  91.  
  92.     return primes;
  93. }
  94.  
  95. bool is_prime(int x)
  96. {
  97.     if (x < 2) {
  98.         return false;
  99.     }
  100.     for (int i = 2; i <= x / 2; i++) {
  101.         if (x % i == 0) {
  102.             return false;
  103.         }
  104.     }
  105.     return true;
  106. }

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN