Madmouse

a fibonacci sequence generator written in C

Oct 20th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.39 KB | None | 0 0
  1. // ----------------------------------------------------------------------------
  2. // "THE BEER-WARE LICENSE" (Revision 43):
  3. // <[email protected]> wrote this file. As long as you retain this notice you
  4. // can do whatever you want with this stuff. If we meet some day, and you think
  5. // this stuff is worth it, you can buy me a beer in return Aaron R. Yool
  6. // ----------------------------------------------------------------------------
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <math.h>
  12.  
  13. typedef enum{false, true = !false} bool;
  14.  
  15. usage(const char *name)
  16. {
  17.     printf("Usage: %s <starting number> <number of iterations>\n", name);
  18.     exit(-1);
  19. }
  20.  
  21. main(unsigned int count, const char **args)
  22. {
  23.     long double i, a = 0, b = 0, c = 0;
  24.     if(count == 3 ? (args[1][0] >= '0' && args[1][0] <= '9') || (strlen(args[1])>1 ? (args[1][0] == '-' && args[1][1] >= '0' && args[1][1] <= '9') : true) : false)
  25.         b = atoll(args[1]);
  26.     else
  27.         usage(args[0]);
  28.    
  29.     if(count == 3 ? (args[2][0] >= '0' && args[2][0] <= '9') || (strlen(args[2])>1 ? (args[2][0] == '-' && args[2][1] >= '0' && args[2][1] <= '9') : true) : false)
  30.         i = atoll(args[2]);
  31.     else
  32.         usage(args[0]);
  33.  
  34.     for(i;i > 0;--i)
  35.     {
  36.         c = a;
  37.         a = a+b;
  38.         b = c;
  39.         if(isinf(a))
  40.         {
  41.             printf("\n\nWe had to stop here: %.0LF\n", i);
  42.             exit(-1);
  43.         }
  44.         i != 1 ? printf("%.0LF, ", a) : printf("%.0LF\n", a);
  45.     }
  46.    
  47.     return;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment