Thursday, December 21, 2006

divisibility tests

there are lots of quick divisibility tests for small numbers. the most basic ones are divisibility by 2, 5 and 10 in base 10 numbers, because a quick glance at the last digit will tell you if it is divisible. the second most basic is divisibility by 3, which requires adding all the digits together and seeing if the sum is divisible by 3. the next tier of tests involves multiplying the last digit by some constant and adding it to the other digits.

using other bases can make divisibility tests easier. for example, in any base if the last digit is zero the number is divisible by the base. and the prime factors of the base can be tested for by examining the last digit, as 2 and 5 in base 10. on computers, numbers are represented in binary, but they can easily be converted to any base which is a power of two. any number can easily be tested for divisibility if some multiple of the number is one less than the base, by simply adding all the digits.

this leads to two novel results. just as divisibility by 9, and its factor 3, are easily tested in base 10, divisibility by 7 is easily tested in base 8 by adding the octal digits because 7 is one less than 8. similarly, in base 100 (which is base 10 taken two digits at a time) divisibility by 11 is easily tested.

take for example the number 10741474139 to test for divisibility by 11. adding pairs of digits gives us 01+07+41+47+41+39 = 176 = 01+76 = 77 which is clearly divisible by 11. again, this works because 99, a multiple of eleven, is one less than our base of 100.

for computers, obviously any power of two is easily checked by examining the last few bits. for the next easiest tier, a well chosen bit size makes testing for divisibility as easy as adding together well chosen digits. as mentioned above, choosing digits of 3 bits makes 7 an easy test. because 3*5*17 is 255, 8 bit digits make tests for those primes as easy as adding bytes with a final modulus.

for other values, where the inverse of the base modulo the divisor is not one, the inverse needs to be multiplied by each last digit as it is added to the word. I have never seen a definition of how to calculate those special last digit multipliers that you see all over the place, so here goes.

given divisor D in base B, calculate X, the inverse of the base modulo the divisor. now take the number N to be tested and add the last digit multiplied by X to the other digits repeatedly.

N1 = N0/B + (N0%B)*X
repeat

for example, given a base of 10 when testing for divisibility by 7, find the inverse 5 such that 5*10 is 1 mod 7. now given a number N such as 1449, repeatedly take the last digit, multiply it by 5, and add it to the other digits.

1449, 144 + 9*5
189, 18 + 9*5
63, 6 + 3*5
21, 2 + 1*5
7 (after this it repeats 7,35,28,42,14,21,7)

of course, you can stop at any step in which it is obvious the number passes or fails the divisibility test. Also, at any step, you can subtract instead of adding. when adding multiply by 5, but when subtracting multiply by 7-5=2. Using the example above, but subtracting in the second step, gives us this.

1449, 144 + 9*5
189, 18 - 9*2
0 (always a positive result for divisibility test)

mathematically, you multiply the remainder of the base division by either X or X-D (which is negative) before adding it to the quotient of the base division.

Wednesday, July 26, 2006

Tuesday, June 20, 2006

Reverse EULA

As users, we are often presented with sneaky licenses that try to remove our freedoms.  Strike back with a reverse EULA.  Specify the rights that media distributors must grant you in order to display their media on your device.  For example, include the following document on your computer, where anyone may read it:

"By allowing media to be displayed on this device, you grant to the owner of the device every right to copy, store, time and space shift, modify and share the media."

As you browse various media sources, you are generally asked to display much more information than you requested.  The cost of displaying unrequested information comes directly from you.  Any media that is displayed on a device you own, or through a connection that you pay for, must do so under whatever terms you set.

They are talking about the rights of broadcasters.  As the owner of a media display device, you are broadcasting the media into your personal space.  Once you have broadcast the media, you gain the full rights of a broadcaster.  You have paid for part of the broadcast infrastructure, and you deserve recompense.

Obviously, this is not intended as legal advice. Enjoy.

Monday, June 05, 2006

Fiction

These are authors that I have read and would recommend. Some I would recommend to almost anyone, like Pratchett, and some would have Caveats, like Jordan. Following the name of the author is series, book or setting that is especially good. In some cases, I like almost everything an author has written, in other cases not so much. The authors are very roughly in my preferred order, but that order has changed over time and with my mood and the flight of moths. Some of these, like Robert Jordan, I would probably like less today, and some I would probably like more.

George Martin, Song of Ice and Fire
Terry Pratchett, Discworld
Gene Wolfe, New Sun
Douglas Adams, Hitchhikers
David Brin, Earth, Postman, Uplift
Frank Herbert, Dune
Ray Bradbury, Illustrated Man
Robert Heinlien, Stranger in a Strange Land
Isaac Asimov, Foundation
Aldous Huxley, Brave New World
Robert Anton Wilson, Schroedingers Cat
Guy Gavriel Kay, Tigana, Fionavar Tapestry
Richard Adams, Maia, Watership Down
Neil Stephenson, Snow Crash, Cryptonomicon
Kurt Vonnegut, Slaughterhouse Five
Orson Scott Card, Enders Shadow
Philip Dick, Do Androids Dream of Electric Sheep
Anne Rice, Vampire
Ian [M] Banks, Culture
Roger Zelazny, Amber
Glen Cook, Black Company
Jim Butcher, Dresden Files
Robert Jordan, Wheel of Time
Tom Robbins, Jitterbug Perfume
William Gibson, Sprawl
Lian Hearn, Tales of the Otori
Barry Hughart, Bridge of Birds
Michelle West, The Sun Sword

These are authors that others have recommended who also liked what I liked. So this is roughly my future reading list. Whether or not I get around to these, or they are any good, remains to be seen. Most of them come highly recommended and a few are classics. Some authors are also listed above, but serve to remind me of certain titles. Again, these are in no particular order, although a few towards the top have been more recommended or sound more interesting than a few towards the bottom.

Anthony Burgess, A Clockwork Orange
George Orwell, 1984, Animal Farm
Kurt Vonnegut, Sirens of Titan
Dave Duncan, Man of his Word
Vernor Vinge, A Fire Upon the Deep, A Deepness in the Sky
Stephen King, Dark Tower
Greg Egan, Permutation City, Diaspora
Philip Dick, A Scanner Darkly, Ubik, Valis
Robert Heinlien, Job, Friday, Door into Summer, Mistress
Steven Lawhead, Pendragon Cycle
Stephen Baxter, Manifold
Stephen Michael Stirling, Draka
Andrew Swann, Hostile Takeover
Robert Rankin, Brentford Trilogy
China Mieville, The Scar
John Varley, Gaean, Steel Beach, Persistence of Vision, Golden Globe
Kim Stanley Robinson, Mars
Steven Brust, Vlad Taltos
Peter Hamilton, Reality Dysfunction
Alistair Reynolds, Revelation Space, Chasm City
Richard Morgan, Altered Carbon
Peter Hamilton, Nights Dawn
Jack Vance, Dying Earth
Clive Lewis, Narnia

These are authors that are specifically not on my list. These are popular authors, and I have read several books from each, and I do not like and would not recommend them. Obviously, lots of people do like them, and you may be one of them. Some of these are simply for a young adult audience and I found them too late, others just fall flat.

Leland Exton Modesit, Saga of Recluse
Terry Goodkind, Sword of Truth
Raymond Feist, Serpentwar Saga
David Eddings, Belgariad
Joanne Rowling, Harry Potter
Ursula Le Guin, Earthsea

Let me mention that the kids books by Terry Pratchett are also great for adults. At least, for adults like me.