Advertisement
hpolzer

Pythagorean Prime

Feb 19th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.44 KB | None | 0 0
  1. {
  2.     PYTHAGOREAN_PRIME.PAS computes pythagorean prime numbers and the square
  3.     numbers that sum up to the respective pythagorean prime.
  4.     To compile run "fpc pythagorean_prime.pas".
  5.     Copyright (C) <February 01, 2016> Henning Polzer,
  6.     send comments and error reports to: h underscore polzer at gmx dot de.
  7.  
  8.     This program is free software; you can redistribute it and/or
  9.     modify it under the terms of the GNU General Public License
  10.     as published by the Free Software Foundation; either version 2
  11.     of the License, or (at your option) any later version.
  12.  
  13.     This program is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.     GNU General Public License for more details.
  17.  
  18.     You should have received a copy of the GNU General Public License
  19.     along with this program; if not, write to the Free Software
  20.     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  21. }
  22.  
  23. PROGRAM pythagoreische_primzahlen_summe_zweier_quadratzahlen;
  24. TYPE Tgz = longint;                 { Standard: integer }
  25.  
  26. VAR i, og, ug: Tgz;
  27.  
  28. PROCEDURE ppz (zahl: Tgz);              { pythag. Primzahl finden und ausgeben }
  29. VAR diff, pythag, wurzel: Tgz;
  30.  
  31.   FUNCTION prim (zahl: Tgz): boolean;       { Primzahl? }
  32.   VAR k: Tgz;
  33.  
  34.   BEGIN { prim }
  35.     prim := true;
  36.     k := 3;
  37.  
  38.     IF zahl / 2 = trunc (zahl / 2) THEN prim := false;
  39.     IF zahl / 2 <> trunc (zahl / 2) THEN
  40.       WHILE ((zahl / k <> trunc (zahl / k)) AND (k * k < zahl)) DO
  41.         k := k + 2;
  42.     IF zahl / k = trunc (zahl / k) THEN prim := false;
  43.  
  44.     CASE zahl OF
  45.       1      : prim := false;
  46.       2, 3, 5: prim := true
  47.     END { case }
  48.   END; { prim }
  49.  
  50.   FUNCTION qzl (zahl: Tgz): boolean;        { Quadratzahl? }
  51.   BEGIN { qzl }
  52.     IF trunc (sqrt (zahl)) = sqrt (zahl) THEN qzl := true ELSE qzl := false
  53.   END; { qzl }
  54.  
  55. BEGIN { ppz }
  56.   pythag := 4*zahl+1;
  57.   IF prim (pythag) THEN
  58.   BEGIN
  59.     wurzel := trunc (sqrt (pythag));
  60.     REPEAT
  61.       diff := pythag - wurzel*wurzel;
  62.       IF qzl (diff) THEN write (diff, '+', wurzel*wurzel);
  63.       wurzel := wurzel-1
  64.     UNTIL (qzl (diff)) OR (wurzel < 2);     { sichere Abbruchbedingung }
  65.     write ('=', pythag, ' ')
  66.   END { if }
  67. END; { ppz }
  68.  
  69. BEGIN { Hauptprogramm }
  70.   write ('Untergrenze: '); readln (ug);
  71.   write ('Obergrenze : '); readln (og);
  72.   i := trunc (ug/4);
  73.  
  74.   REPEAT
  75.     ppz (i);
  76.     i := i + 1
  77.   UNTIL i > trunc (og/4);
  78.   writeln
  79. END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement