Write a program that takes an integer m as input and returns the integer n such that n! = m. It should accept m with up to one million digits and print an error message if m isn't the factorial of a non-negative integer.
Meh.
(defun infac (n)
(loop for i from 0
for f = (fac i)
when (> f n) do (error "Not a factorial")
when (= f n) return i))
>>2
How long to compute (infac (fact 1000000))
?
(define (inverse-factorial n)
(letrec ((iter
(lambda (i a)
(cond
((> a n) (error "Not a factorial!"))
((= a n) (- i 1))
(else (iter (+ i 1) (* a i)))))))
(iter 1 1)))
>>4
How long to compute (inverse-factorial (factorial 1000000)) ?
#lang racket ;;; v7.9
(require math/special-functions)
(require math/base)
(define (fact n) (gamma (+ 1 n)))
(define (invfact n)
(define w (log n))
(define c (/ (log (* 2 pi)) -2))
(define (¡ xₙ)
(let ((xₙ₊₁ (improve xₙ)))
(if (< (abs (- xₙ xₙ₊₁)) 0.001)
(check (exact-round xₙ₊₁))
(¡ xₙ₊₁))))
(define (improve x) (- (/ (+ x w c) (log x)) 0.5))
(define (check res)
(if (= n (fact res))
res
"not a factorial"))
(if (< n 3)
n
(¡ 10)))
a few seconds
>>5
It is linear in time if we ignore that bignum operations. Wikipedia says that the best known multiplication algorithm is O(n log(n)), therefore theoretically it could be O(n² log(n)), but it will depend on your Scheme implementation.
What are some cases where 1000000! is interesting? I thought 70! was already well over the number of observable particles in the universe, why would you need to compute and reverse it?
>>7
I believe it's absolutely useless but I don't see a problem there.
>>2
Finite time.
>>6
and invfact(fact(10000000)) takes 20+20 seconds! Is this Newton's iterative method?
>>10
Yes. It converges very quickly.
Remember that
n! = 1 * 2 * ... * (n - 2) * (n - 1) * n
Then starting with 2 one should go up until m is factored, otherwise it's not a factorial.
This method is O(n) or O(sqrt(m)), but I'm not sure if there are better way to do this.
>>12
If the remainder from division is not 0 it returns earlier.
This method like >>2,4 fails miserably if m is a very large factorial like fact(1000000).
>>13
As long as it gives you the right result in finite time, it hasn't failed.
>>13
Undeniable proof that it actually does work for any positive integer: https://paste.textboard.org/3df10a35
>>15
How do you prove the proof contains no flaws?
>>16
Type checking!
>>15-17
The real question is always: did we actually prove what we wanted to prove? Embarrassingly the paste only proves that the inverse of n! is n. But, for example, it says nothing about the inverse of n!-1 which could also be n. Here's an extended version that also proves that if the inverse of m is n, then m must be n!: https://paste.textboard.org/399b89fa
>>18 That's the trick with theorems and automated tests and such.
You're trying to approach the thing from multiple angles.
In math maybe you start with a conjecture that's something explicit then you come around with an implicit proof.
Software is the same.
For later.
>>> import operator
>>> import functools
>>> f = lambda n: len (str (functools.reduce (operator.mul, range (1, n + 1))))
>>> f (205021)
999994
>>> f (205022)
1000000
>>20
Stop adding spaces before opening parentheses.