collatzdr.racket# Dr. Racket: saving intermidiate results - Collatz Conjecture

I am new into coding and got interested in Dr. Racket and I am facing my first problem now. I created this code:

```
(define (collatz n)
(cond ((= n 1) 1)
((> n 1) (collatz_helper n))))
(define (collatz_helper n)
(if (even? n)
(collatz (/ n 2))
(collatz (+ (* 3 n) 1))))
(collatz 100) ;; >1
```

Is it possible to store all the intermidiate results in a list or something and then print them out. I mean by intermidiate results n/2 or 3n+1 until we get 1.

For example n=100 (100, 50, 25, 76, 38, 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1)

Can someone give me a clue or show me how to implement something like this?

Solution

First, you can merge your two functions into one, using `cond`

to check all three cases:

```
(define (collatz n)
(cond
((= n 1) 1)
((even? n)
(collatz (/ n 2)))
(else
(collatz (+ (* 3 n) 1)))))
```

Then, to build the list of intermediate values, you can cons `n`

with each recursive step, creating a list whose first element is the original n and last element is 1 (assuming it terminates):

```
(define (collatz n)
(cond
((= n 1) '(1))
((even? n)
(cons n (collatz (/ n 2))))
(else
(cons n (collatz (+ (* 3 n) 1))))))
```

For example,

```
(collatz 100)
=> '(100 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1)
```

- Longest Collatz Sequence
- When to use the bitwise and operator (&)?
- Collatz sequence
- How to fix incompatible operand with BigInteger and int
- Stuck in Loop Collatz Conjecture Attempt in C
- How can I shorten a function which outputs the chain numbers of the Collatz conjecture?
- Different ways of expressing collatz conjecture in prolog fail
- How can I write Collatz conjecture by SymPy?
- Using a counter in a python function
- How do I make it so it can loop?
- Collatz Sequence - getting a None at the end
- max collatz sequence python 3
- Implementing the collatz function using Python
- Maple Sequence Length
- MATLAB Collatz plot
- I don't get a print result of function when using range
- Unable to find a way to break a sequence
- Writing a collatz program, wish to repeat process while n == 1
- Maximum Collatz length in Python, but output is not correct
- Why does this code not work with the integer 113383?
- Code to simulate Collatz Conjecture is showing 1 as an even number
- What is wrong with my "Collatz conjecture"?
- Collatz conjecture
- Collatz conjecture function in Python3 calling itself when being assigned to a variable
- How to check if sequence cycles or repeats
- Is there a way to optimise the Collatz conjecture into a branchless algorithm?
- Why is memoization of collision-free sub-chains of Collatz chains slower than without memoization?
- Trying to solve the collatz sequence project at the end of ch. 3 from "automate the boring stuff with python"
- Function to do an operation as much as possible
- strange errors while trying to create a program about Collatz conjecture. How to solve them?