4

I have a function defined in a computer program that I would like to study mathematically:

$y = \frac{x}{2} + (\frac{5}{2}x + 1)*(x\%2)$

This function is used on strictly positive integers and also returns integers only.

The main issue is that it's using the modulo operator ($\%$) as part of the function calculation, not as in a modular equation, and I have no clue how to translate into a formula that is suitable for mathematical analysis...

I have found various topic related to modulo/floor and how to translate them into mathematical form:

But none of them seems to match this particular situation.

Also, to clarify what I mean by "studiy mathematically", it would be for example to determine the range of output values if input values are taken in a given range, or to determine limits/periodicity if this function was called recursively.

Bill Dubuque
  • 272,048
Flo
  • 53
  • Which programming language are you using? – John_dydx Nov 08 '20 at 13:00
  • It was in C++, I do not see how this would impact the answer though as the modulo operator should always have the same result on positive integers, independently of the language :) – Flo Nov 08 '20 at 13:06
  • I hope you find a solution to it! Sorry I can't help you there. – John_dydx Nov 08 '20 at 13:07
  • $x%2=x-k\cdot2$, where $0<k<x$. Then $y=y(x,k) = \frac{x}{2} + (\frac{5}{2}x + 1)\cdot(x-k\cdot2)$. – Dmitry Ezhov Nov 08 '20 at 13:12
  • @Dmitry That sounds correct, but there is even more conditions on $k$ than $0<k<x$, not all integers in this range would lead the the correct result, only the highest integer so that $k \cdot 2 \leq x$ – Flo Nov 08 '20 at 13:40
  • $n$%$2 = (1-(-1)^n)/2.$ – Somos Nov 08 '20 at 13:48

1 Answers1

2

x % 2 is equivalent to $\frac{1-(-1)^x}{2}$. On positive integer $x$, x % 2${}\in \{0,1\}$. (Some programming languages do stupid things for negative $x$. Example.) Consequently, you have $$ y = \begin{cases} x/2 &, \text{$x$ even} \\ 3x + 1 &, \text{$x$ odd} \end{cases} \text{.} $$ The Collatz conjecture covers the study of this function under recursion.

Eric Towers
  • 67,037
  • Okay this helped a lot, so the new expression becomes

    $y = \frac{x}{2} + (\frac{5}{2}x + 1)*(\frac{1-(-1)^x}{2})$

    This can harldy be generalized for any kind of modulo, but at least it helps with my problem so I will mark this as the accepted answer for now.

    – Flo Nov 08 '20 at 22:01
  • 1
    @Flo : The Question does not ask for a version for general modulus. Try $$ x \pmod{n} = \frac{n-1}{2} - \frac{n \arctan(\cot(\pi (x+1/2) / n))}{\pi} \text{.} $$ – Eric Towers Nov 09 '20 at 13:21