Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- pfs.pas computes prime factors within the limits of ugrenze (lower limit)
- to ogrenze (upper limit), to compile run "fpc pfs.pas".
- (This is a translation of my programm "Primfaktorzerlegung" to [Free] Pascal.)
- Copyright (C) <March 19th, 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/>.
- *)
- PROGRAM pfs;
- VAR z, (* zu faktorisierende Zahl *)
- s, (* Startwert, wie z *)
- faktor, (* Primfaktor *)
- af, (* aktueller Faktor *)
- og, (* Obergrenze *)
- exponent, (* ggf. Exponenten speichern *)
- schleife: longword; (* Faktorisierung von ugrenze bis ogrenze, Standard: "integer" *)
- flag: boolean; (* Faktor gefunden? *)
- CONST ugrenze = 1; (* Startwert *)
- ogrenze = 30; (* Endwert *)
- BEGIN (* Hauptprogramm *)
- FOR schleife := ugrenze TO ogrenze DO
- BEGIN
- z := schleife;
- s := z; (* auch Startwert speichern *)
- faktor := 2;
- flag := false;
- exponent := 0;
- write (z, ' = ');
- og := trunc (sqrt (z)) + 1; (* Obergrenze fuer Suche festlegen *)
- IF z > 3 THEN (* 2 und 3 als prim erkennen *)
- WHILE faktor < og + 1 DO
- BEGIN
- IF z MOD faktor = 0 THEN (* z durch faktor teilbar? JA: *)
- BEGIN
- exponent := exponent + 1; (* Exponenten erhoehen *)
- af := faktor; (* akt. Faktor speichern *)
- IF exponent < 2 THEN write (faktor); (* akt. Faktor nur einmal ausgeben *)
- flag := true; (* Faktor gefunden *)
- z := trunc (z / faktor); (* Obergrenze anpassen, Gegenteiler in z speichern *)
- og := z
- END (* IF *)
- ELSE BEGIN (* NEIN: *)
- (* Bei ungeradem Faktor >= 3 Schrittweite = 2: *)
- IF faktor MOD 2 <> 0 THEN faktor := faktor + 2
- ELSE faktor := faktor + 1;
- exponent := 0 (* noch kein Exponent vorhanden *)
- END; (* ELSE *)
- (* Multiplikationszeichen zwischen den Primfaktoren setzen, aber weder
- vor erstem noch nach letztem Faktor; Exponenten >= 2 ausgeben: *)
- IF (z MOD faktor <> 0) AND (exponent > 1) THEN write ('^', exponent);
- IF (z MOD faktor = 0) AND (faktor <> af) AND flag THEN write (' * ')
- END; (* WHILE *)
- IF s = 1 THEN write ('1 * 1') ELSE (* 1 keine Primzahl *)
- IF s = z THEN write ('prim');
- writeln
- END (* FOR *)
- END. (* Hauptprogramm *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement