4

I feel like this is far simpler than I am making it. I am looking for a function $y=f(x)$ such that:
$f(1) = 1$

$f(2) = 1 $

$f(3) = 3 $

$f(4) = 3 $

$f(5) = 5 $

$f(6) = 5$

That is to say, all the evens become the preceding odd number. Does this function exist? If so, what is it? Lastly, how would a mathematician write it?

Shobhit
  • 6,902
mwjohnson
  • 301

6 Answers6

11

How about

$$f(n) = n + \frac{(-1)^{n + 1} - 1}{2}$$

  • Can you comment a bit on how were you able to recognize this relationship? What did you recognize first that made you realize the function would satisfy the values? – mwjohnson Sep 04 '13 at 05:49
  • 3
    Using things that look like $(-1)^n - 1$ are common for when you want to differentiate even / odd, since it'll be zero for even numbers and a non-zero constant otherwise. The rest of the stuff is just rescaling and adjusting it to be zero for odds. –  Sep 04 '13 at 06:03
5

$$f(x)=\left\lfloor \frac{x-1}{2}\right\rfloor+\left\lfloor \frac{x+1}{2}\right\rfloor $$

André Nicolas
  • 507,029
2

Try $$2\left\lfloor\frac{x+1}2\right\rfloor-1$$ which uses the floor function. The floor function is written as $\text{floor}(x)=\lfloor x \rfloor$, and is the greatest integer $n$ with $n\leq x$.

Mark Bennet
  • 100,194
1

I needed a similar function too recently and came up with a solution that should work here.

$$2\left\lceil\frac{x}2\right\rceil - 1$$

To summarise, Odd numbers are left unchanged here while even numbers get rounded down to the nearest odd.

And here's the counterpart for even numbers.

$$2\left\lfloor\frac{x}2\right\rfloor$$

Cubic
  • 11
  • 3
  • Apologizes for the oversight, I have updated the formula and would like to thank you for pointing it out. – Cubic May 27 '23 at 17:35
1

Hint: Have you heard of the floor function? A few transformations of that should get you what you want.

Cameron Buie
  • 102,994
1

Consulting OEIS, gives us more:

   a(n) = 2*floor(n/2) + 1.

   G.f.: (1+x+x^2+x^3)/(1-x^2)^2; - Paul Barry, Oct 14 2005

   a(n)=n+[1+(-1)^n]/2, with n>=0 - Paolo P. Lava, May 08 2007

   a(n)=2*a(n-2)-a(n-4); a(0)=1, a(1)=1, a(2)=3, a(3)=3. - Philippe DELEHAM, Nov 03 2008

   a(n)=2*n-a(n-1), with a(0)=1. - Vincenzo Librandi, Nov 13 2010
Mikasa
  • 67,374