Tuesday, February 21, 2012

AutoLISP reverse problem

Here is a AutoLISP reverse problem. It is reverse problem as I don’t ask you for the solution but rather like to see if you instead can find what problem this is a solution for.

(defun h (n / c)
  (cons n (reverse (cons 1 
  (while (/= 1 (setq n (if (= (rem n 2) 0) 
  (/ n 2) (+ (* 3 n) 1)))) 
  (setq c (cons n c))))))
)

The exercise is to tell me what this function does and possibly refactor it. Leave a comment (comments with solutions will be posted later on).

UPDATE:

The Collatz conjecture is a conjecture in mathematics named after Lothar Collatz, who first proposed it in 1937. The conjecture is also known as the 3n + 1 conjecture, the Ulam conjecture (after Stanisław Ulam), Kakutani's problem (after Shizuo Kakutani), the Thwaites conjecture (after Sir Bryan Thwaites), Hasse's algorithm (after Helmut Hasse), or the Syracuse problem; the sequence of numbers involved is referred to as the hailstone sequence or hailstone numbers, or as wondrous numbers.

Take any natural number n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process (which has been called "Half Or Triple Plus One", or HOTPO) indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1. The property has also been called oneness.

No sequence has been found so far that does not reach the number 1.

The code above (not formatted to make it harder for you to read) will return the hailstone sequence for any given number.

See Collatz conjecture for more on the topic.


Some of the latest blog posts

Subscribe to RSS headline updates from:
Powered by FeedBurner