How to express this code mathematically?

156 Views Asked by At

Any programmer can explain how to express this code in math terms?

public static int px32(int p){
    int h = 0;
    for (int i = 0; i < p; i = i + 32) { h++; }
    return h;
}
1

There are 1 best solutions below

6
On

Summarizing the thread so far, the code above (along with a if(p <= 0) { return 0; }) computes \begin{matrix} &{\rm R\acute{o}cherz} &\text{achille hui}\\ h={}&\left\lceil\dfrac{\max\{0,p\}}{32}\right\rceil &=\left\lfloor\dfrac{\max\{0,p\}+31}{32}\right\rfloor & \qquad \text{for } p \in \mathbb{Z}. \end{matrix} However, since $32 =2^5$ is a power of $2$, perhaps the fastest way to compute $h$ is (zwim):

return (p<0) ? 0 : ((p+31)>>5);.