Last active
February 23, 2023 18:43
-
-
Save Softwave/c8cb649b077eaf2d62c2fcde4d30e8d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Calculate the nth Fibonacci number. | |
example usage: ./fib 50 | |
(Calculates the 50th fibonacci number) | |
(Do what you want with this) | |
https://opensource.org/license/0bsd/ | |
Zero-Clause BSD | |
=============== | |
Permission to use, copy, modify, and/or distribute this software for | |
any purpose with or without fee is hereby granted. | |
THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL | |
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | |
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE | |
FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY | |
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN | |
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
*/ | |
// Calculate Fibonacci Numbers | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <ctype.h> | |
#include <gmp.h> | |
long limit, i = 0; | |
int main(int argc, char *argv[]) | |
{ | |
// Get User Input | |
if (argc != 2) | |
{ | |
printf("Improper input. Exiting.\n"); | |
return -1; | |
} | |
limit = strtol(argv[1], NULL, 10); | |
// Setup GMP | |
mpz_t a, b, c; | |
mpz_init_set_ui(a, 1); | |
mpz_init_set_ui(b, 0); | |
mpz_init(c); | |
for (i = 0; i < limit; i++) | |
{ | |
// Perform the Fibonacci Calculation | |
mpz_add(c, a, b); | |
mpz_set(a, b); | |
mpz_set(b, c); | |
} | |
// Print the results to stdout | |
printf("Fibonacci Number %ld: ", i); | |
mpz_out_str(stdout, 10, b); | |
printf("\n"); | |
// Cleanup | |
mpz_clear(a); | |
mpz_clear(b); | |
mpz_clear(c); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment