-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathhighly_divisible_triangular_number.gsp
More file actions
34 lines (31 loc) · 1.09 KB
/
Copy pathhighly_divisible_triangular_number.gsp
File metadata and controls
34 lines (31 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(ns main
"github.com/jcla1/gisp/core"
; "math"
"fmt")
(def main (fn []
(fmt/println "starting...")
; (fmt/println (divisors 76576500))
(let [[target 500]]
(loop [[n 2]
[num 1]]
(if (> (divisors num) target)
; We need this extra let, because we can't have multiple
; return values which all fmt/PrintXYZ functions have
(let [] (fmt/printf "The %dth triangular (%d) was the first with more than %d divisors!\n" n num target) ())
(recur (int (+ n 1)) (int (+ num n -1))))))))
; Actually not needed, generating the
; triangular numbers in the main loop
(def nth-triangular (fn [n]
(loop [[acc 1.0]
[next n]]
(if (>= 1 next)
(int acc)
(recur (+ acc next) (- next 1))))))
(def divisors (fn [n]
(loop [[start (float64 (assert int n))]
[acc 1.0]]
(if (<= start 1)
(int acc)
(if (= 0 (mod (assert int n) (int start)))
(recur (- start 1) (+ 1 acc))
(recur (- start 1) acc))))))