;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "2017-fall-reader.rkt" "csc104")((modname Mon-Collatz) (compthink-settings #hash((prefix-types? . #f))))
; Collatz sequence
; Takes a positive number
; If it's even, divide by 2
; If it's odd, multiply by 3 and add 1
; Repeat
; Collatz conjecture
; For any positive number, the Collatz sequence
; starting at that number will eventually reach 1
; Let's write a function to generate the next step in
; a Collatz sequence
; Documentation
(check-expect (c-step 12) 6)
(check-expect (c-step 5) 16)
; Design
(check-expect (c-step 12) (/ 12 2))
(check-expect (c-step 5) (+ (* 5 3) 1))
(define (c-step n)
(cond [(even? n) (/ n 2)]
[else (+ (* n 3) 1)]))
#;(repeated c-step 12 20)
; Question: given a number, how many steps does it
; take to get to 1?
(check-expect (number-of-steps 12) 10)
(check-expect (number-of-steps 5) 6)
(check-expect (number-of-steps 12)
(+ 1 (number-of-steps 6)))
(check-expect (number-of-steps 5)
(+ 1 (number-of-steps 16)))
(check-expect (number-of-steps 12)
(+ 1 (number-of-steps (/ 12 2))))
(check-expect (number-of-steps 5)
(+ 1 (number-of-steps (+ (* 5 3) 1))))
(define (number-of-steps n)
(cond [(equal? n 1) 1]
[else (+ 1 (number-of-steps (c-step n)))]))
#; (maximum 1 2 3 4 5)
(apply maximum (map number-of-steps (range 1 100000 1)))
(define (reaches-1? n)
(cond [(equal? n 1) #true]
[(even? n) (reaches-1? (/ n 2))]
[else (reaches-1? (+ (* n 3) 1))]))
; no case where we can say for sure that no, we can't
; reach 1 from this n
; The Halting Problem
; "Snooping the Loop Snooper"
; proof of the Halting Problem in the style of Dr Seuss