Jump to content
  • Advertisement
Sign in to follow this  
Drew_Benton

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.

If you intended to correct an error in the post then please contact us.

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 [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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!