Skip to content

Instantly share code, notes, and snippets.

@schwern
Created October 17, 2010 20:41
Show Gist options
  • Save schwern/631267 to your computer and use it in GitHub Desktop.
Save schwern/631267 to your computer and use it in GitHub Desktop.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int lengthi_log(int x) {
int length = 0;
if( x == 0 )
return 1;
length = ceil(log10(abs(x)));
if( x < 0 )
length++;
return length;
}
int lengthi_pow(int x) {
int length = 1;
int abs_x = abs(x);
while( pow(10, length) <= abs_x ) length++;
if( x < 0 )
length++;
return length;
}
int main() {
int i;
for(i = 0; i < 10000000; i++ ){
lengthi_pow(123);
lengthi_pow(1234);
lengthi_pow(-123);
lengthi_pow(0);
lengthi_pow(-0);
lengthi_pow(1234567);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment