### Collatz Conjecture

Posted:

**Sat Apr 30, 2011 6:56 pm**I'm working on a New Lesson in Lisp this week and decided to tackle Collatz Conjecture.

I am going to express the code two ways. Firstly, as a 'standard' programming example and secondly as a Lisp programming example.

So, I thought I'd share the standard example and give the forums a chance to comment and optimize or convert to a more Lisp-y way of doing things.

The results from this code are:

1 10

1 10 20

100 200

100 200 125

...

I am going to express the code two ways. Firstly, as a 'standard' programming example and secondly as a Lisp programming example.

So, I thought I'd share the standard example and give the forums a chance to comment and optimize or convert to a more Lisp-y way of doing things.

Code: Select all

```
;
; http://streamtech.nl/problemset/100.html
; aka http://en.wikipedia.org/wiki/Collatz_conjecture
;
(define (collatz)
(println "Collatz Conjecture: low high (ENTER/RETURN to end)")
(until (= (set 'p (read-line)) "")
(set 'in (map int (parse p " ")) )
(set 'low (in 0) 'high (in 1))
(set 'max_count 0)
(dolist (num (sequence low high))
(set 'cnt 1)
(while (!= num 1)
(if (!= (mod num 2) 0)
(set 'num (+ (* 3 num ) 1))
(set 'num (/ num 2))
)
(inc cnt)
(if (< max_count cnt) (setf max_count cnt))
)
)
(println low " " high " " max_count)
)
)
(collatz)
```

1 10

1 10 20

100 200

100 200 125

...