[ prog / sol / mona ]

prog


[challenge] Inverse Factorial

6 2021-06-03 19:10
#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

22


VIP:

do not edit these