Created
August 26, 2014 15:51
-
-
Save kittinan/bea0120b0864b1c4d9e8 to your computer and use it in GitHub Desktop.
projecteuler.net : 25. 1000-digit Fibonacci number
This file contains 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
/* | |
* projecteuler.net | |
* 25. 1000-digit Fibonacci number | |
* | |
* compile | |
* g++ 25.cpp -o 25 -lgmp -lgmpxx | |
*/ | |
#include <cstdlib> | |
#include <iostream> | |
#include <gmp.h> | |
#include <gmpxx.h> | |
int main() { | |
mpz_t sum, last_num, current_num; | |
mpz_inits(sum, last_num, current_num, NULL); | |
mpz_set_str(last_num, "1", 10); | |
mpz_set_str(current_num, "1", 10); | |
unsigned long long int count = 2; | |
unsigned long long int i = 3; | |
while (1) { | |
//std::cout << last_num << " + " << current_num; | |
mpz_add(sum, last_num, current_num); | |
mpz_init_set(last_num, current_num); | |
mpz_init_set(current_num, sum); | |
//std::cout << " = " << sum << "\n"; | |
i++; | |
size_t sz = mpz_sizeinbase (sum, 10); | |
if (sz == 1000) { | |
break; | |
} | |
} | |
size_t sz = mpz_sizeinbase (sum, 10); | |
std::cout << sum << " | " << sz << " | "<< i << "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment