sicp-solutions/ex-1.23.scm
Petar Kapriš a697f52405 Add solutions to exercises from section 1.2
To be noted: the drawing in exercise 1.14 is unfinished. I did it in a
notebook, but haven't yet had the time to put it in a txt file.
2025-02-05 13:59:25 +01:00

26 lines
709 B
Scheme

#lang sicp
(define (square x) (* x x))
;(define (next x)
; (if (= x 2)
; 3
; (+ x 2)))
(define (next x) (+ x 1))
(define (smallest-divisor n)
(find-divisor n 2))
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (next test-divisor)))))
(define (divides? a b)
(= (remainder b a) 0))
(define (time-sd n)
(define start-time (runtime))
(smallest-divisor n)
(- (runtime) start-time))
; comparing (time-sd 100069) with the new and old versions
; it's roughly a 1.5 factor increase, likely because the if
; statement checking is slightly more expensive than simply increasing
; by one