Advertisement
hpolzer

Amicable numbers

Mar 23rd, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.94 KB | None | 0 0
  1. (* 
  2.     This program computes the amicable numbers within the range from
  3.     "ug" (lower limit) to "og" (upper limit), to compile run "fpc befr_zahlen.pas".
  4.     (The method used in this program is rather simplistic.)
  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.  
  22. PROGRAM befreundete_zahlen;
  23. VAR hilf, j, ug, og: longword;              (* Standard: integer *)
  24.  
  25. FUNCTION ts (z: longword): longword;            (* Teilersumme berechnen *)
  26. VAR i, gegenteiler, schleife, summe: longword;
  27.  
  28. BEGIN
  29.   schleife := z;
  30.   i := 2;
  31.   summe := 1;
  32.   REPEAT
  33.     gegenteiler := trunc (schleife / i);
  34.     IF schleife MOD i = 0 THEN
  35.       IF i = gegenteiler THEN summe := summe + i    (* Teiler = Gegenteiler? Nur Teiler beachten! *)
  36.       ELSE summe := summe + gegenteiler + i;
  37.     i := i + 1
  38.   UNTIL gegenteiler < i;
  39.   ts := summe
  40. END;
  41.  
  42. BEGIN
  43.   REPEAT
  44.     writeln ('Befreundete Zahlen finden, zum Beenden fuer mind. einen Wert 0 eingeben:');
  45.     write ('Untergrenze: '); readln (ug);
  46.     write ('Obergrenze : '); readln (og);
  47.  
  48.     IF (ug > 0) AND (og > 0) THEN
  49.       FOR j := ug TO og DO
  50.       BEGIN
  51.         hilf := ts (j);
  52.         IF hilf <> j THEN
  53.           IF (ts (hilf) = j) AND (hilf > j) THEN writeln (j:10, ' und ', hilf:10) (* formatierte Ausgabe *)
  54.       END (* FOR *)
  55.   UNTIL (ug = 0) OR (og = 0)
  56. END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement