sicp-exercises/ex-1-11.rkt
2025-01-02 17:49:30 +03:00

44 lines
1.2 KiB
Racket

#lang racket
;; for n=100
;; iterative version takes roughly 0.008 milliseconds.
;; recursive version does not seem like it will finish any time soon.
;; for n=50
;; iterative version takes roughly 0.003 milliseconds in drracket
;; recursive version has not yet finished executing after a couple minutes, and I got bored.
;; for n=25.
;; iterative version takes 0.002ms
;; recursive version takes 46.5 ms
;; yeah. that's big. okay. this shit's important.
;; I wonder how it would be with memoization.
(define-syntax-rule (meas form)
(let ([my-time (current-inexact-milliseconds)])
(let ([res form])
(- (current-inexact-milliseconds) my-time))))
(define (recursive n)
(if (< n 3)
n
(+ (recursive (- n 1))
(* 2 (recursive (- n 2)))
(* 3 (recursive (- n 3))))))
(define (iter a b c n)
(if (<= n 0)
c
(iter b c (+ c (* 2 b) (* 3 a)) (- n 1))))
(define (iter-start n)
(iter 0 1 2 (- n 2)))
(define (ex-1-11 n)
(let loop ((i n))
(if (< i 2)
#t
(if (= (recursive i) (iter-start i))
(loop (- i 1))
(begin
(display "UNEQUAL!")
(displayln i))))))