the simplest function f(1)=-1; f(2)=0; f(3)=1; f(4)=0.

486 Views Asked by At

I'm looking for a function that gives

 f(1)=-1; f(2)=0; f(3)=1; f(4)=0.

The other values are undefined and I don't pay any attention on them. The prefered functions are +, -, *, %, div and abs. (the shorter function - the better).

It could be kind of sinus with period of 4, but sinus is not amonght the functions I asked above. Needed for the programming to unite 4 cases into one (the first case we substract 1, the second and fourth we do nothing, for the third one we add one).

Thank you.

3

There are 3 best solutions below

2
On BEST ANSWER

Try the following: $$-|x-3|+1$$

An easy sine function should also work:$$-\sin(\frac{5x}{\pi})$$

0
On

Have you tried $f(x)=ax^3+bx^2 +cx+d$?

1
On

Make the ansatz $f(x) = (x-2)(x-4)(ax+b),\;$ this trivially gives the zeroes at $x=2, x=4.\;$ Then you get for $x=1,3\;$ the equations $3a+3b=-1\;$ and $-3a-b = 1.\;$ Adding gives $b=0$ which implies $a=-1/3$.

Thus the function $$f(x) = -\frac{1}{3}(x-2)(x-4)x$$ is a simple answer.