Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Problem 122

  • Loading branch information...
commit d7fcbfff0cd59b2c3691293ff35bb2043b409f68 1 parent b407923
@cushon authored
Showing with 28 additions and 1 deletion.
  1. +26 −0 122.rkt
  2. +2 −1  test-runner.rkt
View
26 122.rkt
@@ -0,0 +1,26 @@
+#lang racket
+
+; Find the sum of the minimum number of multiplications required to calculate
+; x^n for 1 <= n <= 200.
+
+(define (solve)
+
+ (define cap 200)
+
+ (define (it n pieces depth best)
+ (if (> n cap)
+ best
+ (foldl
+ (lambda (x best)
+ (if (<= (add1 depth) (hash-ref best (+ x n) +inf.0))
+ (it (+ x n) (cons n pieces) (add1 depth)
+ (hash-set best (+ x n) (add1 depth)))
+ best))
+ best
+ (cons n pieces))))
+
+ (define best (it 2 '(1) 1 (make-immutable-hash '((1 . 0) (2 . 1)))))
+
+ (foldl + 0 (map cdr (filter (lambda (x) (<= (car x) 200)) (hash->list best)))))
+
+(provide solve)
View
3  test-runner.rkt
@@ -121,7 +121,8 @@
(117 100808458960497)
(118 44680)
(119 248155780267521)
- (120 333082500)))
+ (120 333082500)
+ (122 1582)))
(define (test-problem p s)
(printf "Test ~a\t" p)
Please sign in to comment.
Something went wrong with that request. Please try again.