public
Created

  • Download Gist
ilog.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
int
ilog ( int b , int n )
{
int lo=0 , b_lo=1 , hi=1 , mid , b_mid ;
long long b_hi=b ;
while ( b_hi < n )
{ lo = hi ; b_lo = b_hi ; hi = hi*2 ; b_hi = b_hi * b_hi ; }
while( hi-lo > 1 )
{
mid = (lo + hi) / 2 ;
b_mid = b_lo * (int) pow( b , mid - lo ) ;
 
if ( n < b_mid ) { hi = mid ; b_hi = b_mid ; }
if ( n > b_mid ) { lo = mid ; b_lo = b_mid ; }
if ( n == b_mid ) return mid ;
}
 
return b_hi == n ? hi : lo ;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.