Advertisement
hpolzer

Amicable numbers

Nov 18th, 2023
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.27 KB | Source Code | 0 0
  1. /* 
  2.     This program (ANSI C) computes the amicable numbers
  3.     (therefore "an.c") within a given range. The method used by
  4.     this program is rather simplistic. Last change, including minor
  5.     improvements and conversion into ANSI C: Sept. 10th, 2023.
  6.     Compile:    gcc -ansi -pedantic -O3 -o an an.c
  7.     Invoke:     an [lower_limit] upper_limit
  8.     Copyright (C) <March 19th, 2015> Henning POLZER.
  9.     Send comment and error reports to: h underscore polzer at gmx dot de
  10.  
  11.     Example:
  12.     an 1 10000  # "an 10000" yields the same (lower_limit==1 by default)
  13.  
  14.      220;        284
  15.     1184;       1210
  16.     2620;       2924
  17.     5020;       5564
  18.     6232;       6368
  19.  
  20.     This program is free software: you can redistribute it and/or modify
  21.     it under the terms of the GNU General Public License as published by
  22.     the Free Software Foundation, either version 3 of the License, or
  23.     (at your option) any later version.
  24.  
  25.     This program is distributed in the hope that it will be useful,
  26.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  27.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28.     GNU General Public License for more details.
  29.  
  30.     You should have received a copy of the GNU General Public License
  31.     along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. */
  33.  
  34.                     /* Erforderlich fuer: */
  35. #include <math.h>   /* floor () */
  36. #include <stdio.h>  /* printf () */
  37. #include <stdlib.h> /* atoi () */
  38.  
  39. typedef long ganz;  /* signed! */
  40. typedef struct {
  41.             ganz erster,zweiter;
  42.     } freunde; /* speichert Zahlwerte des Freundepaares */
  43.  
  44. /*
  45.     Gibt die TeilerSumme einer gegebenen Zahl zurueck.
  46. */
  47. ganz ts(ganz z)
  48. {
  49.     ganz i=2,gegenteiler,schleife=z,summe=1;
  50.  
  51.     do {
  52.       gegenteiler=floor(schleife/i);
  53.       if (schleife%i==0)
  54.         /* Teiler=Gegenteiler? Nur Teiler beachten: */
  55.         (i==gegenteiler)?summe++:(summe+=gegenteiler+i);
  56.       i++;
  57.     } while (gegenteiler>=i);
  58.     return summe;
  59. } /* ts */
  60.  
  61. /*
  62.     Prueft, ob zu der uebergebenen Zahl z eine befreundete
  63.     Zahl existiert und liefert in diesem Falle das Paar
  64.     befreundeter Zahlen zurueck. Andernfalls liefert
  65.     die Funktion die gepruefte Zahl und die Teilersumme
  66.     derselben als negative Zahlen zurueck, fuer den Fall,
  67.     dass diese weiter betrachtet werden sollen. (-1 als
  68.     Rueckgabewert von f.zweiter steht fuer eine
  69.     Primzahl, wenn z>1.)
  70. */
  71. freunde auf_freundschaft_pruefen(ganz z)
  72. {
  73.     ganz hilf,j=z,startwert=z;
  74.     freunde f;
  75.  
  76.     /* Berechnungen nur fuer Zahlen>3: */
  77.     (j<3)?j=3:0;
  78.  
  79.     hilf=ts(j);
  80.     if((ts(hilf)==j)&&(hilf>j)) {
  81.       f.erster=j; /* Paar gefunden */
  82.       f.zweiter=hilf;
  83.     } else {
  84.         f.erster=startwert*-1; /* Kein Paar */
  85.         f.zweiter=hilf*-1;
  86.       } /* else */
  87.          
  88.     return f;
  89. } /* auf_freundschaft_pruefen */
  90.  
  91. int main(int argc,char *argv[])
  92. {
  93.     ganz i,ug=1,og=1;
  94.     freunde f;
  95.  
  96.     if(argc==2) og=atoi(argv[1]);
  97.     if(argc==3) {
  98.       ug=atoi(argv[1]);
  99.       og=atoi(argv[2]);
  100.     } /* if(argc==3) */
  101.  
  102.     for(i=ug;i<=og;i++) {
  103.       f=auf_freundschaft_pruefen(i);
  104.       /*
  105.          Wenn mindestens ein Rueckgabewert positiv ist, hat
  106.          das Programm ein Paar befreundeter Zahlen gefunden:
  107.       */
  108.       if(f.erster>0) printf("%15.ld;%15.ld\n",f.erster,f.zweiter);
  109.       /* Kommentarmarkierungen folgender Zeile entfernen, um Primzahlen auszugeben: */
  110.       /*if((f.zweiter==-1)&&(i>1)) printf("%15.ld: Primzahl\n",i);*/
  111.     } /* for(i=ug;i<=og;i++) */
  112.  
  113.     return 0;
  114. } /* main */
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement