Categories
Technical

Scheme Language – basic programs set 3

(define (checkperfect num)
(define total 0)
(let loop((count 1) (end(round(/ num 2))) (temp 0))
(if(<= count end)
(begin
(if(= (modulo num count) 0)
(begin
(set! temp count)
(set! total (+ total temp))
(loop (+ count 1) end temp)
)
(loop (+ count 1) end temp)
)
)
(begin
(if(= total num)
#t
#f
)  )   )  )  )
;(checkperfect 28)

(define (findperfects start end)
(define result(list))
(let loop((count start))
(if(<= count end)
(begin
(if(checkperfect count)
(begin
(set! result (append result (list count)))
(loop (+ count 1))
)
(loop (+ count 1))
)    )
result
)   )  )
Input:
(findperfects 1 5000)
Output:
(6 28 496)

By intrepidkarthi

I am Karthikeyan, working as a software professional, interested in Travelling, Bike riding, PC Gaming, Reading Novels, Blogging, Writing thamizh poems.

1 reply on “Scheme Language – basic programs set 3”

Leave a Reply

Your email address will not be published.