On Tue, 6 Feb 2001, Clemens Wyss wrote:

> In the following example:
>   def recursion(n)
>     if n == 0 then
>       1
>     else
>       (n * recursion(n-1)) **2 
>     end
>   end

rewriting it differently:

y = 1
(1..ARGV[0].to_i).each {|n|
        y = (y * n) ** 2
        p y
}

This gives a very fast-growing sequence:

1
4
144
331776
2751882854400
272622932796264897576960000
3641839910835401567626683591527643364677019238400000000


This series is almost an exponential of an exponential!

The size of an integer is expressed in bytes. A byte is worth about 2.4
decimal digits.

at n=10 the size is 6004.

Squaring that number takes a time similar to the square of its number of
digits. Even by working by blocks of 32 bits (4 bytes), you don't get away
with less than a million operations.

Ruby's threading system is preemptive within Ruby code, but is cooperative
at the C level.

Operations on integers are atomic, that is, ininterruptible.

The time it takes to kill depends on the speed of your machine. 
Counterintuitively, the faster it is, the more time it takes to kill your
thread.



matju

note: integers are padded with zeros up to a digit count multiple of 4.