TPK algorithm: Difference between revisions

Content deleted Content added
PGSONIC (talk | contribs)
No edit summary
Maerics (talk | contribs)
Ruby version: New implementation in same spirit but with more idiomatic Ruby, removed sentence about not repeating overflow values, since it is not discussed elsewhere in the article.
Line 62:
The [[Ruby (programming language)|Ruby]] version takes advantage of some of its features:
 
<source lang="ruby"># by pietro gagliardi | june 25, 2007
Array.new(11) { gets.to_i }.reverse.each do |x|
x = []
begin
11.times do
x y = 5 * x +** [gets.to_i]3
raise if y > 400
puts "#{x} #{y}"
rescue
puts "#{x} TOO LARGE"
end
end
end</source>
r = 0
x.reverse.each do |val|
begin
if r == 1 then
r = 0
else
val = 5 * val ** 3
puts val.to_s
end
rescue
puts "overflow"
r = 1
retry
end
end</source>
 
Ruby handles extremely large values very easily, even on 64-bit systems, so the possibility of an overflow in the above program would be slim. If it does happen, the variable r and the retry statement ensures that the exception does not occur again on the same value.
 
==References==