TPK algorithm: Difference between revisions

Content deleted Content added
The algorithm: reformat Algol code
rv recent changes to formatting, coloring; use <code> tags for formatting for now
Line 9:
 
<code>
'''begin integer''' i; '''real''' y; '''real array''' a[0:10];
'''real procedure''' f(t); '''real''' t; '''integervalue''' it;
'''real'''f y:= sqrt(abs(t))+5*t^3;
'''for''' i := 0 '''realstep''' 1 array'''until''' 10 '''do''' read(a[0:10i]);
'''for''' i := 10 '''step''' -1 '''until''' 0 '''do'''
'''real procedurebegin''' y := f(ta[i]);
'''realif''' t;y > 400 '''valuethen''' t;write(i, "TOO LARGE")
f := sqrt '''else''' write(abs(ti,y)) + 5*t^3;
'''end'''
'''for''' i := 0 '''step''' 1 '''until''' 10 '''do'''
read(a[i]);
'''for''' i := 10 '''step''' -1 '''until''' 0 '''do'''
'''begin''' y := f(a[i]);
'''if''' y > 400 '''then'''
write(i, "TOO LARGE")
'''else'''
write(i, y);
'''end'''
'''end'''
</code>
Line 34 ⟶ 25:
The problem with the usually specified function is that the term 5*t^3 gives overflows in almost all languages for very large negative values.
 
==[[Perl golf]]==
The following [[Perl]] implementation is 79 [[byte]]s.
 
<code>
<source lang=perl>
map print(($_>400?"TOO LARGE":$_).$/),reverse map 5*($x=<>)**3+sqrt abs$x,1..11
</sourcecode>
 
==Python version==
The following Python version of the algorithm uses the common Python idiom of using a list instead of an array, (although there is an array module available):
 
<code>
<source lang=python>
from math import sqrt
def f(t):
return sqrt(abs(t))+5*t**3
a = [int(raw_input()) for i in range(11)]
for i in range(10,-1,-1):
y = f(a[i])
if y > 400:
print i, "TOO LARGE"
else:
print i, y
</sourcecode>
 
==References==
Line 65 ⟶ 56:
[[Category:Algorithms]]
[[Category:Donald Knuth]]
[[Category:Articles_with_example_Python_codeArticles with example Python code]]
[[Category:Articles with example ALGOL 60 code]]
[[Category:Articles with example Perl code]]