Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
40
Task/Knuths-power-tree/Racket/knuths-power-tree.rkt
Normal file
40
Task/Knuths-power-tree/Racket/knuths-power-tree.rkt
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#lang racket
|
||||
|
||||
(define pow-path-cache (make-hash '((0 . (0)) (1 . (0 1)))))
|
||||
|
||||
(define pow-path-level '(1))
|
||||
|
||||
(define (pow-path-extend!)
|
||||
(define next-level
|
||||
(for*/fold ([next-level '()])
|
||||
([x (in-list pow-path-level)]
|
||||
[y (in-list (pow-path x))]
|
||||
[s (in-value (+ x y))]
|
||||
#:when (not (hash-has-key? pow-path-cache s)))
|
||||
(hash-set! pow-path-cache s (append (hash-ref pow-path-cache x) (list s)))
|
||||
(cons s next-level)))
|
||||
(set! pow-path-level (reverse next-level)))
|
||||
|
||||
(define (pow-path n)
|
||||
(let loop ()
|
||||
(unless (hash-has-key? pow-path-cache n)
|
||||
(pow-path-extend!)
|
||||
(loop)))
|
||||
(hash-ref pow-path-cache n))
|
||||
|
||||
(define (pow-tree x n)
|
||||
(define pows (make-hash `((0 . 1) (1 . ,x))))
|
||||
(for/fold ([prev 0])
|
||||
([i (in-list (pow-path n))])
|
||||
(hash-set! pows i (* (hash-ref pows (- i prev)) (hash-ref pows prev)))
|
||||
i)
|
||||
(hash-ref pows n))
|
||||
|
||||
(define (show-pow x n)
|
||||
(printf "~a: ~a\n" n (cdr (pow-path n)))
|
||||
(printf "~a^~a = ~a\n" x n (pow-tree x n)))
|
||||
|
||||
(for ([x (in-range 18)])
|
||||
(show-pow 2 x))
|
||||
(show-pow 3 191)
|
||||
(show-pow 1.1 81)
|
||||
Loading…
Add table
Add a link
Reference in a new issue