What function can be used used to map all possible integers from a large interval to a smaller one?

130 Views Asked by At

I want to find a function $f(t)$ that is able to squeeze all integers from a large interval, to a smaller one.

Suppose we have the closed interval $[0, 50M]$. I want to find a function that maps all integers from that interval to the interval $[100, 10K]$.

1

There are 1 best solutions below

4
On BEST ANSWER

You haven't been very specific about the properties that you require. For instance, the constant function defined by $f(n)=999$ for all $n\in[0,50000000000]$ is such a function, but I don't expect it's what you want.

So I assume you want a function $f[a,b]\to[c,d]$ that increases more or less uniformly on $[a,b]$, and satisfies $f(a)=c$ and $f(b)=d$. Here is one such:

$$f(n)=c+\left\lfloor\frac{d-c}{b-a}(n-a)\right\rfloor$$

where $\lfloor x\rfloor$ denotes the largest integer $\le x$.

Now just put $a=0,b=50000000, c=100,d=10000$.

All "reasonable" functions will look something like this. You can add a constant $u$ to get $$f(n)=c+\left\lfloor\frac{d-c}{b-a}(n-a)+u\right\rfloor$$

where $u$ is any real number in $[0,1)$, and $f$ still satisfies the required properties; choosing $u=\frac12$ gives the best fit to the straight line from $(a,c)$ to $(b,d)$.

Note: If you are programming this, and $a,b,c,d$ are positive integer variables, it is important to evaluate $\left\lfloor\frac{d-c}{b-a}(n-a)\right\rfloor$ as ((d-c)*(n-a))/(b-a), and not as ((d-c)/(b-a))*(n-a).