Once when I was in 8th grade, in math class we were discussing different kinds of numbers. There were the ones like 1/8 whic is 0.125 and then the repeating ones like 1/7 which is 0.142857142857142857... My math teacher made the mistake of saying that you could always tell these numbers apart from numbers that never repeat, because the ones that would repeat would repeat after just a few digits.
(Of course, the real way to tell them apart is that fractions always terminate or repeat, it's only things like pi and sqrt(2) and such that never repeat.)
Earlier in the year we had been doing stuff with pi, and we were told to use 22/7 as a fraction equal to pi. Now 22/7 is pretty close to pi, but at the age of 11 I had no concept of close enough, so I was determined to do better. I went home and wrote a little program on my TRS-80 to try to find a closer fraction, and I eventually came up with 355/113, which is accurate to six decimal places.
I also noticed that unlike 22/7, which repeats after six decimal places, 355/113 didn't appear to repeat at all. So I wrote another program to see how many digits you had to go out before it began repeating. To my complete astonishment, you have to go 112 digits before they start repeating.
This had me pretty interested, so I went on to see what other close to pi fractions I could come up with. So then I found 102928/32763. I particularly liked this one since 32763 was close to 32768. This one didn't repeat until after 891 digits, and as an added bonus, was accurate to as many decimal places as my calculator would show. So I made a point to memorize it, and despite it's uselessness, I still remember it to this day.
So anyway, when my math teacher said that all repeating numbers will repeat after just a few numbers, I had to bring 102928/32763 to her attention. She found it absolutely amazing. Not because it repeats after 891 digits, but because she thought it was equal to pi. She just typed it into her calculator, it told her it was 3.14159265, and she knew that pi was 3.14159265, and that was all the convincing she needed. She even dug up a poster with the first 10,000 digits of pi on it and showed it to me, telling me that the number on the poster was what that fraction was. I knew that it wasn't, but I just let her go on believing whatever she wanted to believe.
Later when I got one of those awful graphing calculators in 9th grade, I found that it had more decimal digits in it's numbers, so then I needed a closer fraction, and came up with 833719/265381, which turned up 3.1415926536 on the graphing calculator, and repeats after 88460 digits.
Of course, the pi fractions aren't too useful. Except for 355/113, you end up with more digits in the fraction than the answer is accurate to. However, the many repeating digits are quite interesting.
Playing with a simple C program, I've found the fraction x/4294967291 which repeats after 4294967290 digits, so long as 0 < x < 4294967291. Things like this are an easy way to make a simple random number generator, since it gives you 2**32-5 random bits before it repeats. (For this number the length of repeating bits happens to be the same as the length of repeating decimal digits, however that isn't true for all divisors.)
A random data generator, in my favorite pseudo-assembly:
# =====================================================================
section .text
main proc argc, argv, envp
mov ecx, 64 * 1024 * 1024
do
cld; mov edi, buffer
do
mov eax, [seed]
rol eax, 8
mov edx, eax
and eax, 0xFFFFFF00
and edx, 0x000000FF
div dword [divisor]
mov [seed], edx
stosb
dec ecx
test ecx, 0xFFF
loopif nz
push ecx
sys sys_write, 1, buffer, 4096
pop ecx
or ecx, ecx
loopif nz
return 0
endproc
# =====================================================================
section .data
seed db 'seed'
divisor dd 4294967291
# =====================================================================
section .bss
buffer resb 4096
It seems to be perfectly random enough:
pj@sucks:~/asm$ ./assemble random.asm
pj@sucks:~/asm$ ./random | hexdump -C -n 256
00000000 64 65 65 74 f5 fa fb 48 cd e6 e8 6c 05 82 8a 1c |deetõúûHÍæèl....|
00000010 1b 8c b2 8c 89 bf 7c be b0 bd 6f b9 73 b3 2e 9f |..²..¿|¾°½o¹s³..|
00000020 42 7f e9 1c 4c 7f 8d 8d 7e 7d c3 c3 78 74 d2 d1 |B.é.L...~}ÃÃxtÒÑ|
00000030 5a 48 1e 16 c3 68 96 71 d1 0a f0 39 15 36 b1 1d |ZH..Ãh.qÑ.ð9.6±.|
00000040 6a 11 75 93 12 57 4b df 5b b4 7b 5c ca 86 68 cf |j.u..WKß[´{\Ê.hÏ|
00000050 f4 a0 0c 0f c7 20 3c 4e e3 a1 2d 8a 72 25 e3 b4 |ô ..Ç random_data
pj@sucks:~/asm$ ls -la random_data
-rw-r--r-- 1 pj users 67108864 2005-10-08 04:52 random_data
pj@sucks:~/asm$ gzip random_data
pj@sucks:~/asm$ ls -la random_data.gz
-rw-r--r-- 1 pj users 67119134 2005-10-08 04:52 random_data.gz
It looks random and it doesn't compress. I'd say that's good enough for just about everything short of cryptography and other things which need truely random numbers. And when I think of some of the god-awful complicated as hell random number routines I've seen in the past...
Note the beginning of the data, 'deet', which is 'seed' + 1 backwards. Kind of makes me think there's probably some way to get rid of the divide.