Trabb Pardo–Knuth algorithm: Difference between revisions

m
No edit summary
m (→‎{{header|Wren}}: Minor tidy)
 
(46 intermediate revisions by 27 users not shown)
Line 24:
# ''Print and show the program in action from a typical run here''. (If the output is graphical rather than text then either add a screendump or describe textually what is displayed).
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F f(x)
R sqrt(abs(x)) + 5 * x ^ 3
 
V s = Array(1..11)
s.reverse()
L(x) s
V result = f(x)
I result > 400
print(‘#.: #.’.format(x, ‘TOO LARGE!’))
E
print(‘#.: #.’.format(x, result))
print()</syntaxhighlight>
 
{{out}}
<pre>
11: TOO LARGE!
10: TOO LARGE!
9: TOO LARGE!
8: TOO LARGE!
7: TOO LARGE!
6: TOO LARGE!
5: TOO LARGE!
4: 322
3: 136.732050808
2: 41.414213562
1: 6
</pre>
 
=={{header|Ada}}==
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions;
 
procedure Trabb_Pardo_Knuth is
Line 62 ⟶ 93:
end loop;
 
end Trabb_Pardo_Knuth;</langsyntaxhighlight>
 
{{out}}
Line 82 ⟶ 113:
Tested with Agena 2.9.5 Win32
{{Trans|ALGOL W}}
<langsyntaxhighlight lang="agena">scope # TPK algorithm in Agena
local y;
local a := [];
Line 94 ⟶ 125:
fi
od
epocs</langsyntaxhighlight>
{{out}}
<pre>
Line 124 ⟶ 155:
This is as close as possible to Pardo and Knuth's original but works with the [http://www.gnu.org/software/marst/marst.html GNU MARST] ALGOL-to-C compiler. Note Pardo and Knuth did not insist on prompts or textual I/O as their report mostly concerned systems that predated even the idea of keyboard interaction.
 
<syntaxhighlight lang="text">begin
integer i; real y; real array a[0:10];
real procedure f(t); value t; real t;
Line 136 ⟶ 167:
outchar(1, "\n", 1)
end
end</langsyntaxhighlight>
 
Compilation and sample run:
Line 159 ⟶ 190:
=={{header|ALGOL 68}}==
{{Trans|ALGOL W}} which was itself a Translation of ALGOL 60.
<langsyntaxhighlight lang="algol68">[ 0 : 10 ]REAL a;
PROC f = ( REAL t )REAL:
sqrt(ABS t)+5*t*t*t;
Line 168 ⟶ 199:
ELSE print( ( fixed( y, -9, 4 ), newline ) )
FI
OD</langsyntaxhighlight>
{{out}}
<pre>
Line 187 ⟶ 218:
=={{header|ALGOL W}}==
{{Trans|ALGOL 60}}
<langsyntaxhighlight lang="algolw">begin
real y; real array a( 0 :: 10 );
real procedure f( real value t );
Line 199 ⟶ 230:
else write( y );
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 215 ⟶ 246:
6.0000
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">∇ {res}←Trabb;f;S;i;a;y ⍝ define a function Trabb
f←{(0.5*⍨|⍵)+5×⍵*3} ⍝ define a function f
S←,⍎{⍞←⍵ ⋄ (≢⍵)↓⍞}'Please, enter 11 numbers: '
:For i a :InEach (⌽⍳≢S)(⌽S) ⍝ loop through N..1 and reversed S
:If 400<y←f(a)
⎕←'Too large: ',⍕i
:Else
⎕←i,y
:EndIf
:EndFor
∇</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">proc: function [x]->
((abs x) ^ 0.5) + 5 * x ^ 3
 
ask: function [msg][
to [:floating] first.n: 11 split.words strip input msg
]
 
loop reverse ask "11 numbers: " 'n [
result: proc n
print [n ":" (result > 400)? -> "TOO LARGE!" -> result]
]</syntaxhighlight>
 
{{out}}
 
<pre>11 numbers: 1 2 3 4 5 6 7 8 9 10 11
11.0 : TOO LARGE!
10.0 : TOO LARGE!
9.0 : TOO LARGE!
8.0 : TOO LARGE!
7.0 : TOO LARGE!
6.0 : TOO LARGE!
5.0 : TOO LARGE!
4.0 : 322.0
3.0 : 136.7320508075689
2.0 : 41.41421356237309
1.0 : 6.0</pre>
 
=={{header|AutoIt}}==
<langsyntaxhighlight AutoItlang="autoit">; Trabb Pardo–Knuth algorithm
; by James1337 (autoit.de)
; AutoIt Version: 3.3.8.1
Line 240 ⟶ 314:
Func f($x)
Return Sqrt(Abs($x)) + 5*$x^3
EndFunc</langsyntaxhighlight>
{{out}}
<pre>Input: "1 2 3 4 5 6 7 8 9 10 11"
Line 255 ⟶ 329:
f(2) = 41.4142135623731
f(1) = 6</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">seq := [1,2,3,4,5,6,7,8,9,10,11]
MsgBox % result := TPK(seq, 400)
return
 
TPK(s, num){
for i, v in reverse(s)
res .= v . " : " . ((x:=f(v)) > num ? "OVERFLOW" : x ) . "`n"
return res
}
reverse(s){
Loop % s.Count()
s.InsertAt(A_Index, s.pop())
return s
}
f(x){
return Sqrt(x) + 5* (x**3)
}</syntaxhighlight>
{{out}}
<pre>11 : OVERFLOW
10 : OVERFLOW
9 : OVERFLOW
8 : OVERFLOW
7 : OVERFLOW
6 : OVERFLOW
5 : OVERFLOW
4 : 322.000000
3 : 136.732051
2 : 41.414214
1 : 6.000000</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f TRABB_PARDO-KNUTH_ALGORITHM.AWK
BEGIN {
Line 275 ⟶ 380:
function abs(x) { if (x >= 0) { return x } else { return -x } }
function f(x) { return sqrt(abs(x)) + 5 * x ^ 3 }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 292 ⟶ 397:
</pre>
 
=={{header|BASIC256BASIC}}==
==={{header|ANSI BASIC}}===
<lang BASIC256>dim s(11)
{{trans|Commodere BASIC}}
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">
100 REM Trabb Pardo-Knuth algorithm
110 REM Used "magic numbers" because of strict specification of the algorithm.
120 DEF FNF(N) = SQR(ABS(N)) + 5 * N * N * N
130 DIM S(0 TO 10)
140 PRINT "Enter 11 numbers."
150 FOR I = 0 TO 10
160 INPUT PROMPT STR$(I + 1) & ":": S(I)
170 NEXT I
180 PRINT
190 REM Reverse
200 FOR I = 0 TO INT(10 / 2)
210 LET Tmp = S(I)
220 LET S(I) = S(10 - I)
230 LET S(10 - I) = Tmp
240 NEXT I
250 REM Results
260 FOR I = 0 TO 10
270 LET R = FNF(S(I))
280 PRINT USING "f(####.###) = ": S(I);
290 IF R > 400 THEN
300 PRINT "overflow"
310 ELSE
320 PRINT USING "####.###": R
330 END IF
340 NEXT I
350 END
</syntaxhighlight>
{{out}}
<pre>
Enter 11 numbers.
1:-5
2:-3
3:-2
4:-1
5:0
6:1
7:2
8:3
9:4
10:5
11:6
 
f( 6.000) = overflow
f( 5.000) = overflow
f( 4.000) = 322.000
f( 3.000) = 136.732
f( 2.000) = 41.414
f( 1.000) = 6.000
f( .000) = .000
f( -1.000) = -4.000
f( -2.000) = -38.586
f( -3.000) = -133.268
f( -5.000) = -622.764
</pre>
 
==={{header|Applesoft BASIC}}===
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
<syntaxhighlight lang="qbasic">10 REM Trabb Pardo-Knuth algorithm
20 HOME : REM 20 CLS for Chipmunk Basic or GW-BASIC
30 DIM S(10)
40 PRINT "Enter 11 numbers."
50 FOR I = 0 TO 10
60 PRINT I+1;
70 INPUT "=> ";S(I)
80 NEXT I
90 PRINT
100 REM Results
110 FOR I = 10 TO 0 STEP -1
120 PRINT "f( " S(I)") = ";
130 F = S(I)
140 R = SQR(ABS(F))+5*F^3
150 IF R > 400 THEN PRINT "-=< overflow >=-"
160 IF R <= 400 THEN PRINT R
170 NEXT I
180 END</syntaxhighlight>
 
==={{header|ASIC}}===
{{works with|ASIC|5.0}} Compile with the option ''Decimal Math'' (<code>DEC</code> in command line).
Notes:
* ASIC does not have the fuction that calculates a square root. Thus, the program uses the added subprogram <code>CALCSQRT:</code>.
* ASIC does not scroll a text screen, but it wraps the text. Thus, the program clears the screen after the user enters numbers.
<syntaxhighlight lang="basic">
REM Trabb Pardo-Knuth algorithm
REM Used "magic numbers" because of strict specification of the algorithm.
DIM S@(10)
PRINT "Enter 11 numbers."
FOR I = 0 TO 10
I1= I + 1
PRINT I1;
PRINT " => ";
INPUT TMP@
S@(I) = TMP@
NEXT I
PRINT
REM Reverse
HALFIMAX = 10 / 2
FOR I = 0 TO HALFIMAX
IREV = 10 - I
TMP@ = S@(I)
S@(I) = S@(IREV)
S@(IREV) = TMP@
NEXT I
REM Results
REM Leading spaces in printed numbers are removed
CLS
FOR I = 0 TO 10
PRINT "f(";
STMP$ = STR$(S@(I))
STMP$ = LTRIM$(STMP$)
PRINT STMP$;
PRINT ") = ";
N@ = S@(I)
GOSUB CALCF:
R@ = F@
IF R@ > 400 THEN
PRINT "overflow"
ELSE
STMP$ = STR$(R@)
STMP$ = LTRIM$(STMP$)
PRINT STMP$
ENDIF
NEXT I
END
 
CALCF:
REM Calculates f(N@)
REM Result in F@
X@ = ABS(N@)
GOSUB CALCSQRT:
F@ = 5.0 * N@
F@ = F@ * N@
F@ = F@ * N@
F@ = F@ + SQRT@
RETURN
 
CALCSQRT:
REM Calculates approximate (+- 0.00001) square root of X@ for X@ >= 0 (bisection method)
REM Result in SQRT@
A@ = 0.0
IF X@ >= 1.0 THEN
B@ = X@
ELSE
B@ = 1.0
ENDIF
L@ = B@ - A@
L@ = ABS(L@)
WHILE L@ > 0.00001
MIDDLE@ = A@ + B@
MIDDLE@ = MIDDLE@ / 2
MIDDLETO2@ = MIDDLE@ * MIDDLE@
IF MIDDLETO2@ < X@ THEN
A@ = MIDDLE@
ELSE
B@ = MIDDLE@
ENDIF
L@ = B@ - A@
L@ = ABS(L@)
WEND
SQRT@ = MIDDLE@
RETURN
</syntaxhighlight>
{{out}}
Enter the data.
<pre>
Enter 11 numbers.
1 => ?-5
2 => ?-3
3 => ?-2
4 => ?-1
5 => ?0
6 => ?1
7 => ?2
8 => ?3
9 => ?4
10 => ?5
11 => ?6
</pre>
Results.
<pre>
f(6.00000) = overflow
f(5.00000) = overflow
f(4.00000) = 321.99999
f(3.00000) = 136.73206
f(2.00000) = 41.41422
f(1.00000) = 5.99999
f(0.00000) = 0.00001
f(-1.00000) = -4.00001
f(-2.00000) = -38.58578
f(-3.00000) = -133.26794
f(-5.00000) = -622.76393
</pre>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">dim s(11)
print 'enter 11 numbers'
for i = 0 to 10
Line 303 ⟶ 606:
x = f(s[i])
if x > 400 then
print "---=< toooverflow large -->=-"
else
print x
Line 312 ⟶ 615:
function f(n)
return sqrt(abs(n))+5*n^3
end function</langsyntaxhighlight>
{{out}}
<pre>enter 11 numbers
Line 337 ⟶ 640:
f(-3)=-133.2679492
f(-4)=-318</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|BASIC256}}
<syntaxhighlight lang="qbasic">10 rem Trabb Pardo-Knuth algorithm
20 cls
30 dim s(10)
40 print "Enter 11 numbers."
50 for i = 0 to 10
60 print i+1;
70 input "=> ";s(i)
80 next i
90 print
160 'Results
170 for i = 10 to 0 step -1
180 print "f( " s(i)") = ";
190 r = f(s(i))
200 if r > 400 then
210 print "-=< overflow >=-"
220 else
230 print r
240 endif
250 next i
260 end
270 function f(n)
280 f = sqr(abs(n))+5*n^3
290 return
</syntaxhighlight>
{{out}}
<pre>Same as BASIC256 entry.</pre>
 
==={{header|Commodore BASIC}}===
{{trans|XBasic}}
{{works with|Commodore BASIC|4.5}}
<syntaxhighlight lang="basic">
10 REM TRABB PARDO-KNUTH ALGORITHM
20 REM USED "MAGIC NUMBERS" BECAUSE OF STRICT SPECIFICATION OF THE ALGORITHM.
30 DEF FNF(N)=SQR(ABS(N))+5*N*N*N
40 DIM S(10)
50 PRINT "ENTER 11 NUMBERS."
60 FOR I=0 TO 10
70 PRINT STR$(I+1);
80 INPUT S(I)
90 NEXT I
100 PRINT
110 REM REVERSE
120 FOR I=0 TO 10/2
130 TMP=S(I)
140 S(I)=S(10-I)
150 S(10-I)=TMP
160 NEXT I
170 REM RESULTS
180 FOR I=0 TO 10
190 PRINT "F(";STR$(S(I));") =";
200 R=FNF(S(I))
210 IF R>400 THEN PRINT " OVERFLOW":ELSE PRINT R
220 NEXT I
230 END
</syntaxhighlight>
{{out}}
<pre>
ENTER 11 NUMBERS.
1? -5
2? -3
3? -2
4? -1
5? 0
6? 1
7? 2
8? 3
9? 4
10? 5
11? 6
 
F( 6) = OVERFLOW
F( 5) = OVERFLOW
F( 4) = 322
F( 3) = 136.732051
F( 2) = 41.4142136
F( 1) = 6
F( 0) = 0
F(-1) =-4
F(-2) =-38.5857864
F(-3) =-133.267949
F(-5) =-622.763932
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' version 22-07-2017
' compile with: fbc -s console
 
Function f(n As Double) As Double
return Sqr(Abs(n)) + 5 * n ^ 3
End Function
 
' ------=< MAIN >=------
 
Dim As Double x, s(1 To 11)
Dim As Long i
 
For i = 1 To 11
Print Str(i);
Input " => ", s(i)
Next
 
Print
Print String(20,"-")
 
i -= 1
Do
Print "f(" + Str(s(i)) + ") = ";
x = f(s(i))
If x > 400 Then
Print "-=< overflow >=-"
Else
Print x
End If
i -= 1
Loop Until i < 1
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre>1 => -5
2 => -3
3 => -2
4 => -1
5 => 0
6 => 1
7 => 2
8 => 3
9 => 4
10 => 5
11 => 6
 
--------------------
f(6) = -=< overflow >=-
f(5) = -=< overflow >=-
f(4) = 322
f(3) = 136.7320508075689
f(2) = 41.41421356237309
f(1) = 6
f(0) = 0
f(-1) = -4
f(-2) = -38.58578643762691
f(-3) = -133.2679491924311
f(-5) = -622.7639320225002</pre>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">
// Trabb Pardo-Knuth algorithm
 
include "NSLog.incl"
 
local fn f( x as double ) as double
end fn = fn pow( abs(x), 0.5) + 5 * ( fn pow(x, 3) )
 
void local fn PardoKnuth( userInput as double )
double x = userInput
double y = fn f(x)
NSLog( @"f(%.4f)\t= \b", x )
if( y < 400.0 )
NSLog( @"%.4f", y )
else
NSLog( @"[Overflow]" )
end if
end fn
 
NSUInteger i
CFArrayRef numbers
 
numbers = @[@10, @-1, @1, @2, @3 ,@4, @4.3, @4.305, @4.303, @4.302, @4.301]
 
NSLog( @"Please enter 11 numbers:" )
for i = len(numbers) to 1 step -1
fn PardoKnuth( fn NumberDoubleValue( numbers[i-1] ) )
next
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Please enter 11 numbers:
f(4.3010) = 399.8863
f(4.3020) = [Overflow]
f(4.3030) = [Overflow]
f(4.3050) = [Overflow]
f(4.3000) = 399.6086
f(4.0000) = 322.0000
f(3.0000) = 136.7321
f(2.0000) = 41.4142
f(1.0000) = 6.0000
f(-1.0000) = -4.0000
f(10.0000) = [Overflow]
</pre>
 
=== {{header|GW-BASIC}} ===
{{trans|Commodore BASIC}}
{{works with|BASICA}}
<syntaxhighlight lang="gwbasic">
10 REM Trabb Pardo-Knuth algorithm
20 REM Used "magic numbers" because of strict specification of the algorithm.
30 DEF FNF(N) = SQR(ABS(N)) + 5 * N * N * N
40 DIM S(10)
50 PRINT "Enter 11 numbers."
60 FOR I% = 0 TO 10
70 PRINT STR$(I%+1);
80 INPUT S(I%)
90 NEXT I%
100 PRINT
110 REM Reverse
120 FOR I% = 0 TO 10 \ 2
130 TMP = S(I%): S(I%) = S(10 - I%): S(10 - I%) = TMP
160 NEXT I%
170 REM Results
180 FOR I% = 0 TO 10
190 PRINT "f(";STR$(S(I%));") =";
220 R = FNF(S(I%))
230 IF R > 400 THEN PRINT " overflow" ELSE PRINT R
240 NEXT I%
250 END
</syntaxhighlight>
{{out}}
<pre>
Enter 11 numbers.
1? -5
2? -3
3? -2
4? -1
5? 0
6? 1
7? 2
8? 3
9? 4
10? 5
11? 6
 
f( 6) = overflow
f( 5) = overflow
f( 4) = 322
f( 3) = 136.7321
f( 2) = 41.41422
f( 1) = 6
f( 0) = 0
f(-1) =-4
f(-2) =-38.58579
f(-3) =-133.268
f(-5) =-622.764
</pre>
 
==={{header|Liberty BASIC}}===
{{trans|XBasic}}
{{works with|Just BASIC|any}}
<syntaxhighlight lang="lb">
' Trabb Pardo-Knuth algorithm
' Used "magic numbers" because of strict specification of the algorithm.
dim s(10)
print "Enter 11 numbers."
for i = 0 to 10
print i + 1;
input " => "; s(i)
next i
print
' Reverse
for i = 0 to 10 / 2
tmp = s(i)
s(i) = s(10 - i)
s(10 - i) = tmp
next i
'Results
for i = 0 to 10
print "f("; s(i); ") = ";
r = f(s(i))
if r > 400 then
print "overflow"
else
print r
end if
next i
end
 
function f(n)
f = sqr(abs(n)) + 5 * n * n * n
end function
</syntaxhighlight>
{{out}}
<pre>
Enter 11 numbers.
1 => -5
2 => -3
3 => -2
4 => -1
5 => 0
6 => 1
7 => 2
8 => 3
9 => 4
10 => 5
11 => 6
 
f(6) = overflow
f(5) = overflow
f(4) = 322
f(3) = 136.732051
f(2) = 41.4142136
f(1) = 6
f(0) = 0
f(-1) = -4
f(-2) = -38.5857864
f(-3) = -133.267949
f(-5) = -622.763932
</pre>
 
==={{header|Minimal BASIC}}===
{{works with|QBasic}}
<syntaxhighlight lang="gwbasic">
10 REM Trabb Pardo-Knuth algorithm
20 REM Used "magic numbers" because of strict specification
30 REM of the algorithm.
40 DEF FNF(N) = SQR(ABS(N))+5*N*N*N
50 DIM S(10)
60 PRINT "Enter 11 numbers."
70 FOR I = 0 TO 10
80 PRINT I+1; "- Enter number";
90 INPUT S(I)
100 NEXT I
110 PRINT
120 REM Reverse
130 FOR I = 0 TO 10/2
140 LET T = S(I)
150 LET S(I) = S(10-I)
160 LET S(10-I) = T
170 NEXT I
180 REM Results
190 PRINT "num", "f(num)"
200 FOR I = 0 TO 10
210 LET R = FNF(S(I))
220 IF R>400 THEN 250
230 PRINT S(I), R
240 GOTO 260
250 PRINT S(I), " overflow"
260 NEXT I
270 END
</syntaxhighlight>
 
==={{header|Nascom BASIC}}===
{{trans|Commodore BASIC}}
{{works with|Nascom ROM BASIC|4.7}}
<syntaxhighlight lang="basic">
10 REM Trabb Pardo-Knuth algorithm
20 REM Used "magic numbers" because of strict
30 REM specification of the algorithm.
40 DEF FNF(N)=SQR(ABS(N))+5*N*N*N
50 DIM S(10)
60 PRINT "Enter 11 numbers."
70 FOR I=0 TO 10
80 PRINT STR$(I+1);
90 INPUT S(I)
100 NEXT I
110 PRINT
120 REM ** Reverse
130 FOR I=0 TO 10/2
140 TMP=S(I)
150 S(I)=S(10-I)
160 S(10-I)=TMP
170 NEXT I
180 REM ** Results
190 FOR I=0 TO 10
200 PRINT "f(";STR$(S(I));") =";
210 R=FNF(S(I))
220 IF R>400 THEN PRINT " overflow":GOTO 240
230 PRINT R
240 NEXT I
250 END
</syntaxhighlight>
{{out}}
<pre>
Enter 11 numbers.
1? -5
2? -3
3? -2
4? -1
5? 0
6? 1
7? 2
8? 3
9? 4
10? 5
11? 6
 
f( 6) = overflow
f( 5) = overflow
f( 4) = 322
f( 3) = 136.732
f( 2) = 41.4142
f( 1) = 6
f( 0) = 0
f(-1) =-4
f(-2) =-38.5858
f(-3) =-133.268
f(-5) =-622.764
</pre>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure.d f(x.d)
ProcedureReturn Pow(Abs(x), 0.5) + 5 * x * x * x
EndProcedure
 
Procedure split(i.s, delimeter.s, List o.d())
Protected index = CountString(i, delimeter) + 1 ;add 1 because last entry will not have a delimeter
While index > 0
AddElement(o())
o() = ValD(Trim(StringField(i, index, delimeter)))
index - 1
Wend
 
ProcedureReturn ListSize(o())
EndProcedure
 
Define i$, entriesAreValid = 0, result.d, output$
NewList numbers.d()
 
If OpenConsole()
Repeat
PrintN(#crlf$ + "Enter eleven numbers that are each separated by spaces or commas:")
i$ = Input(
i$ = Trim(i$)
If split(i$, ",", numbers.d()) < 11
ClearList(numbers())
If split(i$, " ", numbers.d()) < 11
PrintN("Not enough numbers were supplied.")
ClearList(numbers())
Else
entriesAreValid = 1
EndIf
Else
entriesAreValid = 1
EndIf
Until entriesAreValid = 1
ForEach numbers()
output$ = "f(" + RTrim(RTrim(StrD(numbers(), 3), "0"), ".") + ") = "
result.d = f(numbers())
If result > 400
output$ + "Too Large"
Else
output$ + RTrim(RTrim(StrD(result, 3), "0"), ".")
EndIf
PrintN(output$)
Next
Print(#crlf$ + #crlf$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</syntaxhighlight>
{{out}}
<pre>
Enter eleven numbers that are each separated by spaces or commas:
10, -1, 1, 2, 3, 4, 4.3, 4.305, 4.303, 4.302, 4.301
f(4.301) = 399.886
f(4.302) = Too Large
f(4.303) = Too Large
f(4.305) = Too Large
f(4.3) = 399.609
f(4) = 322
f(3) = 136.732
f(2) = 41.414
f(1) = 6
f(-1) = -4
f(10) = Too Large</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
<syntaxhighlight lang="qbasic">FUNCTION f (n!)
f = SQR(ABS(n)) + 5 * n ^ 3
END FUNCTION
 
DIM s(1 TO 11)
PRINT "enter 11 numbers"
FOR i = 1 TO 11
PRINT STR$(i);
INPUT " => ", s(i)
NEXT i
 
PRINT : PRINT STRING$(20, "-")
 
i = i - 1
DO
PRINT "f("; STR$(s(i)); ") = ";
x = f(s(i))
IF x > 400 THEN
PRINT "-=< overflow >=-"
ELSE
PRINT x
END IF
i = i - 1
LOOP UNTIL i < 1
END</syntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="vb">dim s(10)
print "Enter 11 numbers."
for i = 0 to 10
print i +1;
input " => "; s(i)
next i
print
'Results
for i = 10 to 0 step -1
print "f("; s(i); ") = ";
r = f(s(i))
if r > 400 then
print "-=< overflow >=-"
else
print r
end if
next i
end
 
function f(n)
f = sqr(abs(n)) + 5 * n * n * n
end function</syntaxhighlight>
{{out}}
<pre>Same as Liberty BASIC entry.</pre>
 
==={{header|Sinclair ZX81 BASIC}}===
Works with the unexpanded (1k RAM) ZX81
<syntaxhighlight lang="basic"> 10 DIM A(11)
20 PRINT "ENTER ELEVEN NUMBERS:"
30 FOR I=1 TO 11
40 INPUT A(I)
50 NEXT I
60 FOR I=11 TO 1 STEP -1
70 LET Y=SQR ABS A(I)+5*A(I)**3
80 IF Y<=400 THEN GOTO 110
90 PRINT A(I),"TOO LARGE"
100 GOTO 120
110 PRINT A(I),Y
120 NEXT I</syntaxhighlight>
{{out}}
<pre>ENTER ELEVEN NUMBERS:
2.8 111.43332
3.333 186.95529
1.01 6.1564926
2.55 84.503747
11 TOO LARGE
6 TOO LARGE
5 TOO LARGE
4 322
3 136.73205
2 41.414214
1 6</pre>
 
==={{header|True BASIC}}===
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">FUNCTION f (n)
LET f = SQR(ABS(n)) + 5 * n ^ 3
END FUNCTION
 
DIM s(1 TO 11)
PRINT "enter 11 numbers"
FOR i = 1 TO 11
PRINT STR$(i); " => ";
INPUT s(i)
NEXT i
 
PRINT
PRINT "--------------------"
 
LET i = i - 1
DO
PRINT "f("; STR$(s(i)); ") = ";
LET x = f(s(i))
IF x > 400 THEN
PRINT "-=< overflow >=-"
ELSE
PRINT x
END IF
LET i = i - 1
LOOP UNTIL i < 1
END</syntaxhighlight>
 
==={{header|VBScript}}===
<syntaxhighlight lang="vb">
Function tpk(s)
arr = Split(s," ")
For i = UBound(arr) To 0 Step -1
n = fx(CDbl(arr(i)))
If n > 400 Then
WScript.StdOut.WriteLine arr(i) & " = OVERFLOW"
Else
WScript.StdOut.WriteLine arr(i) & " = " & n
End If
Next
End Function
 
Function fx(x)
fx = Sqr(Abs(x))+5*x^3
End Function
 
'testing the function
WScript.StdOut.Write "Please enter a series of numbers:"
list = WScript.StdIn.ReadLine
tpk(list)
</syntaxhighlight>
 
{{Out}}
The number series was derived from the example of C.
<pre>
C:\>cscript /nologo tpk.vbs
Please enter 10 numbers:10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301
4.301 = 399.886299747727
4.302 = OVERFLOW
4.303 = OVERFLOW
4.305 = OVERFLOW
4.3 = 399.608644135333
4 = 322
3 = 136.732050807569
2 = 41.4142135623731
1 = 6
-1 = -4
10 = OVERFLOW
</pre>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">
' Trabb Pardo-Knuth algorithm
PROGRAM "tpkalgorithm"
VERSION "0.0001"
 
IMPORT "xma"
 
DECLARE FUNCTION Entry ()
INTERNAL FUNCTION SINGLE F(SINGLE n)
 
FUNCTION Entry ()
' Used "magic numbers" because of strict specification of the algorithm.
SINGLE s[10]
SINGLE tmp, r
UBYTE i
PRINT "Enter 11 numbers."
FOR i = 0 TO 10
PRINT i + 1;
s[i] = SINGLE(INLINE$(" => "))
NEXT i
PRINT
' Reverse
FOR i = 0 TO 10 / 2
tmp = s[i]
s[i] = s[10 - i]
s[10 - i] = tmp
NEXT i
'Results
FOR i = 0 TO 10
PRINT "f("; LTRIM$(STR$(s[i])); ") =";
r = F(s[i])
IF r > 400 THEN
PRINT " overflow"
ELSE
PRINT r
END IF
NEXT i
END FUNCTION
 
FUNCTION SINGLE F(SINGLE n)
RETURN SQRT(ABS(n)) + 5 * n * n *n
END FUNCTION
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>
Enter 11 numbers.
1 => -5
2 => -3
3 => -2
4 => -1
5 => 0
6 => 1
7 => 2
8 => 3
9 => 4
10 => 5
11 => 6
 
f(6) = overflow
f(5) = overflow
f(4) = 322
f(3) = 136.732
f(2) = 41.4142
f(1) = 6
f(0) = 0
f(-1) =-4
f(-2) =-38.5858
f(-3) =-133.268
f(-5) =-622.764
</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">sub f(n)
return sqr(abs(n)) + 5.0 * n ^ 3.0
end sub
 
dim s(11)
print "enter 11 numbers"
for i = 0 to 10
print i, " => ";
input "" s(i)
next i
 
print "\n--------------------"
for i = 10 to 0 step -1
print "f(", s(i), ") = ";
x = f(s(i))
if x > 400 then
print "--- too large ---"
else
print x
endif
next i
end</syntaxhighlight>
 
=={{header|C}}==
<syntaxhighlight lang="c">
<lang c>
#include<math.h>
#include<stdio.h>
Line 377 ⟶ 1,407:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>Please enter 11 numbers :10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301
Line 397 ⟶ 1,427:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <iostream>
#include <cmath>
Line 421 ⟶ 1,451:
}
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>Please enter 11 numbers!
Line 448 ⟶ 1,478:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun read-numbers ()
(princ "Enter 11 numbers (space-separated): ")
(let ((numbers '()))
Line 460 ⟶ 1,490:
(trabb-pardo-knuth (lambda (x) (+ (expt (abs x) 0.5) (* 5 (expt x 3))))
(lambda (x) (> x 400)))</langsyntaxhighlight>
 
{{Out}}
Line 477 ⟶ 1,507:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.math, std.conv, std.algorithm, std.array;
 
double f(in double x) pure nothrow {
Line 497 ⟶ 1,527:
writefln("f(%0.3f) = %s", x, y > 400 ? "Too large" : y.text);
}
}</langsyntaxhighlight>
{{out}}
<pre>Please enter eleven numbers on a line: 1 2 3 -4.55 5.1111 6 -7 8 9 10
Line 513 ⟶ 1,543:
f(2.000) = 41.4142
f(1.000) = 6</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
procedure DoPardoKnuth(Memo: TMemo; A: array of double);
var Y: double;
var I: integer;
var S: string;
 
function Func(T: double): double;
begin
Result:=Sqrt(abs(T))+ 5*T*T*T;
end;
 
begin
for I:=0 to High(A) do
begin
Y:=Func(A[I]);
if y > 400 then S:='Too Large'
else S:=Format('%f %f',[A[I],Y]);
Memo.Lines.Add(S);
end;
end;
 
procedure ShowPardoKnuth(Memo: TMemo);
begin
DoPardoKnuth(Memo, [1,2,3,4,5,6,7,8,9,10,11]);
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
1.00 6.00
2.00 41.41
3.00 136.73
4.00 322.00
Too Large
Too Large
Too Large
Too Large
Too Large
Too Large
Too Large
Elapsed Time: 12.547 ms.
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
print "Please enter 11 numbers :"
n[] = number strsplit input " "
print ""
print "Evaluating f(x) = |x|^0.5 + 5x^3 for the given inputs :"
for i = len n[] downto 1
r = sqrt abs n[i] + 5 * pow n[i] 3
write "f(" & n[i] & ") = "
if r > 400
print "Overflow!"
else
print r
.
.
# without this section, the input is interactive
input_data
10 -1 1 2 3 4 4.3 4.31 4.32 4.32 4.29
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(define (trabb-fun n)
(+ (* 5 n n n) (sqrt(abs n))))
Line 534 ⟶ 1,635:
(error 'incomplete-list numlist))))) ;; users cancel
(for-each check-trabb (reverse (take numlist 11))))
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="scheme">
(trabb)
;; input : (0 4 1 8 5 9 10 3 6 7 2)
Line 557 ⟶ 1,658:
(root g 0 10)
→ 4.301409367213084
</syntaxhighlight>
</lang>
 
=={{header|Ela}}==
Line 563 ⟶ 1,664:
Translation of OCaml version:
 
<langsyntaxhighlight lang="ela">open monad io number string
 
:::IO
Line 586 ⟶ 1,687:
do
putStrLn "Please enter 11 numbers:"
take_numbers 11 []</langsyntaxhighlight>
 
{{out}}
Line 612 ⟶ 1,713:
f(2) = 41.4142135623731
f(1) = 6</pre>
 
=={{header|Elena}}==
{{trans|C}}
ELENA 36.4x :
<langsyntaxhighlight lang="elena">import system'mathextensions;
import extensions.'math;
public program()
{
[
Array<real>[] inputs := V<new real>[](11).;
console .printLine("Please enter 11 numbers :").;
0for(int i till:= 0; i < 11; do(:i += 1)
[{
inputs[i] := console .readLine; toReal().toReal()
].};
console .printLine("Evaluating f(x) = |x|^0.5 + 5x^3 for the given inputs :").;
10for(int i to:= 10; i >= 0; do(:i -= 1)
[{
varreal r1result := sqrt(abs(inputs[i])) absolute;+ sqrt.5 * power(inputs[i], 3);
var r2 := inputs[i] power(3).
var r :=inputs[i] /*absolute;*/ sqrt + 5*r2.
real result := console.print(inputs[i]"f(", absolute; sqrt) + 5 * (inputs[i], power(3")=").;
console print("f(", inputs[i], ")=").
if (result > 400)
[{
console .printLine("Overflow!")
];}
[else
console printLine(result).{
] console.printLine(result)
] }
}
]</lang>
}</syntaxhighlight>
{{out}}
<pre>
Line 677 ⟶ 1,776:
=={{header|Elixir}}==
{{trans|Erlang}}
<langsyntaxhighlight lang="elixir">defmodule Trabb_Pardo_Knuth do
def task do
Enum.reverse( get_11_numbers )
Line 700 ⟶ 1,799:
end
 
Trabb_Pardo_Knuth.task</langsyntaxhighlight>
 
{{out}}
Line 719 ⟶ 1,818:
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( trabb_pardo_knuth ).
 
Line 743 ⟶ 1,842:
perform_operation_check_overflow( N, Result, Overflow ) when Result > Overflow -> alert( N );
perform_operation_check_overflow( N, Result, _Overflow ) -> io:fwrite( "f(~p) => ~p~n", [N, Result] ).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 762 ⟶ 1,861:
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
!Trabb Pardo-Knuth algorithm
PROGRAM TPK
Line 785 ⟶ 1,884:
END FOR
END PROGRAM
</syntaxhighlight>
</lang>
Numbers to be elaborated is included in the program with a DATA statement. You can substitute
this with an input keyboard like this
Line 795 ⟶ 1,894:
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
module ``Trabb Pardo - Knuth``
open System
Line 805 ⟶ 1,904:
| n when n <= 400.0 -> Console.WriteLine(n)
| _ -> Console.WriteLine("Overflow"))
</syntaxhighlight>
</lang>
{{out}}
<pre>fsharpi Program.fsx
Line 835 ⟶ 1,934:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.functions math.parser
prettyprint sequences splitting ;
IN: rosetta-code.trabb-pardo-knuth
Line 858 ⟶ 1,957:
[ string>number [ fn ] ?result print ] each ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 877 ⟶ 1,976:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: f(x) fdup fsqrt fswap 3e f** 5e f* f+ ;
 
4e2 fconstant f-too-big
Line 916 ⟶ 2,015:
 
: tpk ( -- )
get-it reverse-it do-it ;</langsyntaxhighlight>
{{out}}
<pre>
Line 950 ⟶ 2,049:
===Fortran 95===
{{works with|Fortran|95 and later}}
<langsyntaxhighlight lang="fortran">program tpk
implicit none
Line 979 ⟶ 2,078:
 
end function
end program</langsyntaxhighlight>
{{out}}
<pre> Input eleven numbers:
Line 997 ⟶ 2,096:
===Fortran I===
Written in FORTRAN I (1957), the original language quoted in the 1976 Donald Knuth & Luis Trabb Pardo’s study. Let’ note: no type declarations (INTEGER, REAL), no subprogram FUNCTION (only statement function), no logical IF, no END statement, and only Hollerith strings. The input data are on 2 80-column punched cards, only 1 to 72 columns are used so 6 values are read on the first card and 5 on the second card, so even input data could be numbered in the 73-80 area.
<langsyntaxhighlight lang="fortran">C THE TPK ALGORITH - FORTRAN I - 1957 TPK00010
FTPKF(X)=SQRTF(ABSF(X))+5.0*X**3 TPK00020
DIMENSION A(11) TPK00030
Line 1,013 ⟶ 2,112:
3 CONTINUE TPK00150
STOP 0 TPK00160
</syntaxhighlight>
</lang>
=={{header|FreeBASIC}}==
<lang freebasic>' version 22-07-2017
' compile with: fbc -s console
 
Function f(n As Double) As Double
return Sqr(Abs(n)) + 5 * n ^ 3
End Function
 
' ------=< MAIN >=------
 
Dim As Double x, s(1 To 11)
Dim As Long i
 
For i = 1 To 11
Print Str(i);
Input " => ", s(i)
Next
 
Print
Print String(20,"-")
 
i -= 1
Do
Print "f(" + Str(s(i)) + ") = ";
x = f(s(i))
If x > 400 Then
Print "-=< overflow >=-"
Else
Print x
End If
i -= 1
Loop Until i < 1
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
<pre>1 => -5
2 => -3
3 => -2
4 => -1
5 => 0
6 => 1
7 => 2
8 => 3
9 => 4
10 => 5
11 => 6
 
--------------------
f(6) = -=< overflow >=-
f(5) = -=< overflow >=-
f(4) = 322
f(3) = 136.7320508075689
f(2) = 41.41421356237309
f(1) = 6
f(0) = 0
f(-1) = -4
f(-2) = -38.58578643762691
f(-3) = -133.2679491924311
f(-5) = -622.7639320225002</pre>
 
=={{header|Go}}==
===Task/Wikipedia===
This solution follows the task description by reversing the sequence. It also rejects non-numeric input until 11 numbers are entered.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,118 ⟶ 2,154:
result := math.Sqrt(math.Abs(x)) + 5*x*x*x
return result, result > 400
}</langsyntaxhighlight>
{{out}}
The input is chosen to show some interesting boundary cases.
Line 1,137 ⟶ 2,173:
===TPK paper===
The original paper had no requirement to reverse the sequence in place, but instead processed the sequence in reverse order.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,160 ⟶ 2,196:
}
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Control.Monad (replicateM, mapM_)
 
f :: Floating a => a -> a
Line 1,178 ⟶ 2,214:
else print x) .
f) $
reverse x</langsyntaxhighlight>
{{out}}
<pre>Enter 11 numbers for evaluation
Line 1,210 ⟶ 2,246:
<tt>reverse(S)</tt> with <tt>S[*S to 1 by -1]</tt>.
 
<langsyntaxhighlight lang="unicon">procedure main()
S := []
writes("Enter 11 numbers: ")
Line 1,220 ⟶ 2,256:
procedure f(x)
return abs(x)^0.5 + 5*x^3
end</langsyntaxhighlight>
 
Sample run:
Line 1,243 ⟶ 2,279:
=={{header|Io}}==
 
<syntaxhighlight lang="io">
<lang Io>
// Initialize objects to be used
in_num := File standardInput()
Line 1,267 ⟶ 2,303:
)
)
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,304 ⟶ 2,340:
No checks are done if the input is actually numbers and if there are actually eleven of them. This doesn't affect the algorithm.
Additional checks can be done separately.
<langsyntaxhighlight Jlang="j">tpk=: 3 :0
smoutput 'Enter 11 numbers: '
t1=: ((5 * ^&3) + (^&0.5@* *))"0 |. _999&".;._1 ' ' , 1!:1 [ 1
smoutput 'Values of functions of reversed input: ' , ": t1
; <@(,&' ')@": ` ((<'user alert ')&[) @. (>&400)"0 t1
)</langsyntaxhighlight>
A possible use scenario:
<langsyntaxhighlight Jlang="j"> tpk ''
Enter 11 numbers:
1 2 3 4 5 6 7 8.8 _9 10.123 0
Values of functions of reversed input: 0 5189.96 _3642 3410.33 1717.65 1082.45 627.236 322 136.732 41.4142 6
0 user alert _3642 user alert user alert user alert user alert 322 136.732 41.4142 6
</syntaxhighlight>
</lang>
 
Note that the result of tpk is persisted in t1 and is also its explicit result rather than being an explicit output.
Line 1,322 ⟶ 2,358:
Here's an alternative approach:
 
<langsyntaxhighlight Jlang="j">get11numbers=: 3 :0
smoutput 'Enter 11 numbers: '
_&". 1!:1]1
Line 1,331 ⟶ 2,367:
overflow400=: 'user alert'"_`":@.(<:&400)"0
 
tpk=: overflow400@f_x@|.@get11numbers</langsyntaxhighlight>
 
And, here's this alternative in action:
 
<langsyntaxhighlight Jlang="j"> tpk''
Enter 11 numbers:
1 2 3 4 5 6 7 8.8 _9 10.123 0
Line 1,348 ⟶ 2,384:
136.732
41.4142
6</langsyntaxhighlight>
 
(clearly, other alternatives are also possible).
Line 1,356 ⟶ 2,392:
=={{header|Java}}==
 
<langsyntaxhighlight lang="java">/**
* Alexander Alvonellos
*/
Line 1,392 ⟶ 2,428:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,423 ⟶ 2,459:
 
=== Spidermonkey ===
<langsyntaxhighlight lang="javascript">#!/usr/bin/env js
 
function main() {
Line 1,459 ⟶ 2,495:
 
main();
</syntaxhighlight>
</lang>
 
Results:
Line 1,489 ⟶ 2,525:
jq does not currently have an interactive mode allowing a prompt to be issued first,
and so the initial prompt is implemented here using "echo", in keeping with the jq approach of dovetailing with other command-line tools.
<langsyntaxhighlight lang="jq">def f:
def abs: if . < 0 then -. else . end;
def power(x): (x * log) | exp;
Line 1,499 ⟶ 2,535:
else error("The number of numbers was not 11.")
end
| .[] # print one result per line</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ echo "Enter 11 numbers on one line; when done, enter the end-of-file character:" ;\
jq -M -s -R -f Trabb_Pardo-Knuth_algorithm.jq
> Enter 11 numbers on one line; when done, enter the end-of-file character:
Line 1,515 ⟶ 2,551:
136.73205080756887
41.41421356237309
6</langsyntaxhighlight>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">f(x) = √abs(x) + 5x^3
for i in reverse(split(readline()))
v = f(parse(Int, i))
println("$(i): ", v > 400 ? "TOO LARGE" : v)
end</syntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 11
11: TOO LARGE
10: TOO LARGE
9: TOO LARGE
8: TOO LARGE
7: TOO LARGE
6: TOO LARGE
5: TOO LARGE
4: 322.0
3: 136.73205080756887
2: 41.41421356237309
1: 6.0</pre>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun f(x: Double) = Math.sqrt(Math.abs(x)) + 5.0 * x * x * x
Line 1,546 ⟶ 2,602:
println(v)
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,581 ⟶ 2,637:
</pre>
 
=={{header|JuliaKsh}}==
<syntaxhighlight lang="ksh">
<lang julia>f(x) = abs(x)^.5 + 5x^3
#!/bin/ksh
for i in map(parseint,reverse(split(readline())))
 
v = f(i)
# Trabb Pardo–Knuth algorithm
println("$i: ", v > 400 ? "TOO LARGE" : v)
 
end</lang>
# # Variables:
{{out}}
#
<pre>1 2 3 4 5 6 7 8 9 10 11
integer NUM_ELE=11
11: TOO LARGE
typeset -F FUNC_LIMIT=400
10: TOO LARGE
 
9: TOO LARGE
# # Functions:
8: TOO LARGE
#
7: TOO LARGE
# # Function _input(_arr) - Ask for user input, build array
6: TOO LARGE
#
5: TOO LARGE
function _input {
4: 322.0
typeset _arr ; nameref _arr="$1"
3: 136.73205080756887
typeset _i ; integer _i
2: 41.41421356237309
 
1: 6.0</pre>
clear ; print "Please input 11 numbers..."
for ((_i=1 ; _i<=NUM_ELE ; _i++)); do
read REPLY?"${_i}: "
[[ $REPLY != {,1}(-)+(\d){,1}(.)*(\d) ]] && ((_i--)) && continue
_arr+=( $REPLY )
done
}
 
# # Function _function() - Apply |x|^0.5 + 5x^3
# # note: >400 creates an overflow situation
#
function _function {
typeset _x ; _x=$1
 
(( _result = sqrt(abs(${_x})) + 5 * _x * _x * _x ))
(( _result <= $FUNC_LIMIT )) && echo ${_result} && return 0
return 1
}
 
######
# main #
######
 
typeset -a inputarr
_input inputarr
integer i
printf "%s\n\n" "Evaluating f(x) = |x|^0.5 + 5x^3 for the given inputs :"
for (( i=NUM_ELE-1; i>=0; i-- )); do
result=$(_function ${inputarr[i]})
if (( $? )); then
printf "%s\n" "Overflow"
else
printf "%s\n" "${result}"
fi
done</syntaxhighlight>
{{out}}<pre>
Please input 11 numbers...
1: 10
2: -1
3: 1
4: 2
5: 3
6: 4
7: 4.3
8: 4.305
9: 4.303
10: 4.302
11: 4.301
Evaluating f(x) = |x|^0.5 + 5x^3 for the given inputs :
 
399.8862997477268
Overflow
Overflow
Overflow
399.608644135332772
322
136.732050807568877
41.414213562373095
6
-4
Overflow</pre>
 
=={{header|Lua}}==
===Implementation of task description===
<langsyntaxhighlight Lualang="lua">function f (x) return math.abs(x)^0.5 + 5*x^3 end
 
function reverse (t)
Line 1,620 ⟶ 2,737:
result = f(x)
if result > 400 then print("Overflow!") else print(result) end
end</langsyntaxhighlight>
{{out}}
<pre>Enter 11 numbers...
Line 1,647 ⟶ 2,764:
 
===Line-for-line from TPK paper===
<langsyntaxhighlight Lualang="lua">local a, y = {}
function f (t)
return math.sqrt(math.abs(t)) + 5*t^3
Line 1,656 ⟶ 2,773:
if y > 400 then print(i, "TOO LARGE")
else print(i, y) end
end</langsyntaxhighlight>
{{out}}
<pre>1
Line 1,680 ⟶ 2,797:
1 41.414213562373
0 6</pre>
 
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Input11 {
Flush ' empty stack
Line 1,711 ⟶ 2,827:
Input11
Run
</syntaxhighlight>
</lang>
 
To collect the output in clipboard. Global variables need <= to assign values, and document append values using = or <= (for globals)
Line 1,717 ⟶ 2,833:
Output with "," for decimals (Locale 1032). We can change this using statement Locale 1033
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Global a$
Document a$ ' make a$ as a document - string with paragraphs
Line 1,742 ⟶ 2,858:
Run 1, 2, 3, -4.55,5.1111, 6, -7, 8, 9, 10, 11
Clipboard a$
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,775 ⟶ 2,891:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">seqn := ListTools:-Reverse([parse(Maplets[Display](Maplets:-Elements:-Maplet(Maplets:-Elements:-InputDialog['ID1']("Enter a sequence of numbers separated by comma", 'onapprove' = Maplets:-Elements:-Shutdown(['ID1']), 'oncancel' = Maplets:-Elements:-Shutdown())))[1])]):
f:= x -> abs(x)^0.5 + 5*x^3:
for item in seqn do
Line 1,784 ⟶ 2,900:
print(result):
end if:
end do:</langsyntaxhighlight>
{{Out|Usage}}
Input:1,2,3,4,5,6,7,8,9,10,11
Line 1,799 ⟶ 2,915:
6.</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
 
<syntaxhighlight lang="mathematica">numbers=RandomReal[{-2,6},11]
=={{header|Mathematica}}==
<lang Mathematica>numbers=RandomReal[{-2,6},11]
tpk[numbers_,overflowVal_]:=Module[{revNumbers},
revNumbers=Reverse[numbers];
Line 1,815 ⟶ 2,930:
]
]
tpk[numbers,400]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,830 ⟶ 2,945:
f[1.18367]= 9.38004
f[0.470145]= 1.20527
</pre>
 
=={{header|MATLAB}}==
{{trans|Julia}}
<syntaxhighlight lang="MATLAB}}">
clear all;close all;clc;
 
% Define the function f(x)
f = @(x) sqrt(abs(x)) + 5 * x^3;
 
% Read a line of input, split it into elements, convert to numbers
inputLine = input('', 's');
numbers = str2double(strsplit(inputLine));
 
% Process each number in reverse order
for i = length(numbers):-1:1
value = f(numbers(i));
if value > 400
fprintf('%g: TOO LARGE\n', numbers(i));
else
fprintf('%g: %g\n', numbers(i), value);
end
end
</syntaxhighlight>
{{out}}
<pre>
1 2 3 4 5 6 7 8 9 10 11
11: TOO LARGE
10: TOO LARGE
9: TOO LARGE
8: TOO LARGE
7: TOO LARGE
6: TOO LARGE
5: TOO LARGE
4: 322
3: 136.732
2: 41.4142
1: 6
</pre>
 
=={{header|min}}==
{{works with|min|0.19.3}}
<syntaxhighlight lang="min">((0 <) (-1 *) when) :abs
(((abs 0.5 pow) (3 pow 5 * +)) cleave) :fn
"Enter 11 numbers:" puts!
(gets float) 11 times
(fn (400 <=) (pop "Overflow") unless puts!) 11 times</syntaxhighlight>
{{out}}
<pre>
Enter 11 numbers:
1
2
3
4
5
6
7
8
9
10
11
Overflow
Overflow
Overflow
Overflow
Overflow
Overflow
Overflow
322.0
136.7320508075689
41.41421356237309
6.0
</pre>
 
=={{header|Modula-2}}==
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}}
<syntaxhighlight lang="modula2">
MODULE TrabbPardoKnuth;
 
FROM STextIO IMPORT
SkipLine, WriteString, WriteLn;
FROM SRealIO IMPORT
ReadReal, WriteFixed;
FROM SWholeIO IMPORT
WriteInt;
FROM RealMath IMPORT
sqrt;
 
CONST
Size = 11;
 
TYPE
TSequence = ARRAY [1 .. Size] OF REAL;
 
VAR
S: TSequence;
I: CARDINAL;
Result: REAL;
 
PROCEDURE ReverseSequence(VAR S: TSequence);
VAR
I: CARDINAL;
Temp: REAL;
BEGIN
FOR I := 1 TO Size DIV 2 DO
Temp := S[I];
S[I] := S[Size - I + 1];
S[Size - I + 1] := Temp;
END;
END ReverseSequence;
 
PROCEDURE DoOperation(Item: REAL): REAL;
BEGIN
RETURN sqrt(ABS(Item)) + 5.0 * Item * Item * Item;
END DoOperation;
 
BEGIN
WriteString("Enter 11 numbers.");
WriteLn;
FOR I := 1 TO Size DO
WriteInt(I, 2);
WriteString(": ");
ReadReal(S[I]);
SkipLine
END;
ReverseSequence(S);
WriteLn;
FOR I := 1 TO Size DO
Result := DoOperation(S[I]);
WriteString("f(");
WriteFixed(S[I], 3, 8);
WriteString(") = ");
IF Result > 400.0 THEN
WriteString("overflow");
ELSE
WriteFixed(Result, 3, 8)
END;
WriteLn;
END;
END TrabbPardoKnuth.
</syntaxhighlight>
{{out}}
<pre>
Enter 11 numbers.
1: -5
2: -3
3: -2
4: -1
5: 0
6: 1
7: 2
8: 3
9: 4
10: 5
11: 6
 
f( 6.000) = overflow
f( 5.000) = overflow
f( 4.000) = 322.000
f( 3.000) = 136.732
f( 2.000) = 41.414
f( 1.000) = 6.000
f( 0.000) = 0.000
f( -1.000) = -4.000
f( -2.000) = -38.586
f( -3.000) = -133.268
f( -5.000) = -622.764
</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">import math, rdstdin, strutils, algorithm, sequtils
 
proc f(x: float): float = x.abs.pow(0.5) + 5 * x.pow(3)
 
proc ask: seq[float] =
readLineFromStdin("\n1111 numbers: ").strip.split[0..10].map(parseFloat)
 
var s = ask()
reverse s
for x in s:
let result = f (x)
stdout.writeecho " ",x, ": ", if result > 400: "TOO LARGE!" else: $result</syntaxhighlight>
 
echo ""</lang>
{{out}}
<pre>11 numbers: 1 2 3 4 5 6 7 8 9 10 11
11 numbers: 1 2 3 4 5 6 7 8 9 10 11
11.0:TOO LARGE! 10.0:TOO LARGE! 9.0:TOO LARGE! 8.0:TOO LARGE! 7.0:TOO LARGE! 6.0:TOO LARGE! 5.0:TOO LARGE! 4.0:322.0 3.0:136.7320508075689 2.0:41.41421356237309 1.0:6.0</pre>
11.0: TOO LARGE!
10.0: TOO LARGE!
9.0: TOO LARGE!
8.0: TOO LARGE!
7.0: TOO LARGE!
6.0: TOO LARGE!
5.0: TOO LARGE!
4.0: 322.0
3.0: 136.7320508075689
2.0: 41.41421356237309
1.0: 6.0</pre>
 
=={{header|Objective-C}}==
{{works with|Mac OS X|10.6+}}
 
<langsyntaxhighlight lang="objc">//
// TPKA.m
// RosettaCode
Line 1,895 ⟶ 3,188:
return 0;
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,926 ⟶ 3,219:
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let f x = sqrt x +. 5.0 *. (x ** 3.0)
let p x = x < 400.0
 
Line 1,937 ⟶ 3,230:
then Printf.printf "f(%g) = %g\n%!" x res
else Printf.eprintf "f(%g) :: Overflow\n%!" x
) (List.rev lst)</langsyntaxhighlight>
 
{{out}}
Line 1,970 ⟶ 3,263:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">{
print("11 numbers: ");
v=vector(11, n, eval(input()));
v=apply(x->x=sqrt(abs(x))+5*x^3;if(x>400,"overflow",x), v);
vector(11, i, v[12-i])
}</langsyntaxhighlight>
{{out}}
<pre>11 numbers:
Line 1,994 ⟶ 3,287:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">print "Enter 11 numbers:\n";
for ( 1..11 ) {
$number = <STDIN>;
Line 2,004 ⟶ 3,297:
my $result = sqrt( abs($n) ) + 5 * $n**3;
printf "f( %6.2f ) %s\n", $n, $result > 400 ? " too large!" : sprintf "= %6.2f", $result
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,031 ⟶ 3,324:
f( 2.00 ) = 41.41</pre>
 
=={{header|Perl 6}}==
<lang perl6>my @nums = prompt("Please type 11 space-separated numbers: ").words
until @nums == 11;
for @nums.reverse -> $n {
my $r = $n.abs.sqrt + 5 * $n ** 3;
say "$n\t{ $r > 400 ?? 'Urk!' !! $r }";
}</lang>
{{out}}
<pre>Please type 11 space-separated numbers: 10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301
4.301 399.88629974772681
4.302 Urk!
4.303 Urk!
4.305 Urk!
4.3 399.60864413533278
4 322
3 136.73205080756887
2 41.414213562373092
1 6
-1 -4
10 Urk!</pre>
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function f(atom x)
<span style="color: #008080;">function</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
return sqrt(abs(x))+5*power(x,3)
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">))+</span><span style="color: #000000;">5</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
string s = substitute(prompt_string("Enter 11 numbers:"),","," ")
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">fake_prompt</span><span style="color: #0000FF;">=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
sequence S = scanf(s,"%f %f %f %f %f %f %f %f %f %f %f")
<span style="color: #008080;">if</span> <span style="color: #000000;">fake_prompt</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Enter 11 numbers:%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if length(S)!=1 then puts(1,"not 11 numbers") abort(0) end if
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">","</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)</span>
S = reverse(S[1])
<span style="color: #004080;">sequence</span> <span style="color: #000000;">S</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%f %f %f %f %f %f %f %f %f %f %f"</span><span style="color: #0000FF;">)</span>
for i=1 to length(S) do
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">S</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"not 11 numbers"</span><span style="color: #0000FF;">)</span> <span style="color: #7060A8;">abort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
atom result = f(S[i])
<span style="color: #000000;">S</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">S</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
if result>400 then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">S</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
printf(1,"f(%g):overflow\n",{S[i]})
<span style="color: #004080;">atom</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">(</span><span style="color: #000000;">S</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
else
<span style="color: #008080;">if</span> <span style="color: #000000;">result</span><span style="color: #0000FF;">></span><span style="color: #000000;">400</span> <span style="color: #008080;">then</span>
printf(1,"f(%g):%g\n",{S[i],result})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"f(%g):overflow\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">S</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]})</span>
end if
<span style="color: #008080;">else</span>
end for</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"f(%g):%g\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">S</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">result</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000080;font-style:italic;">--test(prompt_string("Enter 11 numbers:"),false)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1,2,3,4,5,6,7,8,9,10,11"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"0.470145,1.18367,2.36984,4.86759,2.40274,5.48793,3.30256,5.34393,4.21944,2.23501,-0.0200707"</span><span style="color: #0000FF;">}</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">,</span><span style="color: #000000;">test</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{Out}}
<pre>
Line 2,109 ⟶ 3,393:
f(0.470145):1.20527
</pre>
 
=={{header|PHP}}==
<syntaxhighlight lang="php">
<?php
// Trabb Pardo-Knuth algorithm
// Used "magic numbers" because of strict specification of the algorithm.
 
function f($n)
{
return sqrt(abs($n)) + 5 * $n * $n * $n;
}
 
$sArray = [];
echo "Enter 11 numbers.\n";
for ($i = 0; $i <= 10; $i++) {
echo $i + 1, " - Enter number: ";
array_push($sArray, (float)fgets(STDIN));
}
echo PHP_EOL;
// Reverse
$sArray = array_reverse($sArray);
// Results
foreach ($sArray as $s) {
$r = f($s);
echo "f(", $s, ") = ";
if ($r > 400)
echo "overflow\n";
else
echo $r, PHP_EOL;
}
?>
</syntaxhighlight>
{{out}}
<pre>
Enter 11 numbers.
1 - Enter number: -5
2 - Enter number: -3
3 - Enter number: -2
4 - Enter number: -1
5 - Enter number: 0
6 - Enter number: 1
7 - Enter number: 2
8 - Enter number: 3
9 - Enter number: 4
10 - Enter number: 5
11 - Enter number: 6
 
f(6) = overflow
f(5) = overflow
f(4) = 322
f(3) = 136.73205080757
f(2) = 41.414213562373
f(1) = 6
f(0) = 0
f(-1) = -4
f(-2) = -38.585786437627
f(-3) = -133.26794919243
f(-5) = -622.7639320225
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">import util.
 
go =>
L = [I.parse_term() : I in split(read_line())],
S = [[I,cond(F<=400,F,'TOO LARGE')] : I in L.len..-1..1, F=f(L[I])],
println(S),
nl.
 
f(T) = sqrt(abs(T)) + 5*T**3.</syntaxhighlight>
 
Example run:
{{out}}
<pre>
$ echo "1 2 3 4 5 6 7 8 9 10 11" | picat tpk.pi
[[11,TOO LARGE],[10,TOO LARGE],[9,TOO LARGE],[8,TOO LARGE],[7,TOO LARGE],[6,TOO LARGE],[5,TOO LARGE],[4,322.0],[3,136.732050807568868],[2,41.414213562373092],[1,6.0]]</pre>
 
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de f (X)
(+ (sqrt (abs X)) (* 5 X X X)) )
 
Line 2,120 ⟶ 3,481:
(for X (reverse (make (do 11 (link (read)))))
(when (> (f X) 400)
(prinl "TOO LARGE") ) ) )</langsyntaxhighlight>
Test:
<langsyntaxhighlight PicoLisplang="picolisp">Input 11 numbers: 1 2 3 4 5 6 7 8 9 10 11
f : 11
f = 6658
Line 2,151 ⟶ 3,512:
f = 41
f : 1
f = 6</langsyntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
Trabb: Procedure options (main); /* 11 November 2013 */
 
Line 2,180 ⟶ 3,541:
 
end Trabb;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,212 ⟶ 3,573:
=={{header|PL/M}}==
Assuming the existence of suitable external library routines.
<langsyntaxhighlight lang="plm">TPK: DO;
/* external I/O and real mathematical routines */
WRITE$STRING: PROCEDURE( S ) EXTERNAL; DECLARE S POINTER; END;
Line 2,239 ⟶ 3,600:
END MAIN;
 
END TPK;</langsyntaxhighlight>
{{out}}
<pre>
Line 2,257 ⟶ 3,618:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Get-Tpk
{
Line 2,307 ⟶ 3,668:
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$tpk = 1..11 | Get-Tpk
$tpk
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,329 ⟶ 3,690:
</pre>
Sort back to ascending order ignoring '''Overflow''' results:
<syntaxhighlight lang="powershell">
<lang PowerShell>
$tpk | where result -ne overflow | sort number
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,341 ⟶ 3,702:
4 322
</pre>
 
=={{header|PureBasic}}==
<lang purebasic>Procedure.d f(x.d)
ProcedureReturn Pow(Abs(x), 0.5) + 5 * x * x * x
EndProcedure
 
Procedure split(i.s, delimeter.s, List o.d())
Protected index = CountString(i, delimeter) + 1 ;add 1 because last entry will not have a delimeter
While index > 0
AddElement(o())
o() = ValD(Trim(StringField(i, index, delimeter)))
index - 1
Wend
 
ProcedureReturn ListSize(o())
EndProcedure
 
Define i$, entriesAreValid = 0, result.d, output$
NewList numbers.d()
 
If OpenConsole()
Repeat
PrintN(#crlf$ + "Enter eleven numbers that are each separated by spaces or commas:")
i$ = Input(
i$ = Trim(i$)
If split(i$, ",", numbers.d()) < 11
ClearList(numbers())
If split(i$, " ", numbers.d()) < 11
PrintN("Not enough numbers were supplied.")
ClearList(numbers())
Else
entriesAreValid = 1
EndIf
Else
entriesAreValid = 1
EndIf
Until entriesAreValid = 1
ForEach numbers()
output$ = "f(" + RTrim(RTrim(StrD(numbers(), 3), "0"), ".") + ") = "
result.d = f(numbers())
If result > 400
output$ + "Too Large"
Else
output$ + RTrim(RTrim(StrD(result, 3), "0"), ".")
EndIf
PrintN(output$)
Next
Print(#crlf$ + #crlf$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</lang>
{{out}}
<pre>
Enter eleven numbers that are each separated by spaces or commas:
10, -1, 1, 2, 3, 4, 4.3, 4.305, 4.303, 4.302, 4.301
f(4.301) = 399.886
f(4.302) = Too Large
f(4.303) = Too Large
f(4.305) = Too Large
f(4.3) = 399.609
f(4) = 322
f(3) = 136.732
f(2) = 41.414
f(1) = 6
f(-1) = -4
f(10) = Too Large</pre>
 
=={{header|Python}}==
===Functional===
<langsyntaxhighlight lang="python">Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> def f(x): return abs(x) ** 0.5 + 5 * x**3
Line 2,422 ⟶ 3,714:
11 numbers: 1 2 3 4 5 6 7 8 9 10 11
11:TOO LARGE!, 10:TOO LARGE!, 9:TOO LARGE!, 8:TOO LARGE!, 7:TOO LARGE!, 6:TOO LARGE!, 5:TOO LARGE!, 4:322.0, 3:136.73205080756887, 2:41.41421356237309, 1:6.0
>>> </langsyntaxhighlight>
 
===Procedural===
{{Works with|Python|3.10}}
<lang python>def f(x):
<syntaxhighlight lang="python">import math
return abs(x) ** 0.5 + 5 * x**3
 
def f(x):
return math.sqrt(abs(x)) + 5 * x**3
 
def askask_numbers(n=11):
print(f'Enter {n} numbers:')
return [float(y)
return (float(input('>')) for y_ in inputrange('\n11 numbers: 'n).strip().split()[:11]]
 
if __name__ == '__main__':
sfor =x askin ask_numbers().reverse():
if (result := f(x)) > 400:
s.reverse()
print(f'f({x}): overflow')
for x in s:
result = f(x)
if result > 400:
print(' %s:%s' % (x, "TOO LARGE!"), end='')
else:
print(f' %s:%s' % f({x, result}), end=' {result}')</syntaxhighlight>
{{out}}
print('')</lang>
<pre>Enter 11 numbers:
{{out|Sample output}}
>1
<pre>
>532
11 numbers: 1 2 3 4 5 6 7 8 9 10 11
>465
11.0:TOO LARGE! 10.0:TOO LARGE! 9.0:TOO LARGE! 8.0:TOO LARGE! 7.0:TOO LARGE! 6.0:TOO LARGE! 5.0:TOO LARGE! 4.0:322.0 3.0:136.73205080756887 2.0:41.41421356237309 1.0:6.0</pre>
>0
>-8456
>1
>2
>3
>4
>5
>98465465
f(98465465.0): overflow
f(5.0): overflow
f(4.0) = 322.0
f(3.0) = 136.73205080756887
f(2.0) = 41.41421356237309
f(1.0) = 6.0
f(-8456.0) = -3023186413988.0435
f(0.0) = 0.0
f(465.0): overflow
f(532.0): overflow
f(1.0) = 6.0</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ $ "bigrat.qky" loadfile ] now!
 
[ $->v drop
2dup vabs 10 vsqrt drop
2swap 2dup 2dup
v* v* 5 n->v v* v+ ] is function ( $ --> n/d )
 
[ $ "Please enter 11 numbers: "
input nest$
reverse
witheach
[ function
400 n->v 2over v< iff
[ 2drop say "overflow" ]
else
[ 7 point$ echo$ ]
sp ]
cr ] is task ( --> )</syntaxhighlight>
 
{{out}}
 
As a dialogue in the Quackery shell.
 
<pre>/O> task
...
Please enter 11 numbers: 10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301
399.8862997 overflow overflow overflow 399.6086441 322 136.7320508 41.4142136 6 -4 overflow
</pre>
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">S <- scan(n=11)
 
f <- function(x) sqrt(abs(x)) + 5*x^3
Line 2,458 ⟶ 3,800:
else
print(res)
}</langsyntaxhighlight>
 
{{out|Sample output}}
Line 2,479 ⟶ 3,821:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 2,492 ⟶ 3,834:
(displayln "Overflow!")
(printf "f(~a) = ~a\n" x res)))
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,519 ⟶ 3,861:
f(1) = 6
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>my @nums = prompt("Please type 11 space-separated numbers: ").words
until @nums == 11;
for @nums.reverse -> $n {
my $r = $n.abs.sqrt + 5 * $n ** 3;
say "$n\t{ $r > 400 ?? 'Urk!' !! $r }";
}</syntaxhighlight>
{{out}}
<pre>Please type 11 space-separated numbers: 10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301
4.301 399.88629974772681
4.302 Urk!
4.303 Urk!
4.305 Urk!
4.3 399.60864413533278
4 322
3 136.73205080756887
2 41.414213562373092
1 6
-1 -4
10 Urk!</pre>
 
=={{header|REXX}}==
The REXX language doesn't have a &nbsp; '''sqrt''' &nbsp; function, so a RYO version is included here. &nbsp; &nbsp; ['''RYO''' &nbsp; = &nbsp; '''<u>R'''</u>oll '''<u>Y'''</u>our '''<u>O'''</u>wn.]
<br><br>It could be noted that almost half of this program is devoted to prompting, parsing and validating of the (input) numbers,
<br>not to mention some hefty code to support right-justified numbers such that they are aligned when displayed.
<langsyntaxhighlight lang="rexx">/*REXX program implements the Trabb─Pardo-Knuth algorithm for N numbers (default is 11).*/
numeric digits 200 /*the number of digits precision to use*/
parse arg N .; if N=='' | N=="," then N=11 /*Not specified? Then use the default.*/
Line 2,571 ⟶ 3,935:
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ % 2
do j=0 while h>9; m.j=h; h=h % 2 + 1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; return g</langsyntaxhighlight>
{{out|output|text=&nbsp; when prompted, using the input of: &nbsp; &nbsp; <tt> 5 &nbsp; 3.3 &nbsp; 3 &nbsp; 2e-1 &nbsp; 1 &nbsp; 0 &nbsp; -1 &nbsp; -222 &nbsp; -33 &nbsp; 4.0004 &nbsp; +5 </tt>}}
<pre>
Line 2,597 ⟶ 3,961:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Trabb Pardo–Knuth algorithm
 
Line 2,626 ⟶ 3,990:
func f(n)
return sqrt(fabs(n)) + 5 * pow(n, 3)
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,653 ⟶ 4,017:
f(-5) = -622.764
 
</pre>
 
=={{header|RPL}}==
Idiomatic RPL is based on use of stack whenever possible, short code and minimalist UX.
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ DUP ABS √ SWAP 3 ^ + ≫ ''''FUNC'''' STO
≪ "Push 11 numbers in stack, then CONT"
HALT 11 →LIST → s
≪ DROP 11 1
'''FOR''' j s j GET
'''FUNC'''
DUP 400 >
"Too large!"
ROT '''IFTE'''
-1 '''STEP'''
≫ ≫ ''''TPK'''' STO
|
'''FUNC''' ''( x -- sqrt(abs(x))+x^3 )''
'''TPK''' ''( -- report )''
ask for 11 numbers to be read into a sequence S
reverse sequence S
for each item in sequence S
call a function to do an operation
if result overflows
alert user
else print result
|}
<code>CONT</code> is not an instruction but a command, triggered by a specific keystroke.
On RPL versions used by HP-48 calculators and beyond, the first 2 lines of TPK can be replaced by
≪ { }
1 11 '''START''' "Enter a number" { "?" V } INPUT + '''NEXT''' → s
to slightly improve the collection of the 11 numbers, avoiding the need for the <code>CONT</code> command.
{{in}}
<pre>
TPK 100 3 0 1 0 1 0 1 0 1 10
</pre>
{{out}}
<pre>
11: "Too large!"
10: "28.7320508076"
9: "0"
8: "2"
7: "0"
6: "2"
5: "0"
4: "2"
3: "0"
2: "2"
1: "Too large!"
</pre>
 
=={{header|Ruby}}==
 
<langsyntaxhighlight lang="ruby">def f(x) x.abs ** 0.5 + 5 * x ** 3 end
 
puts "Please enter 11 numbers:"
Line 2,666 ⟶ 4,088:
res = f(n)
puts res > 400 ? "Overflow!" : res
end</langsyntaxhighlight>
 
{{out}}
Line 2,697 ⟶ 4,119:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
use std::io::{self, BufRead};
 
Line 2,728 ⟶ 4,150:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,758 ⟶ 4,180:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object TPKa extends App {
final val numbers = scala.collection.mutable.MutableList[Double]()
final val in = new java.util.Scanner(System.in)
Line 2,783 ⟶ 4,205:
private final val THRESHOLD = 400D
private final val CAPACITY = 11
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="ruby">var nums; do {
nums = Sys.readln("Please type 11 space-separated numbers: ").nums
} while(nums.len != 11)
Line 2,794 ⟶ 4,216:
var r = (n.abs.sqrt + (5 * n**3));
say "#{n}\t#{ r > 400 ? 'Urk!' : r }";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,810 ⟶ 4,232:
10 Urk!
</pre>
 
=={{header|Sinclair ZX81 BASIC}}==
Works with the unexpanded (1k RAM) ZX81
<lang basic> 10 DIM A(11)
20 PRINT "ENTER ELEVEN NUMBERS:"
30 FOR I=1 TO 11
40 INPUT A(I)
50 NEXT I
60 FOR I=11 TO 1 STEP -1
70 LET Y=SQR ABS A(I)+5*A(I)**3
80 IF Y<=400 THEN GOTO 110
90 PRINT A(I),"TOO LARGE"
100 GOTO 120
110 PRINT A(I),Y
120 NEXT I</lang>
{{out}}
<pre>ENTER ELEVEN NUMBERS:
2.8 111.43332
3.333 186.95529
1.01 6.1564926
2.55 84.503747
11 TOO LARGE
6 TOO LARGE
5 TOO LARGE
4 322
3 136.73205
2 41.414214
1 6</pre>
 
=={{header|Swift}}==
{{works with|Swift 2.0}}
<langsyntaxhighlight lang="swift">import Foundation
 
print("Enter 11 numbers for the Trabb─Pardo─Knuth algorithm:")
Line 2,859 ⟶ 4,253:
print("f(\($0))", result > 400.0 ? "OVERFLOW" : result, separator: "\t")
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,888 ⟶ 4,282:
 
=={{header|Symsyn}}==
<langsyntaxhighlight lang="symsyn">
|Trabb Pardo–Knuth algorithm
 
Line 2,926 ⟶ 4,320:
+ y x
return
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl"># Helper procedures
proc f {x} {expr {abs($x)**0.5 + 5*$x**3}}
proc overflow {y} {expr {$y > 400}}
Line 2,948 ⟶ 4,342:
puts "${x}: $result"
}
}</langsyntaxhighlight>
{{out|Sample run}}
<pre>
Line 2,975 ⟶ 4,369:
</pre>
 
=={{header|VBScriptWren}}==
{{libheader|Wren-fmt}}
<lang vb>
<syntaxhighlight lang="wren">import "io" for Stdin, Stdout
Function tpk(s)
import "./fmt" for Fmt
arr = Split(s," ")
For i = UBound(arr) To 0 Step -1
n = fx(CDbl(arr(i)))
If n > 400 Then
WScript.StdOut.WriteLine arr(i) & " = OVERFLOW"
Else
WScript.StdOut.WriteLine arr(i) & " = " & n
End If
Next
End Function
 
var f = Fn.new { |x| x.abs.sqrt + 5*x*x*x }
Function fx(x)
fx = Sqr(Abs(x))+5*x^3
End Function
 
var s = List.filled(11, 0)
'testing the function
WScriptSystem.StdOut.Write print("Please enter a series of11 numbers:")
var count = 0
list = WScript.StdIn.ReadLine
while (count < 11) {
tpk(list)
Fmt.write(" Number $-2d : ", count + 1)
</lang>
Stdout.flush()
var number = Num.fromString(Stdin.readLine())
if (!number) {
System.print("Not a valid number, try again.")
} else {
s[count] = number
count = count + 1
}
}
s = s[-1..0]
System.print("\nResults:")
for (item in s) {
var fi = f.call(item)
if (fi <= 400) {
Fmt.print(" f($6.3f) = $7.3f", item, fi)
} else {
Fmt.print(" f($6.3f) = overflow", item)
}
}</syntaxhighlight>
 
{{Outout}}
TheEntering numberthe seriessame wasnumbers derived fromas the Ada example of C.:
<pre>
Please enter 11 numbers:
C:\>cscript /nologo tpk.vbs
Number 1 : 10
Please enter 10 numbers:10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301
Number 2 : -1
4.301 = 399.886299747727
Number 3 : 1
4.302 = OVERFLOW
Number 4 : 2
4.303 = OVERFLOW
Number 5 : 3
4.305 = OVERFLOW
Number 6 : 4
4.3 = 399.608644135333
Number 7 : 4.3
4 = 322
Number 8 : 4.305
3 = 136.732050807569
Number 9 : 4.303
2 = 41.4142135623731
Number 10 : 4.302
1 = 6
Number 11 : 4.301
-1 = -4
 
10 = OVERFLOW
Results:
f( 4.301) = 399.886
f( 4.302) = overflow
f( 4.303) = overflow
f( 4.305) = overflow
f( 4.300) = 399.609
f( 4.000) = 322.000
f( 3.000) = 136.732
f( 2.000) = 41.414
f( 1.000) = 6.000
f(-1.000) = -4.000
f(10.000) = overflow
</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
 
func real F(X);
Line 3,035 ⟶ 4,449:
else RlOut(0, Result);
CrLf(0)];
]</langsyntaxhighlight>
 
{{out}}
Line 3,054 ⟶ 4,468:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn f(x) { x.abs().pow(0.5) + x.pow(3)*5 }
reg ns; do{
ns=ask("11 numbers seperated by spaces: ");
Line 3,061 ⟶ 4,475:
ns.reverse().apply(fcn(x){
fx:=f(x); "f(%7.3f)-->%s".fmt(x, if(fx>400)"Overflow" else fx) })
.pump(Console.println);</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits