Fibonacci Number Generator (src)

This topic was published by and viewed 1016 times since "". The last page revision was "".

Viewing 1 post (of 1 total)
  • Author
    Posts
  • DevynCJohnson
    DevynCJohnson
    Keymaster
    • Topics - 444
    • @devyncjohnson

    "fib" (Fibonacci Number Generator) is a fast and cross-platform program that can be used to find a Fibonacci number. This small program works in a command-line and can be called in shell-scripts. Currently, the program cannot find Fibonacci numbers beyond the 410th Fibonacci number. "fib" (Fibonacci Number Generator) is licensed under LGPLv3 and is made by Devyn Collier Johnson <DevynCJohnson@Gmail.com>.
    NOTE: "fib" has been merged with "Linux-Utils" (http://dcjtech.info/topic/linux-utils-software-collection-for-linux/). Updates will be released with Linux-Utils rather than this page.
    To compile the C-source-code, execute one of the below listed commands. Suggestions for other compiler commands are welcome.

    • General (GNU GCC) - gcc -O3 -gtoggle -g0 -s -pipe -pedantic -std=c11 ./fib.c -o ./fib
    • Optimized (GNU GCC) - gcc -O3 -gtoggle -g0 -s -pipe -funroll-loops -ffunction-sections -fdata-sections -std=c11 -Wl,-O3 ./fib.c -o ./fib && strip --remove-section=.note --remove-section=.comment --strip-debug --strip-unneeded ./fib

    To use the program, in a terminal, type "/PATH/TO/fib 10" to get the tenth Fibonacci number.
    Please feel free to email me suggestions, bug reports, concerns, etc. Also, please consider donating to DCJTech for further development.
    [paypal-donation]

    Download Version 2015.11.15

    http://dcjtech.info/wp-content/uploads/2015/11/fib.c.tar.gz

    Source Code

    /*
    Created by Devyn Collier Johnson
    <DevynCJohnson@Gmail.com>
    LGPLv3
    GNU Lesser General Public License v3
    Copyright (c) Devyn Collier Johnson, All rights reserved.
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3.0 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public
    License along with this library.
    */
    // gcc -O3 -gtoggle -g0 -s -pipe -pedantic -std=c11 ./fib.c -o ./fib
    // gcc -O3 -gtoggle -g0 -s -pipe -funroll-loops -ffunction-sections -fdata-sections -pedantic -Wall -Wextra -std=c11 -Wl,-O3 ./fib.c -o ./fib && strip --remove-section=.note --remove-section=.comment --strip-debug --strip-unneeded ./fib
    // Fibonacci Number Generator
    // Usage: ./fib 10  # Return the tenth Fibonacci number
    #include <stdio.h>
    #include <stdlib.h>
    
    #if defined(__linux__)
    #if !defined(_GNU_SOURCE)
    #define _GNU_SOURCE
    #endif
    #include <unistd.h>
    #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
    #define _POSIX_C_SOURCE = 199309L
    #include <unistd.h>
    #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
    #include <sys/param.h>
    #if defined(BSD)
    #define _XOPEN_SOURCE_EXTENDED
    #endif
    #endif
    #endif
    
    // static const char __author__[32] = "Devyn Collier Johnson";
    // static const char __copyright__[8] = "LGPLv3";
    // static const char __version__[16] = "2015.11.15";
    unsigned int fib(const unsigned int num);
    
    unsigned int fib(const unsigned int num) {
        // Fibonacci Number Generator
        unsigned int i;
        unsigned int fval[num+1];
        if (num > 410) {
            printf("ERROR! Number too large.\n");
            return 0;
        } else {
            if (num == 0) {
                return 0;
            } else if (num == 1 || num == 2) {
                return 1;
            } else {
                fval[0] = 0;
                fval[1] = 1;
                fval[2] = 1;
                fval[3] = 2;
                fval[4] = 3;
                for (i = 2; i <= num; i++) {
                    fval[ i ] = fval[ i - 1 ] + fval[ i - 2 ];
                }
                return fval[num];
            }
        }
    }
    
    int main(int argc, char *argv[]) {
        printf("%u\n", fib((unsigned int) strtol(argv[1], (char **)NULL, 10)));
        return 0;
    }

     

Viewing 1 post (of 1 total)