Quick Way to Get Number of Digits in an Integer

Started by
51 comments, last by ToohrVyk 19 years ago
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 [edit] 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]
Advertisement
Quote:Original Number is: 0
Number of digits is: -2147483647


Hint: log(0) is -∞
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Quote:Original post by smart_idiot
Quote:Original Number is: 0
Number of digits is: -2147483647


Hint: log(0) is -∞


Foiled again! [lol] Thanks for pointing that out [wink] I will make the update.
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;}
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;}
Quote:Original post by Prototype
I had this one which doesn't involve division and it appears to be 300% faster [smile]

stupid me, thanks [smile]
Quote:Original post by Prototype
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;}

I think if you initialize digits to 1, and step to 10, you can just return digits without the branch at the end.
[edit] Whoa I type slow [lol]

Quote:Original post by doho
I 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!
hmmm. i thought there wasent a way to determine the runtime of euclid's alg.
must of had a bad discreet teacher
the mountains quake, and a little mouse pops out
Quote:Original post by micro_mus
must of had a bad discreet teacher


Would that be an indiscreet teacher? [wink]

This topic is closed to new replies.

Advertisement