RosettaCodeData/Task/Tau-function/Fennel/tau-function.fennel
2026-04-30 12:34:36 -04:00

45 lines
1.4 KiB
Fennel

(do ;;; find the count of the divisors of the first 100 positive integers
; returns the number of divisors of v
(fn divisor-count [v]
(var (total n) (values 1 v))
; Deal with powers of 2 first
(while (= 0 (% n 2))
(set total (+ total 1))
(set n (math.floor (/ n 2)))
)
; Odd prime factors up to the square root
(var p 1)
(while (do (set p (+ p 2))
(<= (* p p) n)
)
(var count 1)
(while (= 0 (% n p))
(set count (+ count 1))
(set n (math.floor (/ n p)))
)
(set total (* total count))
)
; If n > 1 then it's prime
(when (> n 1)
(set total (* total 2))
)
total
)
(do
(local limit 100)
(io.write (.. "Count of divisors for the first " limit " positive integers:\n"))
(for [n 1 limit]
(io.write (string.format "%3d%s"
(divisor-count n)
(if (= 0 (% n 20))
"\n"
;else
" "
)
)
)
)
)
)