# Quick Way to Get Number of Digits in an Integer

This topic is 4825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I was in Discrete Math today and we were going over some lessons on recursion, fib sequences, etc... One of the concepts that was brought up was finding the run time of Euclidean's GCF. There is this theorem, Lame's theorem that does this. Now part of the proof tells that the number of digits is what determines the run time. Now what concerned me was the method that was used to get the number of digits. I was like "hmmmm" this could be interesting! So here it is, a quick method of getting the number of digits for an Integer.
 digits = floor( log10( abs( number ) ) ) + 1;
This is demonstrated in this program.
#include <cmath>
#include <iostream>

int main( int argc, char* argv[] )
{
int long number = 0;
int digits = 0;

for( int x = 0; x < 25; x++ )
{
number = rand();
number != 0
// digits = floor( log10( abs( number ) ) ) + 1;
digits = floor( log10( abs( number != 0 ? number : 1 ) ) ) + 1;

std::cout << "Original Number is: " << number << std::endl;
std::cout << "Number of digits is: " << digits << std::endl;
std::cout << std::endl;
}

return 0;
}


Which produces an output of:
Original Number is: 41
Number of digits is: 2

Original Number is: 18467
Number of digits is: 5

Original Number is: 6334
Number of digits is: 4

Original Number is: 26500
Number of digits is: 5

Original Number is: 19169
Number of digits is: 5

Original Number is: 15724
Number of digits is: 5

Original Number is: 11478
Number of digits is: 5

Original Number is: 29358
Number of digits is: 5

Original Number is: 26962
Number of digits is: 5

Original Number is: 24464
Number of digits is: 5

Original Number is: 5705
Number of digits is: 4

Original Number is: 28145
Number of digits is: 5

Original Number is: 23281
Number of digits is: 5

Original Number is: 16827
Number of digits is: 5

Original Number is: 9961
Number of digits is: 4

Original Number is: 491
Number of digits is: 3

Original Number is: 2995
Number of digits is: 4

Original Number is: 11942
Number of digits is: 5

Original Number is: 4827
Number of digits is: 4

Original Number is: 5436
Number of digits is: 4

Original Number is: 32391
Number of digits is: 5

Original Number is: 14604
Number of digits is: 5

Original Number is: 3902
Number of digits is: 4

Original Number is: 153
Number of digits is: 3

Original Number is: 292
Number of digits is: 3

Press any key to continue


Just thought this was very interesting [smile]. What do you think? It sure beats converting to a string then checking the length [wink] - Drew  Thanks to smart_idiot for pointing out log(0) DNE That kind of takes the ease away from this since you have the check for 0 everytime, or you can just hope 0 isn't used [grin] [Edited by - Drew_Benton on March 28, 2005 2:29:43 PM]

##### Share on other sites
Quote:
 Original Number is: 0Number of digits is: -2147483647

Hint: log(0) is -∞

##### Share on other sites
Quote:
Original post by smart_idiot
Quote:
 Original Number is: 0Number of digits is: -2147483647

Hint: log(0) is -∞

Foiled again! [lol] Thanks for pointing that out [wink] I will make the update.

##### Share on other sites
I see nothing fundamentally different from your code than with the code below, which itself is quite logical.

int digits(int n) {	int i;	if(!n) {		return 1;	}	for(i = 0; n; ++i) {		n /= 10;	}		return i;}

##### Share on other sites
I had this one which doesn't involve division and it appears to be 300% faster [smile]

int Digits(const int number){	int digits = 0;	int step = 1;	while (step <= number) {		digits++;		step *= 10;	}	return digits ? digits : 1;}

##### Share on other sites
Quote:
 Original post by PrototypeI had this one which doesn't involve division and it appears to be 300% faster [smile]

stupid me, thanks [smile]

##### Share on other sites
Quote:
 Original post by PrototypeI had this one which doesn't involve division and it appears to be 300% faster [smile]int Digits(const int number){ int digits = 0; int step = 1; while (step <= number) { digits++; step *= 10; } return digits ? digits : 1;}

I think if you initialize digits to 1, and step to 10, you can just return digits without the branch at the end.

##### Share on other sites
 Whoa I type slow [lol]

Quote:
 Original post by dohoI see nothing fundamentally different from your code with the code below, which is quite logical.

You are right, but having:

int digits( int n ) {     return floor( log10( abs( number != 0 ? number : 1 ) ) ) + 1; }
is a new way that I've never seen before. Like I said, it's far from efficient, your examples are much faster than mine ( I just looked at the asm listing), but I think it is quite interesting nevertheless. [smile] I mean this was done around the mid 1800's in a proof!

##### Share on other sites
hmmm. i thought there wasent a way to determine the runtime of euclid's alg.

##### Share on other sites
Quote:

Would that be an indiscreet teacher? [wink]

• 33
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631352
• Total Posts
2999486
×