Advertisement
hpolzer

Fibonacci numbers

Apr 16th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 KB | None | 0 0
  1. /*  A command-line program to compute (huge) Fibonacci numbers. An old program,
  2.     not very efficient concerning memory usage. To compile run: cc -ansi -o fib fib.c
  3.     Copyright (C) <September 1, 1992> Henning POLZER, h underscore polzer at gmx dot de
  4.  
  5.     This program is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21.  
  22. #define M 210   /* maximale Anzahl der Stellen; 1000. Fibonacci-Zahl hat 209 Stellen */
  23.  
  24. int             v[M], nv[M], erg[M], i, z;
  25.  
  26. void
  27. berechne(void)
  28. {
  29.     for (i = M - 1; i >= 0; i--) {
  30.         if ((erg[i] = v[i] + nv[i]) > 9) {
  31.             erg[i] -= 10;
  32.             v[i - 1]++;
  33.         }
  34.         v[i] = nv[i];
  35.         nv[i] = erg[i];
  36.     }
  37. }
  38.  
  39. void
  40. aktuell(void)
  41. {
  42.     int             k = 0;
  43.  
  44.     while (!erg[k])
  45.         k++;
  46.  
  47.     for (i = k; i < M; i++)
  48.         printf("%d", erg[i]);
  49. }
  50.  
  51. int
  52. main(int argc, char *argv[])    /* Ausgabe erst ab 3. Fibonacci-Zahl (= 2) */
  53. {
  54.     v[M - 1] = nv[M - 1] = 1;
  55.  
  56.     for (z = 1; z < atoi(argv[1]) - 1; z++)
  57.         berechne();
  58.  
  59.     aktuell();
  60.     printf("\n");
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement