2
$\begingroup$

I'm trying to find a function. And although it seems to be very simple at first I can't figure it out. Maybe I just need some sleep, and maybe someone could help me out.

given an Integer x between 0 and 100: if x is between 0-10 then f(x)=0 if x is between 11-20 then f(x)=11 if x is between 21-30 then f(x)=21 if x is between 31-40 then f(x)=31 ... if x is between 91-100 f(x)=91 

I'm trying to find the simplest f possible.

The best I could do is:

f(x) = x / 10 * 10 

But it's not right.

What function is f?

EDIT

I'm accepting @Didier's solution but I'm going with this one instead.

$f(x) = \begin{cases} 1+10\lfloor (x-1)/10 \rfloor & \mbox{if } x >10; \\ 0 & \mbox{if } x \leq 10 \end{cases}$

  • 0
    P.S. [tag:generating-functions] is for something quite different...2011-09-10

4 Answers 4

3

Let $H$ denote the Heaviside step function, using the convention that $H(x)=1$ if $x\ge0$ and $H(x)=0$ if $x<0$. Then, for every $x$, $ f(x)=H(x-11)+10\,\sum\limits_{k=1}^9H(x-1-10\,k). $ An equivalent formulation, based on Iverson bracket, is $ f(x)=[x\ge11]+10\,\sum\limits_{k=1}^9[x\ge1+10\,k]. $ Still equivalently, but less rigorously, $ f(x)=[x\ge11]+10\,[x\ge11]+10\,[x\ge21]+10\,[x\ge31]+\cdots+10\,[x\ge91]. $

  • 0
    @J.M. Thanks for mentioning Iverson brackets.2011-09-10
2

In C notation,

(x > 10 ? 1 : 0)*((x-1)/10*10 +1) 
  • 0
    This is very useful, thanks for providing this, I'm accepting Didier's because it's more in line with my question about a single function to solve the case. Though your solution is better in pratical terms to me.2011-09-10
0

$f(x) = 1+10\lfloor (x-1)/10 \rfloor$ works, except for the first range. Are you sure that $f(x)$ is not $1$ for $0 \le x \le 10$?

  • 0
    yes I'm sure. But thanks for the effort.2011-09-10
0

The function would be $f(x)=10\left\lfloor\frac{x-1}{10}\right\rfloor+1$ where $\lfloor \;\;\;\rfloor$ denotes the floor function, except that the definition for $0\leq x\leq 10$ does not match the pattern of the other parts of the definition - if you defined $f(x)=1$ for $1\leq x\leq 10$ and $f(0)=-9$, then the above function would be correct for all inputs.

  • 0
    I had to google about the Iverson bracket. But it seems good. http://en.wikipedia.org/wiki/Iverson_bracket2011-09-10