Calculate Number of Digits in a Number - Formula

  • Thread starter Thread starter UrbanXrisis
  • Start date Start date
  • Tags Tags
    Formula
AI Thread Summary
The discussion revolves around methods to calculate the number of digits in a number. One suggested approach involves dividing the number by increasing powers of ten until the result is less than ten, indicating the number of digits. Another participant discusses using programming techniques, such as converting numbers to strings and manipulating them, to achieve the same result. A mathematical perspective is also presented, highlighting the relationship between a number's digits and its decimal representation. Overall, the conversation explores both manual and programming methods for digit calculation.
UrbanXrisis
Messages
1,192
Reaction score
1
Okay, if I was given the number 4346, there are 4 digits there right? duh! :-p

If I was given 45645, there are 5 digits right? duh! :-p

Okay, does anyone know a formula to calculate the number of digits in a number?
 
Physics news on Phys.org
Yeah, divide the number by 10, 100, 1000, 10000, and so on until you get a result that is less than 10 and more than 1, then the number has the same number of digits with the number you last divided it by.

For example,

45654/10 > 10, so no.
45654/100 > 10, again no.
45654/1000 > 10, nope.
45654/10000 is < 10, so yes!

10000 is a 5 digit number so 45654 is a 5 digit number.

I hope you don't take me too seriously because I don't know any special formulae, but this is how I think of the problem anyway.
 
That's exactly what I did for my Computer Science program. We were told to switch any given number so it would read backwards...ex...5432 --> 2345

Not knowing a forumla, what you described above is exactly what I did
 
Switching numbers to read backwards is fun. I've never taken a single computer lesson, but I have a sketchy image of how programming works. How did you do the problem though? I don't see how you could have arrived at the solution with the method I mentioned earlier.

Lets see:

5432 is a 4-digit number so that means that the "highest number" able to divide it is 1000.

5432/1000 = 5.432

I think there is an in-built function in most computer programmes that allow you to take the integer part of a number, is there not?

So we take the integer part of the number, which is 5 and call it z.

We minus 1000z from 5432 to get 432, which is a three-digit number and is 4.32 when divided by 100. So we can obtain 4 with the integer function and call it y.

I think this can continue until we have x (3) and w (2) as well.

So we can string them together to get 1000w + 100x + 10y + z = 2345

Is that how you did it? Is there a shorter algorithm for it?
 
I don't know what language you're using, but I'd convert it to a string and work with it that way. In Visual Basic:
Code:
n = 12345
s = str$(n)
r = ""
for k=len(s) to 1  step -1
   r=r+mid$(s,k,1)
next k
n2 = val(r)
 
Is there a good site online that teaches Visual Basic? I don't really know any programming but I would like to start with Visual Basic. I also need a compiler for writing programs in VBasic. Is there anywhere I can download it for free?
 
I recommend taking this question over to the computer science forum on this website. They have been very helpful with my Java problems.
 
From a mathematical point of view if a number N contains n digits, we can write it as

N = a_{0}a_{1}a_{2}a_{3}...a_{n-1}

In decimal notation,

N = \sum_{i = 0}^{n-1}a_{i}10^i

Your problem is to convert N to N' which is

N&#039; = a_{n-1}...a_{3}a_{2}a_{1}a_{0}

So

N&#039; = \sum_{i = 0}^{n-1} a_{i}10^{n-i-1}

The digits can be extracted using division by 10.

Cheers
Vivek
 
Last edited:
recon said:
Switching numbers to read backwards is fun. I've never taken a single computer lesson, but I have a sketchy image of how programming works. How did you do the problem though? I don't see how you could have arrived at the solution with the method I mentioned earlier.

I program in the language Java, all I did was the following:

public class program12 {
public static void main(String[] args) {

int number=0, counter=0,trueOrFalse=0;
String firstNum;

firstNum=JOptionPane.showInputDialog("Enter integer: ");
number=Integer.parseInt(firstNum);
trueOrFalse= factorial(number);
System.out.println(trueOrFalse);
}

public static int factorial (int integer)
{
int tens=1,mod=0,value=0,temp=0, counterTens=10,counterOnes=1;

while (mod!=integer){
tens=tens*10;
mod=integer%tens;
}

for (int tenSec=10;tenSec<=tens;tenSec=tenSec*10){
value=(integer%tenSec);
integer=integer-value;
value=value/counterOnes;
value=(value*tens)/counterTens;
temp=temp+value;
counterTens=counterTens*10;
counterOnes=counterOnes*10;
}
return (temp);
}
}


Basically...enter 5321

It counts that it has 4 digits
then does 1000+200+30+5 = 1235
 
Last edited:
  • #10
If n>0 is an integer, then it has floor(\log_{10}n+1) digits. I don't know if this will be preferable to your loop though.
 
  • #11
I'll never ever learn how to program. :(

My chances of mastering Latin should be higher than mastering any programming languages.
 

Similar threads

Replies
11
Views
1K
Replies
8
Views
2K
Replies
4
Views
3K
Replies
3
Views
2K
Replies
11
Views
2K
Replies
4
Views
2K
Replies
1
Views
598
Back
Top