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
    What language are you using? As I mentioned in comments to the answers, your problem becomes slightly easier if your environment treats `0` and `1` as Booleans...2011-09-10
  • 0
    I'm on Javascript2011-09-10
  • 1
    JS, huh? Well then: `(x < 11 ? 0 : (10*Math.floor((x-1)/10)+1))` ...2011-09-10
  • 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
    I have no idea of what you mean. But it looks like cheating to me. A magical function that get's rid of the different range problem. You sure there's no simpler function?2011-09-10
  • 0
    If OP is working in a C-ish language, one could formally replace the unit step functions with Iverson brackets...2011-09-10
  • 2
    @eduardo: What's cheating here? Your function is piecewise-defined, and unit step functions are used for representing piecewise-defined functions among other things...2011-09-10
  • 0
    Sorry I was just checking. I'm not a pro mthematician like you guys. But I'm still trying to understand this function. The $\sum$ is puzzling me. I think I'll go with 2 separate functions, one for the first range and one for the second.2011-09-10
  • 0
    @eduardo: The $\sum$ means you can use a `for` loop or `while` loop, as the case may be...2011-09-10
  • 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
    Yup, the inline conditional would be best, but I'd have done `(x <= 10 ? 0 : 1 + (x-1)/10*10)` or something...2011-09-10
  • 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
    This is exactly what is driving me crazy. The first range doesn't match. But the question is correct.2011-09-10
  • 2
    If you multiply $f(x)$ with $[x-11 \geq 0]$ where $[p]$ is an Iverson bracket, then his problem's solved.2011-09-10
  • 0
    I had to google about the Iverson bracket. But it seems good. http://en.wikipedia.org/wiki/Iverson_bracket2011-09-10