Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- pfs.c computes prime factors within the limits of UGRENZE (lower limit)
- to OGRENZE (upper limit), to compile run "gcc -ansi -o pfs pfs.c"
- Copyright (C) <February 27th, 2015> Henning POLZER, h underscore polzer at gmx dot de
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include <math.h> /* fuer sqrt () */
- #include <stdio.h>
- #define UGRENZE 1 /* Startwert */
- #define OGRENZE 50 /* Endwert */
- int
- main()
- {
- unsigned long z, /* zu faktorisierende Zahl */
- s, /* Startwert, wie z */
- faktor, /* Primfaktor */
- af, /* aktueller Faktor */
- og, /* Obergrenze */
- flag, /* mindestens ein Faktor gefunden? */
- exponent, /* ggf. Exponenten speichern */
- schleife; /* Faktorisierung von UGRENZE bis
- * OGRENZE */
- for (schleife = UGRENZE; schleife < OGRENZE; schleife++) {
- s = z = schleife; /* auch Startwert speichern */
- faktor = 2;
- flag = 0;
- exponent = 0;
- printf ("%lu = ", z);
- og = (int) sqrt(z) + 1; /* Obergrenze fuer Suche festlegen */
- if (z > 3) /* 2 und 3 als prim erkennen */
- do {
- if (z % faktor == 0) { /* z durch faktor
- * teilbar? JA: */
- exponent++; /* Exponenten erhoehen */
- af = faktor; /* akt. Faktor speichern */
- if (exponent < 2) /* akt. Faktor nur
- * einmal ausgeben */
- printf("%lu", faktor);
- flag = 1; /* Faktor gefunden */
- og = z = (int) z / faktor; /* Obergrenze anpassen
- * und Gegenteiler in z
- * speichern */
- } else { /* NEIN: */
- /*
- * Bei ungeradem Faktor >= 3
- * Schrittweite = 2:
- */
- if (faktor % 2 != 0)
- faktor += 2;
- else
- faktor++;
- exponent = 0; /* noch kein Exponent
- * vorhanden */
- }
- /*
- * Multiplikationszeichen zwischen den
- * Primfaktoren setzen, aber weder vor erstem
- * noch nach letztem Faktor; Exponenten >= 2
- * ausgeben:
- */
- if ((z % faktor != 0) && (exponent > 1))
- printf("^%lu", exponent);
- if ((z % faktor == 0) && (faktor != af) && (flag == 1))
- printf(" * ");
- } while (faktor < og + 1);
- if (s == 1)
- printf("1 * 1"); /* 1 keine Primzahl */
- else if (s == z)
- printf("prim");
- printf("\n");
- } /* for */
- return 0;
- } /* main */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement