Created
November 19, 2017 12:16
-
-
Save izanbf1803/4538a7cb1d59076dbd85867f5b5f43db to your computer and use it in GitHub Desktop.
Get number of digits in factorial result without compute it.
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 <cmath> | |
using namespace std; | |
typedef long long ll; | |
typedef long double ld; | |
ld factorial_digit_count(ld n) | |
{ | |
// log(ab) = log(a) + log(b) | |
ld digits = 0; | |
for (ll k = 2; k <= n; ++k) { | |
digits += log10((ld)k); | |
} | |
return floor(digits) + 1; | |
} | |
int main() | |
{ | |
// log10(n^k) = k*log10(n) | |
ld n = 19941994; | |
ld n_factorial_square_digits = 2*factorial_digit_count(n); // digits of (19941994!)^2 | |
ld n_to_n_digits = n*log10(n); // digits of 19941994^19941994 | |
cout << (ll)n_factorial_square_digits << " " << (ll)n_to_n_digits << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment