Left factorials: Difference between revisions

→‎{{header|Racket}}: tightened up a bit (especially the dnl's)
(=={{header|Racket}}== implementation added)
(→‎{{header|Racket}}: tightened up a bit (especially the dnl's))
Line 346:
 
<lang racket>#lang racket
(define ! (let ((rv# (lambdamake-hash))) (λ (n) (hash-ref! memorv# n (lambdaλ () (if (zero?= n 0) 1 (* n (! (sub1- n 1)))))))))
(define !
(let ((memo# (make-hash)))
(lambda (n) (hash-ref! memo# n (lambda () (if (zero? n) 1(* n (! (sub1 n)))))))))
 
(define (!n n)
Line 354 ⟶ 352:
(for/sum ((k (in-range n))) (! k)))
 
(define !n-digits (composednl. add1s) order(for-of-magnitudeeach displayln !ns))
(dnl
 
(dln "Display the left factorials for:"
(define (dln . s)
"zero through ten (inclusive)")
(for-each displayln s))
(pretty-printformat (for/list ((i (in-range 0 (add1 10)))) (!n i)))
 
(dln "20 through 110 (inclusive) by tens")
(dln "Display the left factorials for:"
(pretty-printformat (for/list ((i (in-range 20 (add1 110) 10))) (!n i)))
"zero through ten (inclusive)")
(dln "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-printformat (for/list ((!i-len (sequence-map !n-digits (in-range 1000 10001 1000)))) (add1 (order-of-magnitude (!n i-len))))))</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}}
569

edits