Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I am doing a website and I need an algorithm. I am not so good in math, that is why I come here for help. It is, from my point, for a mathematician very easy.

Here's the deal :

IF X IS BETWEEN 0 AND 9
    Y = 2

IF X IS BETWEEN 10 AND 19
    Y = 4

IF X IS BETWEEN 20 AND 39
    Y = 8

IF X IS BETWEEN 40 AND 79
    Y = 16

IF X IS BETWEEN 80 AND 159
    Y = 32

ETC...

Anyone can help by doing a simple algorithm ? You can use any functions you want or whatever. Thanks a lot !

EDIT: It is possible to change the interval between X but it need to start low and be higher then Y and has to give similar results.

share|improve this question
    
"Algorithm", or "formula"? –  Arturo Magidin Jan 25 '12 at 22:23
    
Something that I can run on a computer. I'm really no expert. Like X + 2 or f(x) = 2(x-2) - 1. etc... you can use function or whatever you need. –  David Bélanger Jan 25 '12 at 22:24
add comment

1 Answer

up vote 4 down vote accepted

Divide $x$ by 10, multiply by 4, take the base-2 logarithm, round down to an integer $n$, then $y=2^n$. This doesn't work for $x$ between 0 and 5, so just define $y$ to be 2 in that range.

share|improve this answer
    
Here's is what I did : log(($X / 10) * 4, 2) ^ 2 Right ? The , 2 will use base 2 of log –  David Bélanger Jan 25 '12 at 22:32
    
@david: you probably mean two to the power of the thing rather than the thing to the power of 2. –  opt Jan 25 '12 at 22:33
    
Yeah so N = log2((X / 10) * 4)` so .. Y = 2^N... Ok it is working but only problem is I get a base 2 higher then what I wanted but... I just devide by 2 at the end. Working like a charm. Thanks a lot all of you ! –  David Bélanger Jan 25 '12 at 22:35
    
Final PHP Code : $Y = ceil( pow(2, log(($X / 10) * 4, 2)) / 2); –  David Bélanger Jan 25 '12 at 22:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.