p076.scm 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. ;; Counting summations
  2. (define-module (unsolved p076))
  3. (define-public (counting-summation limit)
  4. (1- (coin-partitions limit)))
  5. ;; Borrowed from p078
  6. (define (coin-partitions n)
  7. (define (pile-loop pile left partitions)
  8. (cond
  9. [(= 1 pile) (1+ partitions)]
  10. [(zero? left)
  11. (pile-loop (1- pile) (1+ left) (1+ partitions))]
  12. [else (let ([next-pile (min left pile)])
  13. (pile-loop
  14. (1- pile) (1+ left)
  15. (pile-loop next-pile (- left next-pile) partitions)))]))
  16. (pile-loop n 0 0))
  17. ;; This is not correct, and not very useful, since the cache isn't helping much
  18. (define (coin-partitions-cache n)
  19. (define partition-cache (make-vector (1+ n) #f))
  20. (define (pile-loop pile left partitions)
  21. (cond
  22. [(= 1 pile) (1+ partitions)]
  23. [(zero? left)
  24. (let ([cached-v? (vector-ref partition-cache pile)])
  25. (if cached-v? (begin (display "cached: ") (display cached-v?) (+ cached-v? partitions))
  26. ;; can i do tail recursion here?
  27. (let ([part-for-pile
  28. (pile-loop (1- pile) (1+ left) (1+ partitions))])
  29. (vector-set! partition-cache pile part-for-pile)
  30. part-for-pile)))]
  31. [else (let ([next-pile (min left pile)])
  32. (pile-loop
  33. (1- pile) (1+ left)
  34. (pile-loop next-pile (- left next-pile) partitions)))]))
  35. (pile-loop n 0 0))