Skip to content

Instantly share code, notes, and snippets.

@dharmatech
Created May 22, 2010 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dharmatech/409723 to your computer and use it in GitHub Desktop.
Save dharmatech/409723 to your computer and use it in GitHub Desktop.
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 ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment