MuPAD
Pro Computing Essentials - Examples
Example 9.1 - Checking Factorials
How to check if a given integer n is the factorial of another integer?
Here is the procedure doing it.
isfactorial:= proc(n:Type::PosInt)
local i;
begin
i:=2;
repeat
if (n mod i = 0) and (n div i <>1) then
n:=n div i;
i:=i+1
elif i=n then
return(TRUE)
else
return(FALSE)
end
until FALSE end
end:
isfactorial(479001600)
true
12!
479001600
Now, we can use the procedure in a more bulky way. Let
us check which numbers starting from 1 to 1000000 are
factorials.
for n from 1 to 1000000 do
if isfactorial(n) then
print(Unquoted,expr2text(n)." is factorial number")
end
end:
2 is factorial number
6 is factorial number
24 is factorial number
120 is factorial number
720 is factorial number
5040 is factorial number
40320 is factorial number
362880 is factorial number
|