Created
January 24, 2014 21:08
-
-
Save mrklein/8606466 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
#include <iostream> | |
#include <ctime> | |
#include <gmpxx.h> | |
typedef mpz_class Integer; | |
Integer N[] = {1, 12, 123, 1234, 12345, 123456}; | |
Integer sum_digits(Integer n); | |
Integer factorial(Integer n); | |
int main(int argc, char *argv[]) | |
{ | |
Integer res = 0; | |
std::clock_t start = std::clock(); | |
for(int i = 0; i < sizeof(N)/sizeof(Integer); ++i) | |
{ | |
res += sum_digits(factorial(N[i])); | |
} | |
std::cout << res << std::endl; | |
std::clock_t end = std::clock(); | |
std::cout << "Execution time: " | |
<< 1e3 * ((double) (end - start)) / CLOCKS_PER_SEC | |
<< " ms" << std::endl; | |
} | |
Integer factorial(Integer n) | |
{ | |
Integer res = 1; | |
for(Integer k = n; k > 1; k -= 1) | |
res *= k; | |
return res; | |
} | |
Integer sum_digits(Integer n) | |
{ | |
Integer res = 0; | |
std::string s = n.get_str(); | |
for(auto i = s.begin(); i != s.end(); ++i) | |
res += (int) *i - (int) '0'; | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment