highly_divisible_triangular_number.gsp 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. (ns main
  2. "github.com/jcla1/gisp/core"
  3. ; "math"
  4. "fmt")
  5. (def main (fn []
  6. (fmt/println "starting...")
  7. ; (fmt/println (divisors 76576500))
  8. (let [[target 500]]
  9. (loop [[n 2]
  10. [num 1]]
  11. (if (> (divisors num) target)
  12. ; We need this extra let, because we can't have multiple
  13. ; return values which all fmt/PrintXYZ functions have
  14. (let [] (fmt/printf "The %dth triangular (%d) was the first with more than %d divisors!\n" n num target) ())
  15. (recur (int (+ n 1)) (int (+ num n -1))))))))
  16. ; Actually not needed, generating the
  17. ; triangular numbers in the main loop
  18. (def nth-triangular (fn [n]
  19. (loop [[acc 1.0]
  20. [next n]]
  21. (if (>= 1 next)
  22. (int acc)
  23. (recur (+ acc next) (- next 1))))))
  24. (def divisors (fn [n]
  25. (loop [[start (float64 (assert int n))]
  26. [acc 1.0]]
  27. (if (<= start 1)
  28. (int acc)
  29. (if (= 0 (mod (assert int n) (int start)))
  30. (recur (- start 1) (+ 1 acc))
  31. (recur (- start 1) acc))))))