p044.scm 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. ;; Pentagon numbers
  2. (define-module (unsolved p044))
  3. (define pentagonal-list '())
  4. (set! pentagonal-list
  5. (assoc-set! pentagonal-list "1" 100))
  6. (display pentagonal-list)
  7. (define (min-pair-that-satisfies proc)
  8. (let loop ((i 2) (j 1) (future-min 1) (curr-min 0))
  9. (cond
  10. ((>= j i) (loop (1+ i) 1 (get-future-min i) curr-min))
  11. ((and (not (zero? curr-min)) (> future-min curr-min)) curr-min)
  12. (else
  13. (loop i
  14. (1+ j)
  15. future-min
  16. (let* ((p-i (pentagonal-number i))
  17. (p-j (pentagonal-number j))
  18. (p-sum (+ p-i p-j))
  19. (p-diff (abs (- p-i p-j))))
  20. (if (and (pentagonal? p-sum) (pentagonal? p-diff) (< p-diff curr-min))
  21. p-diff
  22. curr-min)))))))
  23. (define (get-future-min i)
  24. (abs (- (pentagonal-number i) (pentagonal-number (1+ i)))))
  25. (define (pentagonal? v)
  26. (let loop ((n 0))
  27. (let ((curr-pent (pentagonal-number n)))
  28. (cond
  29. ((= curr-pent v) #t)
  30. ((> curr-pent v) #f)
  31. (else (loop (1+ n)))))))
  32. (define (pentagonal-number n)
  33. (/ (* n (- (* 3 n) 1)) 2))