def f(n,i) s=1 t=2 x=s for j=1to i-1 x=x+t if x>n then s=s*2 x=s t=t*2 endif next return x end