Created
May 22, 2010 03:19
-
-
Save dharmatech/409723 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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