After my factorial base post, a commenter asked about multiplication and it turns out multiplication is hard in factoradix, as far as I know. A natural question is, what does factoradix make easy? Here's an example: testing for divisibility.
In fact, this can be downright awesome by comparison. In decimal, testing if a number n divides into m takes time about equal to the length of m. Checking if 3 divides into a billion digit decimal number will take your computer about one billion cycles, within some constant factor of time spent.
Checking if m divides n in factoradix takes time proportional only to m. Checking if 3 divides a billion digit number in factoradix (which would be about a 9 billion digit number in decimal because of decimal's less efficient use of digits), only requires looking at the last 2 places!
Let me give an example. Does 3 divide into 9a0a4023220011?
Recall that each of the places corresponds to a certain number of factorials. Well, every factorial greater than 2! contains 3 as a factor, by definition. Therefore, the values of any digit other that the two rightmost, is irrelevant, because it could only change the value of the number by a multiple of three. This is sort of like how you can tell if a decimal number is divisible by ten only by looking at the last digit (all the other digits cannot affect the divisibility by ten).
Getting back to 9a0a4023220011, we need only check 11. Sure enough 11(factoradix) = 1 * 2! + 1 * 1! = 3. So yes, 3 does divide 9a0a4023220011.
Friday, June 13, 2008
Thursday, June 12, 2008
Is No One Rational?
I ran into a math subproblem in the course of a bigger problem. I thought I'd give the Internet a shot before posting the answer. Here it is:
Show that for any finite set of positive real numbers, there must exist some number x such that x times each of the numbers in the set produces an irrational number.
Or rephrased:
If n runners start out together on an infinite race, each at a constant speed, which is different from the others' speeds, show that at some time each one will be an irrational distance from the start line.
Just a reminder: irrational numbers are those that cannot be written as a fraction a/b with a and b both whole numbers.
Show that for any finite set of positive real numbers, there must exist some number x such that x times each of the numbers in the set produces an irrational number.
Or rephrased:
If n runners start out together on an infinite race, each at a constant speed, which is different from the others' speeds, show that at some time each one will be an irrational distance from the start line.
Just a reminder: irrational numbers are those that cannot be written as a fraction a/b with a and b both whole numbers.
Sunday, June 8, 2008
The Factorial Base
We've all heard of binary and decimal as systems to represent integers. The idea is simple: In the decimal number 328, 3 reflects three hundreds, 2 reflects two tens and the 8 adds in eight ones.
The same number is 101001000 in binary. Specifically, one 256, one 64 and one 8. 256, 64 and 8 are the eighth, sixth and third powers of two, and those occur as ones that many spaces over from the right side of the number.
Clearly, numbers are longer in binary than they are in decimal, but the digits are much simpler in binary. If all we cared about was reducing the number of digits, we could use base 20, or 2008 or the largest number we could think of.
Of course the tradeoff that comes is that it can be hard to make up and keep straight all the extra digits we would need. For example, the decimal number 1330 only needs three digits in base 11, but each of those digits is the digit "10", because 1330 = 10 * 112 + 10 * 11 + 10. How would we write this? One option is the hexadecimal system where 10 is a, 11 is b and so on. Then 1330 (base 10) = aaa (base 11). We can take this all the way up to base 36 using all 26 letters, in which case a long decimal number like 2621102907451 (base 10) compacts down to xg48zv3v (base 36).
But for a small number like 10, it seems unreasonable to write it as "a" just to save one digit. What if there was some way to have small digits for small numbers and compact representations for large numbers? It turns out there is a way to do this using the "factorial base" or "factoradix".
In factoradix, the rightmost digit is base 2, the next one is base 3, and so on. As an example, here's how you would calculate the factoradix notation for 99:
First we notice that 99 gives a remainder of 1 when divided by 2. The quotient is 49. We keep both of these numbers: 1 because it is the rightmost digit of the factoradix representation of 99 and 49 because we need it for the next steps. Next, 49 divided by 3 is 16 with remainder 1. So 1 is the next digit and so far we have _11 as our representation with the _ part still to be filled in (we can't know how many digits it will be, yet). Divide 16 by 4, quotient: 4, remainder: 0. Last, divide 4 by 5 to get a remainder of 4 and quotient of 0 (0 means the algorithm is finished). Combining all the remainders, 4011 ends up as the factoradix notation for 99. Longer yes, but with a simpler choice of digits.
We don't need to go through the details, but larger numbers make the opposite tradeoff in factoradix. A 1 with 47 zeros behind it becomes 4z7,h0x,ipf,5gr,0bc,l7l,977,460,068,952,315,551,220 which is only 39 digits long. A 1 with 100 zeros is only 69 digits long, but we need some way to encode digits like "58".
What do some more of these representations look like? First, the Perl function below encapsulates the algorithm above.
Using this function, here's how the powers of ten start out in factoradix:
For 100, we get 1
For 101, we get 120
For 102, we get 4,020
For 103, we get 121,220
For 104, we get 1,651,220
For 105, we get 23,551,220
For 106, we get 266,251,220
For 107, we get 2,750,051,220
For 108, we get 25,551,151,220
For 109, we get 210,564,451,220
For 1010, we get 1,7a5,726,651,220
For 1015, we get 2d,caa,584,234,451,220
For 1020, we get 1k2,13c,54a,a81,881,151,220
For 1025, we get g2i,h8c,8f7,bc3,5b8,250,051,220
For 1030, we get 3,7mf,cdg,584,64c,f8b,c01,182,251,220
For 1035, we get c,4tr,pn8,9ef,g2b,d3f,34b,225,975,551,220
For 1040, we get x,tkt,18i,jep,8bh,g9j,f7e,1bb,652,056,651,220
For 1064, we get g,lqd,ila,9ii,ju0,a5m,bas,91k,7l5,m66,1ii,4h5,462,7a5,726,651,220
Every power of ten larger than 1064 seems to use a digit that we don't have a letter for (and I checked up to 101400!). Of course, all we need to do is add more symbols, maybe hearts or that thing The Artist Formerly Known as Prince uses...
What do you think is the next factoradix question to investigate?
The same number is 101001000 in binary. Specifically, one 256, one 64 and one 8. 256, 64 and 8 are the eighth, sixth and third powers of two, and those occur as ones that many spaces over from the right side of the number.
Clearly, numbers are longer in binary than they are in decimal, but the digits are much simpler in binary. If all we cared about was reducing the number of digits, we could use base 20, or 2008 or the largest number we could think of.
Of course the tradeoff that comes is that it can be hard to make up and keep straight all the extra digits we would need. For example, the decimal number 1330 only needs three digits in base 11, but each of those digits is the digit "10", because 1330 = 10 * 112 + 10 * 11 + 10. How would we write this? One option is the hexadecimal system where 10 is a, 11 is b and so on. Then 1330 (base 10) = aaa (base 11). We can take this all the way up to base 36 using all 26 letters, in which case a long decimal number like 2621102907451 (base 10) compacts down to xg48zv3v (base 36).
But for a small number like 10, it seems unreasonable to write it as "a" just to save one digit. What if there was some way to have small digits for small numbers and compact representations for large numbers? It turns out there is a way to do this using the "factorial base" or "factoradix".
In factoradix, the rightmost digit is base 2, the next one is base 3, and so on. As an example, here's how you would calculate the factoradix notation for 99:
First we notice that 99 gives a remainder of 1 when divided by 2. The quotient is 49. We keep both of these numbers: 1 because it is the rightmost digit of the factoradix representation of 99 and 49 because we need it for the next steps. Next, 49 divided by 3 is 16 with remainder 1. So 1 is the next digit and so far we have _11 as our representation with the _ part still to be filled in (we can't know how many digits it will be, yet). Divide 16 by 4, quotient: 4, remainder: 0. Last, divide 4 by 5 to get a remainder of 4 and quotient of 0 (0 means the algorithm is finished). Combining all the remainders, 4011 ends up as the factoradix notation for 99. Longer yes, but with a simpler choice of digits.
We don't need to go through the details, but larger numbers make the opposite tradeoff in factoradix. A 1 with 47 zeros behind it becomes 4z7,h0x,ipf,5gr,0bc,l7l,977,460,068,952,315,551,220 which is only 39 digits long. A 1 with 100 zeros is only 69 digits long, but we need some way to encode digits like "58".
What do some more of these representations look like? First, the Perl function below encapsulates the algorithm above.
sub factoradix {
my $n = shift;
my $base = Math::BigInt->new(2);
my (@digits, $digit);
while ($n->bcmp(0)) {
($n, $digit) = $n->bdiv($base);
unshift @digits, $digit->bstr();
$base->binc();
}
return [ @digits ];
}
Using this function, here's how the powers of ten start out in factoradix:
For 100, we get 1
For 101, we get 120
For 102, we get 4,020
For 103, we get 121,220
For 104, we get 1,651,220
For 105, we get 23,551,220
For 106, we get 266,251,220
For 107, we get 2,750,051,220
For 108, we get 25,551,151,220
For 109, we get 210,564,451,220
For 1010, we get 1,7a5,726,651,220
For 1015, we get 2d,caa,584,234,451,220
For 1020, we get 1k2,13c,54a,a81,881,151,220
For 1025, we get g2i,h8c,8f7,bc3,5b8,250,051,220
For 1030, we get 3,7mf,cdg,584,64c,f8b,c01,182,251,220
For 1035, we get c,4tr,pn8,9ef,g2b,d3f,34b,225,975,551,220
For 1040, we get x,tkt,18i,jep,8bh,g9j,f7e,1bb,652,056,651,220
For 1064, we get g,lqd,ila,9ii,ju0,a5m,bas,91k,7l5,m66,1ii,4h5,462,7a5,726,651,220
Every power of ten larger than 1064 seems to use a digit that we don't have a letter for (and I checked up to 101400!). Of course, all we need to do is add more symbols, maybe hearts or that thing The Artist Formerly Known as Prince uses...
What do you think is the next factoradix question to investigate?
Subscribe to:
Posts (Atom)