TPK algorithm: Difference between revisions

Content deleted Content added
rv recent changes to formatting, coloring; use <code> tags for formatting for now
Coop (talk | contribs)
Added Ruby version
Line 47:
print i, y
</code>
 
==Ruby version==
The following Ruby version makes heavy use of blocks / closures, as is common practice in the language:
 
<code>
f = proc { |t| (t.abs + 5 * t ** 3) ** 0.5 }
(1..11).map { f[gets.to_i] }.each_with_index { |t,u| puts u, t > 400 ? "TOO LARGE" : t }
</code>
 
Or, in a slightly longer, but more standard implementation:
 
<code>
a = []
f = lambda { |t| (t.abs + 5 * t ** 3) ** 0.5 }
11.times { a << gets.to_i }.times do |i|
j = f[a.shift]
puts i, j > 400 ? "TOO LARGE" : j
end
</code>
 
 
==References==