Convert for loop into math sigma notation Have some code (written in C), need the equivalent notation for mathematics.
Is it possible using if-statements and sigma notation for this, and if so, how?

int res = 0;
for (int i=0; i &lt; (N/2)+1; i=i+1)
  if (N % pow(2, i) == 0) res = i;


I have gotten as far as:

$$ \sum _{i=0}^{{N\over2}+1}\:(N \mod 2^i )$$ (missing the == 0 and res = i)

Ideas or a solution are welcome.
Let me define
$$f(x) = \begin{cases} 0 &amp; x \ne 0 \\
 1 &amp; x = 0\end{cases}$$
Now it is
$$ \sum _{i=0}^{{N\over2}+1}\:f(N \text { mod } 2^i)$$
The code calculates how many times number is divisible by two (which by the way could be simplified to checking number of trailing zeros in binary representation, say hacky method x &amp; -x).

You can define {N, 2} for the highest power of a prime 2 that divides N, and it is the valid notation. There are other approaches and notations of greatest power of two dividing N at MathOverflow.