This question refers to the number problem "3096", whose title is "overflow", made available by URI Online Judge (online programming marathon).
See here the entirety of the statement.
The question emphasizes that in many cases where it is necessary to calculate the factorial of a number, whose result has a number muito grande
of digits, possibly can result in a overflow.
Note that the question says that the program must support values N
, such that, 1<= N <= 10^8
, that is, it must bear values between 1
and 100000000
. Now, imagine how much computational resources a program would use if it had to calculate the factor of 100000000
?
To begin with, the número total de dígitos
of 100000000!
is 756570557
. To improve readability we can divide into thousands and leave it as follows 756.570.557
,that is, the total number of digits resulting from the 100.000.000
is setecentos e cinquenta e seis milhões, quinhentos e setenta mil, quinhentos e cinquenta e sete
.
How much demorado
the correct value for factors of grandes
values, the problem asks us to calculate apenas
the number of digits of the possible factorial.
In mathematics, we can calculate the number of digits of the factorial result without even having calculated the factorial of that number. To do this, simply use the formula Kamenetsky.
In order to implement this formula correctly in the Uri issue, with all the constraints that are emphasized, we must write the following code...
from math import log10, e, pi, floor
n = int(input())
num_digitos = floor((n * log10(n / e)) + (log10(2 * pi * n) / 2.0)) + 1
print('{}'.format(num_digitos))
See here the functioning of the algorithm.
Note that when we run this program, the console screen is limpa
, awaiting the typing of the value of N
. After we enter the value of N
and then press Enter
, the program completes the task.
This code has already been testado
, submetido
and properly aprovado
, in the marathon that I refer to at the beginning of the post, under programming language Python 3
.
For the record, this program only took 0.028 s
to run on the website URI
.
You mentioned the time spent, but you didn’t mention what the value of
N
that time has been calculated. What are the values ofN
that you used in your tests ?– Lacobus
@Lacobus This is a challenge from the URI. Normally, the URI tests with a lot of different inputs, and many of them are secret to prevent algorithms from being developed to try to circumvent them. Many of the inputs that typically explode algorithms made by beginners are monstrous things and/or astronomical numbers.
– Victor Stafusa
I edited the question, the test cases are for 0 < N <= 10 8
– Almir Lopes Martins
Vey, ask veya and n should need more, but
n! = Gamma(n+1)
andDigits(i) = floor(log10(i))+1 = floor(log10(exp(1))*ln(i))+1 ~ floor(0.43429448190325182765*ln(i))+1
, thereforeDigits(n!) ~ floor(0.43429448190325182765*ln( Gamma(n+1) ))+1
. Python function that calculatesln(Gamma())
isscipy.special.gammaln
.– RHER WOLF
That is, simply from the import of
math
andscipy.special
and uses the expressionmath.floor( 1+0.43429448190325182765*scipy.special.gammaln(N+1) )
or even (to avoid having to calculate the constant)math.floor( 1+math.log10( math.exp(1) )*scipy.special.gammaln(N+1) )
. That’s pretty quick calculation.– RHER WOLF