# In need of a formula

1. Oct 28, 2004

### UrbanXrisis

Okay, if I was given the number 4346, there are 4 digits there right? duh! :tongue2:

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

Okay, does anyone know a formula to calculate the number of digits in a number?

2. Oct 28, 2004

### recon

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.

3. Oct 28, 2004

### UrbanXrisis

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

4. Oct 28, 2004

### recon

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?

5. Oct 28, 2004

### tony873004

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 (Text):

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)

6. Oct 29, 2004

### recon

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?

7. Oct 29, 2004

### ek

I recommend taking this question over to the computer science forum on this website. They have been very helpful with my Java problems.

8. Oct 29, 2004

### maverick280857

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' = a_{n-1}....a_{3}a_{2}a_{1}a_{0}$$

So

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

The digits can be extracted using division by 10.

Cheers
Vivek

Last edited: Oct 29, 2004
9. Oct 29, 2004

### UrbanXrisis

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: Oct 29, 2004
10. Oct 29, 2004

### shmoe

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. Oct 30, 2004

### recon

I'll never ever learn how to program. :(

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