-4

I am trying to find a function that perfectly defines the first iteration of the Collatz Conjecture. Is this good enough? Or is there a better and more compact function?

$f\left(x\right)=\left(\frac{x}{2}-\operatorname{floor}\left(\frac{x}{2}\right)\right)\cdot2\cdot\left(3x+1\right)+\left(1-\left(\left(\frac{x}{2}-\operatorname{floor}\left(\frac{x}{2}\right)\right)\cdot2\right)\right)\cdot\frac{x}{2}$

2 Answers2

7

There is a better way:

$$f(x)=\begin{cases}3x+1 & \text{if } x \text{ is odd}\\ \frac x2 & \text{if } x \text{ is even}\end{cases}$$

Unlike your version, if a human is reading it, this second version is much easier to understand.

5xum
  • 126,227
  • 6
  • 135
  • 211
4

If you really want one function keep it simple

$$f(x)=(3x+1)\cdot \frac{1-(-1)^{x}}{2} + \frac{x}{2}\cdot \frac{1+(-1)^{x}}{2}$$ but again, I would personally discourage its use for the same reasons as @5xum's

b00n heT
  • 17,044
  • 1
  • 39
  • 52