Advertisement
hpolzer

Emirp number

Feb 22nd, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.38 KB | None | 0 0
  1. {
  2.     EMIRP.PAS, computes numbers that are prime numbers when read backwards
  3.     to forwards, too, e.g., 13 17 31 37 71 73 79 97. To compile run "fpc emirp.pas".
  4.     Copyright (C) <February 01, 2016> Henning Polzer,
  5.     send comments and error reports to: h underscore polzer at gmx dot de.
  6.  
  7.     This program is free software; you can redistribute it and/or
  8.     modify it under the terms of the GNU General Public License
  9.     as published by the Free Software Foundation; either version 2
  10.     of the License, or (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, write to the Free Software
  19.     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  20. }
  21.  
  22. PROGRAM mirpzahlen_berechnen;
  23. TYPE Tgz = int64;                   { Standard: integer }
  24.  
  25. VAR i, og, ug: Tgz;
  26.  
  27. PROCEDURE emirp (zahl: Tgz);
  28. VAR h: Tgz;
  29.  
  30.   FUNCTION prim (zahl: Tgz): boolean;       { Primzahl? }
  31.   VAR k: Tgz;
  32.  
  33.   BEGIN { prim }
  34.     prim := true;
  35.     k := 3;
  36.  
  37.     IF zahl / 2 = trunc (zahl / 2) THEN prim := false;
  38.     IF zahl / 2 <> trunc (zahl / 2) THEN
  39.       WHILE ((zahl / k <> trunc (zahl / k)) AND (k * k < zahl)) DO
  40.         k := k + 2;
  41.     IF zahl / k = trunc (zahl / k) THEN prim := false;
  42.  
  43.     CASE zahl OF
  44.       1      : prim := false;
  45.       2, 3, 5: prim := true
  46.     END { case }
  47.   END; { prim }
  48.  
  49.   FUNCTION spiegelzahl (zahl: Tgz): Tgz;
  50.   VAR div_erg, s, z: Tgz;
  51.  
  52.   BEGIN { spiegelzahl }
  53.     s := 0;
  54.     z := zahl;
  55.  
  56.     IF z < 10 THEN s := z ELSE
  57.       REPEAT
  58.         div_erg := trunc (z / 10);
  59.         s := (s + (z - div_erg * 10)) * 10;
  60.         IF div_erg < 10 THEN s := s + div_erg;
  61.         z := div_erg
  62.       UNTIL z < 10;
  63.  
  64.     spiegelzahl := s
  65.   END; { spiegelzahl }
  66.  
  67. BEGIN { emirp }
  68.   h := spiegelzahl (zahl);
  69.   IF h <> zahl THEN                 { (Primzahl)palindrom ausser acht lassen }
  70.     IF (prim (zahl) AND prim (h)) THEN write (zahl, ' ')
  71. END; { emirp }
  72.  
  73. BEGIN { Hauptprogramm }
  74.   write ('Untergrenze: '); readln (ug);
  75.   write ('Obergrenze : '); readln (og);
  76.   i := ug;
  77.   IF i < 13 THEN i := 13;
  78.  
  79.   REPEAT
  80.     emirp (i);
  81.     i := i + 1
  82.   UNTIL i > og;
  83.   writeln
  84. END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement