Left factorials: Difference between revisions

Content added Content deleted
(=={{header|Racket}}== implementation added)
(→‎{{header|Racket}}: tightened up a bit (especially the dnl's))
Line 346: Line 346:


<lang racket>#lang racket
<lang racket>#lang racket
(define ! (let ((rv# (make-hash))) (λ (n) (hash-ref! rv# n (λ () (if (= n 0) 1 (* n (! (- n 1)))))))))
(define !
(let ((memo# (make-hash)))
(lambda (n) (hash-ref! memo# n (lambda () (if (zero? n) 1(* n (! (sub1 n)))))))))


(define (!n n)
(define (!n n)
Line 354: Line 352:
(for/sum ((k (in-range n))) (! k)))
(for/sum ((k (in-range n))) (! k)))


(define !n-digits (compose add1 order-of-magnitude !n))
(define (dnl. s) (for-each displayln s))
(dnl

"Display the left factorials for:"
(define (dln . s)
"zero through ten (inclusive)"
(for-each displayln s))
(pretty-format (for/list ((i (in-range 0 (add1 10)))) (!n i)))

"20 through 110 (inclusive) by tens"
(dln "Display the left factorials for:"
(pretty-format (for/list ((i (in-range 20 (add1 110) 10))) (!n i)))
"zero through ten (inclusive)")
"Display the length (in decimal digits) of the left factorials for:"
(pretty-print (for/list ((i (in-range 0 (add1 10)))) (!n i)))
"1,000, 2,000 through 10,000 (inclusive), by thousands."

(pretty-format (for/list ((i (in-range 1000 10001 1000))) (add1 (order-of-magnitude (!n i))))))</lang>
(dln "20 through 110 (inclusive) by tens")
(pretty-print (for/list ((i (in-range 20 (add1 110) 10))) (!n i)))

(dln "Display the length (in decimal digits) of the left factorials for:"
"1,000, 2,000 through 10,000 (inclusive), by thousands.")
(pretty-print (for/list ((!i-len (sequence-map !n-digits (in-range 1000 10001 1000)))) !i-len))</lang>


{{out}}
{{out}}