Advertisement
hpolzer

Primfaktorzerlegung

Mar 19th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.91 KB | None | 0 0
  1. (*
  2.     pfs.pas computes prime factors within the limits of ugrenze (lower limit)
  3.     to ogrenze (upper limit), to compile run "fpc pfs.pas".
  4.     (This is a translation of my programm "Primfaktorzerlegung" to [Free] Pascal.)
  5.     Copyright (C) <March 19th, 2015> Henning POLZER, h underscore polzer at gmx dot de
  6.  
  7.     This program is free software: you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation, either version 3 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19. *)
  20.  
  21. PROGRAM pfs;
  22. VAR z,                  (* zu faktorisierende Zahl *)
  23.     s,                  (* Startwert, wie z *)
  24.     faktor,                 (* Primfaktor *)
  25.     af,                 (* aktueller Faktor *)
  26.     og,                 (* Obergrenze *)
  27.     exponent,               (* ggf. Exponenten speichern *)
  28.     schleife:   longword;       (* Faktorisierung von ugrenze bis ogrenze, Standard: "integer" *)
  29.     flag:       boolean;        (* Faktor gefunden? *)
  30.  
  31. CONST   ugrenze =  1;               (* Startwert *)
  32.     ogrenze = 30;               (* Endwert *)
  33.  
  34. BEGIN (* Hauptprogramm *)
  35.   FOR schleife := ugrenze TO ogrenze DO
  36.   BEGIN
  37.     z := schleife;
  38.     s := z;                 (* auch Startwert speichern *)
  39.     faktor := 2;
  40.     flag := false;
  41.     exponent := 0;
  42.  
  43.     write (z, ' = ');
  44.     og := trunc (sqrt (z)) + 1;         (* Obergrenze fuer Suche festlegen *)
  45.  
  46.     IF z > 3 THEN               (* 2 und 3 als prim erkennen *)
  47.     WHILE faktor < og + 1 DO
  48.     BEGIN
  49.       IF z MOD faktor = 0 THEN          (* z durch faktor teilbar? JA: *)
  50.       BEGIN
  51.         exponent := exponent + 1;       (* Exponenten erhoehen *)
  52.         af := faktor;               (* akt. Faktor speichern *)
  53.         IF exponent < 2 THEN write (faktor);    (* akt. Faktor nur einmal ausgeben *)
  54.         flag := true;               (* Faktor gefunden *)
  55.         z := trunc (z / faktor);        (* Obergrenze anpassen, Gegenteiler in z speichern *)  
  56.         og := z
  57.       END (* IF *)
  58.       ELSE BEGIN                (* NEIN: *)
  59.           (* Bei ungeradem Faktor >= 3 Schrittweite = 2: *)
  60.          IF faktor MOD 2 <> 0 THEN faktor := faktor + 2
  61.            ELSE faktor := faktor + 1;
  62.          exponent := 0          (* noch kein Exponent vorhanden *)
  63.        END; (* ELSE *)
  64.       (* Multiplikationszeichen zwischen den Primfaktoren setzen, aber weder
  65.          vor erstem noch nach letztem Faktor; Exponenten >= 2 ausgeben: *)
  66.       IF (z MOD faktor <> 0) AND (exponent > 1) THEN write ('^', exponent);
  67.       IF (z MOD faktor = 0) AND (faktor <> af) AND flag THEN write (' * ')
  68.     END; (* WHILE *)
  69.  
  70.     IF s = 1 THEN write ('1 * 1') ELSE      (* 1 keine Primzahl *)
  71.       IF s = z THEN write ('prim');
  72.     writeln
  73.   END (* FOR *)
  74. END. (* Hauptprogramm *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement