Answer the question
In order to leave comments, you need to log in
Why doesn't the factorial value fit even in QWord?
Need to solve a problem outside the formula C=N!/M!*(NM)!. With a value of N=21, the value of N! does not fit anywhere, tried QWord, integer, longint, word, int64, longword errors 201 and 215. N and M are less than 50000.
Code:
function Prost(a:QWord):boolean;
var i:int64;
f:boolean;
begin
if a<2 then f:=false
else
begin
f:=true;
i:=2;
while (i*i<=a) and f do
if a mod i=0 then f:=false
else i:=i+1;
end;
Prost:=f;
end;
function fact(k: QWord) : QWord;
var
p: QWord;
i: integer;
begin
p:=1;
for i:=2 to k do
begin
p:=p*i;
End;
fact:= p;
end;
var
N, M, C, count : QWord;
i : integer;
begin
read(N);
read(M);
count:= 0;
C:= fact(N) div (fact(M) * fact(N - M));
for i:= 1 to C do
Begin
if C mod i = 0 then
Begin
if Prost(i) then inc(count);
End;
End;
write(count);
End.
Answer the question
In order to leave comments, you need to log in
Long arithmetic
is used to store very large numbers , I think you can easily google ready-made libraries.
But something tells me that in your case you need some kind of tricky algorithmic solution, because the factorial of 50000 is a dozen or two seconds on average hardware.
If you look at this formula, you can see that the entire factorial does not need to be calculated and it is slightly reduced (the beginning of the factorial N is divided by the entire factorial M).
import math
def combinations(n, m):
k = n - m
if k == 0:
return 1
result = 1
for i in range(m + 1, n + 1):
result *= i
result //= math.factorial(k)
return result
print(combinations(30, 25)) # 142506
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question