tags:

views:

145

answers:

7

This isn't a homework question. All I can think of is to repeatedly divide the number by 10 (until number is less than 10) and keep a count but is there a trick for this sort fo thing?

A: 

If it is an Integer, you could convert the string representation into an array of characters, and then convert that into an array of bytes (0-9)

Charles Bretana
I think converting it into a string representation would essentially do repeatedly dividing by 10. At least, that's how I would be doing it.
David Liu
+4  A: 

Yep, you pretty much have the mathematical way to do it right there.

while (num >= 10)
    digit = num MOD 10   // and save this into an array or whatever
    num = num / 10

at the end of this, num will contain the last digit.

Here's a Javascript implementation:

function getDigits(num) {
    var digits = [];
    while (num >= 10) {
        digits.unshift(num % 10);
        num = Math.floor(num / 10);
    }
    digits.unshift(num);
    return digits;
}

Note that it only works for non-negative integers.

nickf
You will lose first digit of a number.
Vadim Shender
@Vadim yeah, should be _while (num > 0)_
Nikita Rybak
So please correct it.
Bart
+2  A: 

Python code using your approach:

def digits(n):
  ds = []
  while n > 0:
    ds.append(n % 10)
    n /= 10
  ds.reverse()
  return ds

Using convertation to string:

def digits(n):           
  return map(int, str(n))
Vadim Shender
+3  A: 

Why implement the conversion yourself when there's already a very reliable way to do it? (And since it's not homework).

In pseudo-C:

char digits[10];
sprintf(digits, "%d", number);

Now your digits char array (string) should consist of each digit of the number. Most other scripting languages also contain a sprintf function.

This will work if you want base 8 or base 16 or binary, etc. Just use a different format specifier.

but not generalizable to bases other than 16, 10, or 8. (binary doesn't have a "%" specifier for the standard version of sprintf)
Jason S
Then, if you want to use them as actual digits, you have to subtract `'0'` from all of them. As for why... maybe you're doing some recreational computational math problems and you don't want that bit of extra overhead. I'm sure there are project euler problems using the digits of a number!
Jefromi
I hate this converting to strings. I will downwote this as soon as I can. I just think it is really bad solution.
Bart
As I said -1. I the number is too long it will override mem after 9 digits (one byte for null char). And what if for some reason wide char strings will be used ? `number` is a number so there are functions `div` and `mod` for that purpose.
Bart
A 32 bit integer will not have more than 9 digits (in base 10 at least). Adjust this if you want to handle 64-bit integers.And as for using div and mod, that's what sprintf() would use internally. The OP only wanted to extract the digits, he did not state for what purpose. If it is for computational purposes, then I can imagine a different method may be more appropriate, this is just one method.
A: 

The mathematical answer is to mod by 10 and add each result to a list, then reverse the list's order. Here's a basic C# algorithm that will do this:

List<byte> digits = new List<byte>();

while(number > 10)
{
   digits.Add(number % 10);
   number %= 10;
}

byte temp;
for(var i=0;i<digits.Count/2;i++)
{
   temp = digits[i];
   digits[i] = digits[digits.Count-(i+1)];
   digits[digits.Count-(i+1)] = temp;
}

Other "tricks" usually involve a string conversion. Here's a C# one-liner using Linq that will give the same result as the above:

var digits = number.ToString().Select(c=>byte.Parse(c)).ToList();
KeithS
+1  A: 

A more efficient algorithm, if your input numbers may be large, is to divide by a power of 10, say 1000, and use a lookup table:

s = ""; // or use a string builder appropriate to your language...
table = {"000", "001", ..., "999"};
tableInitial = {"unused", "1", "2", ..., "9", "10", ..., "999"};
while(n >= 1000) {
  m = n%1000;
  n /= 1000;
  s = table[m] + s;
}
s = tableInitial[n] + s;
Charles
A: 

The given python solution could be further optimized using

zerostr = ord('0')
def digits(n): 
    return map(lambda x: ord(x)-zerostr, str(n))

In fact, where the int -> str conversion is probably completely optimized, to get the numeric value it's much better to use the instrinsic character value of the digit string, which in every encoding (including EBCDIC) gives the numeric value by means of an int subtraction instead of a str parsing.

saverio