Advertisement
hpolzer

Primfaktorzerlegung

Apr 7th, 2015
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 KB | None | 0 0
  1. /*
  2.     pfs.c computes prime factors within the limits of UGRENZE (lower limit)
  3.     to OGRENZE (upper limit), to compile run "gcc -ansi -o pfs pfs.c"
  4.     Copyright (C) <February 27th, 2015> Henning POLZER, h underscore polzer at gmx dot de
  5.  
  6.     This program is free software: you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation, either version 3 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. */
  19.  
  20.  
  21. #include <math.h>                           /* fuer sqrt () */
  22. #include <stdio.h>
  23.  
  24. #define UGRENZE  1                          /* Startwert */
  25. #define OGRENZE 50                          /* Endwert */
  26.  
  27. int
  28. main()
  29. {
  30.     unsigned long   z,                      /* zu faktorisierende Zahl */
  31.                     s,                      /* Startwert, wie z */
  32.                     faktor,                     /* Primfaktor */
  33.                     af,                     /* aktueller Faktor */
  34.                     og,                     /* Obergrenze */
  35.                     flag,                       /* mindestens ein Faktor gefunden? */
  36.                     exponent,                   /* ggf. Exponenten speichern */
  37.                     schleife;                   /* Faktorisierung von UGRENZE bis
  38.                                      * OGRENZE */
  39.  
  40.     for (schleife = UGRENZE; schleife < OGRENZE; schleife++) {
  41.         s = z = schleife;                   /* auch Startwert speichern */
  42.         faktor = 2;
  43.         flag = 0;
  44.         exponent = 0;
  45.  
  46.         printf ("%lu = ", z);
  47.         og = (int) sqrt(z) + 1;                 /* Obergrenze fuer Suche festlegen */
  48.  
  49.         if (z > 3)                      /* 2 und 3 als prim erkennen */
  50.             do {
  51.                 if (z % faktor == 0) {          /* z durch faktor
  52.                                      * teilbar? JA: */
  53.                     exponent++;         /* Exponenten erhoehen */
  54.                     af = faktor;            /* akt. Faktor speichern */
  55.                     if (exponent < 2)       /* akt. Faktor nur
  56.                                      * einmal ausgeben */
  57.                         printf("%lu", faktor);
  58.                     flag = 1;           /* Faktor gefunden */
  59.                     og = z = (int) z / faktor;  /* Obergrenze anpassen
  60.                                      * und Gegenteiler in z
  61.                                      * speichern */
  62.                 } else {    /* NEIN: */
  63.                     /*
  64.                      * Bei ungeradem Faktor >= 3
  65.                      * Schrittweite = 2:
  66.                      */
  67.                     if (faktor % 2 != 0)
  68.                         faktor += 2;
  69.                     else
  70.                         faktor++;
  71.                     exponent = 0;           /* noch kein Exponent
  72.                                      * vorhanden */
  73.                 }
  74.  
  75.                 /*
  76.                  * Multiplikationszeichen zwischen den
  77.                  * Primfaktoren setzen, aber weder vor erstem
  78.                  * noch nach letztem Faktor; Exponenten >= 2
  79.                  * ausgeben:
  80.                  */
  81.                 if ((z % faktor != 0) && (exponent > 1))
  82.                     printf("^%lu", exponent);
  83.                 if ((z % faktor == 0) && (faktor != af) && (flag == 1))
  84.                     printf(" * ");
  85.             } while (faktor < og + 1);
  86.  
  87.         if (s == 1)
  88.             printf("1 * 1");                /* 1 keine Primzahl */
  89.         else if (s == z)
  90.             printf("prim");
  91.         printf("\n");
  92.     }           /* for */
  93.  
  94.     return 0;
  95. }               /* main */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement