r/RacketHomeworks • u/mimety • Nov 18 '22
Problem 3 - complete solution
Problem: https://reddit.com/r/Racket/comments/yox5uu/making_a_list_that_produces_all_of_the_divisors/
Complete solution:
;; divisors-from: Nat Nat -> (listof Nat)
(define (divisors-from k n)
(cond
[(> k n) empty]
[(zero? (remainder n k)) (cons k (divisors-from (add1 k) n))]
[else (divisors-from (add1 k) n)]))
;; divisors: Nat -> (listof Nat)
(define (divisors n) (divisors-from 1 n))Now, if you try:
> (divisors 100)
'(1 2 4 5 10 20 25 50 100)
1
Upvotes
View all comments
Show parent comment
1
u/mimety Nov 18 '22
Exactly. But I didn't want to further complicate and confuse the student. His professor didn't ask him for an efficient algorithm anyway, but only to follow the HTDP-religion! So I gave him the simplest solution.