| 1 |
### Remarks |
| 2 |
|
| 3 |
Just run it with no argument: |
| 4 |
|
| 5 |
$ ruby entry.rb |
| 6 |
|
| 7 |
I confirmed the following implementation/platform: |
| 8 |
|
| 9 |
- ruby 2.2.3p173 (2015-08-18 revision 51636) [x64-mingw32] |
| 10 |
|
| 11 |
|
| 12 |
### Description |
| 13 |
|
| 14 |
The program is a [Piphilology](https://en.wikipedia.org/wiki/Piphilology#Examples_in_English) |
| 15 |
suitable for Rubyists to memorize the digits of [Pi](https://en.wikipedia.org/wiki/Pi). |
| 16 |
|
| 17 |
In English, the poems for memorizing Pi start with a word consisting of 3-letters, |
| 18 |
1-letter, 4-letters, 1-letter, 5-letters, ... and so on. 10-letter words are used for the |
| 19 |
digit `0`. In Ruby, the lengths of the lexical tokens tell you the number. |
| 20 |
|
| 21 |
$ ruby -r ripper -e \ |
| 22 |
'puts Ripper.tokenize(STDIN).grep(/\S/).map{|t|t.size%10}.join' < entry.rb |
| 23 |
31415926535897932384626433832795028841971693993751058209749445923078164062862... |
| 24 |
|
| 25 |
The program also tells you the first 10000 digits of Pi, by running. |
| 26 |
|
| 27 |
$ ruby entry.rb |
| 28 |
31415926535897932384626433832795028841971693993751058209749445923078164062862... |
| 29 |
|
| 30 |
|
| 31 |
### Internals |
| 32 |
|
| 33 |
Random notes on what you might think interesting: |
| 34 |
|
| 35 |
- The 10000 digits output of Pi is seriously computed with no cheets. It is calculated |
| 36 |
by the formula `Pi/2 = 1 + 1/3 + 1/3*2/5 + 1/3*2/5*3/7 + 1/3*2/5*3/7*4/9 + ...`. |
| 37 |
|
| 38 |
- Lexical tokens are not just space-separated units. For instance, `a*b + cdef` does |
| 39 |
not represent [3,1,4]; rather it's [1,1,1,1,4]. The token length |
| 40 |
burden imposes hard constraints on what we can write. |
| 41 |
|
| 42 |
- That said, Pi is [believed](https://en.wikipedia.org/wiki/Normal_number) to contain |
| 43 |
all digit sequences in it. If so, you can find any program inside Pi in theory. |
| 44 |
In practice it isn't that easy particularly under the TRICK's 4096-char |
| 45 |
limit rule. Suppose we want to embed `g += hij`. We have to find [1,2,3] from Pi. |
| 46 |
Assuming uniform distribution, it occurs once in 1000 digits, which already consumes |
| 47 |
5000 chars in average to reach the point. We need some TRICK. |
| 48 |
|
| 49 |
- `alias` of global variables was useful. It allows me to access the same value from |
| 50 |
different token-length positions. |
| 51 |
|
| 52 |
- `srand` was amazingly useful. Since it returns the "previous seed", the token-length |
| 53 |
`5` essentially becomes a value-store that can be written without waiting for the |
| 54 |
1-letter token `=`. |
| 55 |
|
| 56 |
- Combination of these techniques leads to a carefully chosen 77-token Pi computation |
| 57 |
program (quoted below), which is embeddable to the first 242 tokens of Pi. |
| 58 |
Though the remaining 165 tokens are just no-op fillers, it's not so bad compared to |
| 59 |
the 1000/3 = 333x blowup mentioned above. |
| 60 |
|
| 61 |
|
| 62 |
big, temp = Array 100000000**0x04e2 |
| 63 |
srand big |
| 64 |
alias $curTerm $initTerm |
| 65 |
big += big |
| 66 |
init ||= big |
| 67 |
$counter ||= 02 |
| 68 |
while 0x00012345 >= $counter |
| 69 |
numbase = 0x0000 |
| 70 |
$initTerm ||= Integer srand * 0x00000002 |
| 71 |
srand $counter += 0x00000001 |
| 72 |
$sigmaTerm ||= init |
| 73 |
$curTerm /= srand |
| 74 |
pi, = Integer $sigmaTerm |
| 75 |
$counter += 1 |
| 76 |
srand +big && $counter >> 0b1 |
| 77 |
num = numbase |= srand |
| 78 |
$sigmaTerm += $curTerm |
| 79 |
pi += 3_3_1_3_8 |
| 80 |
$curTerm *= num |
| 81 |
end |
| 82 |
print pi |
| 83 |
|
| 84 |
- By the way, what's the blowup ratio of the final code, then? |
| 85 |
It's 242/77, whose first three digits are, of course, 3.14. |