Trabb Pardo–Knuth algorithm

You are encouraged to solve this task according to the task description, using any language you may know.
The TPK algorithm is an early example of a programming chrestomathy. It was used in Donald Knuth and Luis Trabb Pardo's Stanford tech report The Early Development of Programming Languages. The report traces the early history of work in developing computer languages in the 1940s and 1950s, giving several translations of the algorithm.
From the wikipedia entry:
ask for 11 numbers to be read into a sequence S reverse sequence S for each item in sequence S result := call a function to do an operation if result overflows alert user else print result
The task is to implement the algorithm:
- Use the function:
- The overflow condition is an answer of greater than 400.
- The 'user alert' should not stop processing of other items of the sequence.
- Print a prompt before accepting eleven, textual, numeric inputs.
- You may optionally print the item as well as its associated result, but the results must be in reverse order of input.
- The sequence S may be 'implied' and so not shown explicitly.
- 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).
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()
- Output:
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
Ada
with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions;
procedure Trabb_Pardo_Knuth is
type Real is digits 6 range -400.0 .. 400.0;
package TIO renames Ada.Text_IO;
package FIO is new TIO.Float_IO(Real);
package Math is new Ada.Numerics.Generic_Elementary_Functions(Real);
function F(X: Real) return Real is
begin
return (Math.Sqrt(abs(X)) + 5.0 * X**3);
end F;
Values: array(1 .. 11) of Real;
begin
TIO.Put("Please enter 11 Numbers:");
for I in Values'Range loop
FIO.Get(Values(I));
end loop;
for I in reverse Values'Range loop
TIO.Put("f(");
FIO.Put(Values(I), Fore => 2, Aft => 3, Exp => 0);
TIO.Put(")=");
begin
FIO.Put(F(Values(I)), Fore=> 4, Aft => 3, Exp => 0);
exception
when Constraint_Error => TIO.Put("-->too large<--");
end;
TIO.New_Line;
end loop;
end Trabb_Pardo_Knuth;
- Output:
> ./trabb_pardo_knuth Please enter 11 Numbers: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.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)=-->too large<--
Agena
Tested with Agena 2.9.5 Win32
scope # TPK algorithm in Agena
local y;
local a := [];
local f := proc( t :: number ) is return sqrt(abs(t))+5*t*t*t end;
for i from 0 to 10 do a[i] := tonumber( io.read() ) od;
for i from 10 to 0 by - 1 do
y:=f(a[i]);
if y > 400
then print( "TOO LARGE" )
else printf( "%10.4f\n", y )
fi
od
epocs
- Output:
1 2 3 4 5 6 7 8 9 10 11 TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE 322.0000 136.7321 41.4142 6.0000
ALGOL 60
This is as close as possible to Pardo and Knuth's original but works with the 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.
begin
integer i; real y; real array a[0:10];
real procedure f(t); value t; real t;
f:=sqrt(abs(t))+5*t^3;
for i:=0 step 1 until 10 do inreal(0, a[i]);
for i:=10 step -1 until 0 do
begin
y:=f(a[i]);
if y > 400 then outstring(1, "TOO LARGE")
else outreal(1,y);
outchar(1, "\n", 1)
end
end
Compilation and sample run:
bash-3.2$ marst tpk.a60 -o tpk.c bash-3.2$ gcc tpk.c -lalgol -lm -o tpk bash-3.2$ ./tpk 1 2 3 4 5 6 7 8 9 10 11 TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE 322 136.732050808 41.4142135624 6 bash-3.2$
ALGOL 68
which was itself a Translation of ALGOL 60.
[ 0 : 10 ]REAL a;
PROC f = ( REAL t )REAL:
sqrt(ABS t)+5*t*t*t;
FOR i FROM LWB a TO UPB a DO read( ( a[ i ] ) ) OD;
FOR i FROM UPB a BY -1 TO LWB a DO
REAL y=f(a[i]);
IF y > 400 THEN print( ( "TOO LARGE", newline ) )
ELSE print( ( fixed( y, -9, 4 ), newline ) )
FI
OD
- Output:
1 2 3 4 5 6 7 8 9 10 11 TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE 322.0000 136.7321 41.4142 6.0000
ALGOL W
begin
real y; real array a( 0 :: 10 );
real procedure f( real value t );
sqrt(abs(t))+5*t*t*t;
for i:=0 until 10 do read( a(i) );
r_format := "A"; r_w := 9; r_d := 4;
for i:=10 step -1 until 0 do
begin
y:=f(a(i));
if y > 400 then write( "TOO LARGE" )
else write( y );
end
end.
- Output:
1 2 3 4 5 6 7 8 9 10 11 TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE 322.0000 136.7320 41.4142 6.0000
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
∇
Arturo
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]
]
- Output:
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
AutoIt
; Trabb Pardo–Knuth algorithm
; by James1337 (autoit.de)
; AutoIt Version: 3.3.8.1
Local $S, $i, $y
Do
$S = InputBox("Trabb Pardo–Knuth algorithm", "Please enter 11 numbers:", "1 2 3 4 5 6 7 8 9 10 11")
If @error Then Exit
$S = StringSplit($S, " ")
Until ($S[0] = 11)
For $i = 11 To 1 Step -1
$y = f($S[$i])
If ($y > 400) Then
ConsoleWrite("f(" & $S[$i] & ") = Overflow!" & @CRLF)
Else
ConsoleWrite("f(" & $S[$i] & ") = " & $y & @CRLF)
EndIf
Next
Func f($x)
Return Sqrt(Abs($x)) + 5*$x^3
EndFunc
- Output:
Input: "1 2 3 4 5 6 7 8 9 10 11" f(11) = Overflow! f(10) = Overflow! f(9) = Overflow! f(8) = Overflow! f(7) = Overflow! f(6) = Overflow! f(5) = Overflow! f(4) = 322 f(3) = 136.732050807569 f(2) = 41.4142135623731 f(1) = 6
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)
}
- Output:
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
AWK
# syntax: GAWK -f TRABB_PARDO-KNUTH_ALGORITHM.AWK
BEGIN {
printf("enter 11 numbers: ")
getline S
n = split(S,arr," ")
if (n != 11) {
printf("%d numbers entered; S/B 11\n",n)
exit(1)
}
for (i=n; i>0; i--) {
x = f(arr[i])
printf("f(%s) = %s\n",arr[i],(x>400) ? "too large" : x)
}
exit(0)
}
function abs(x) { if (x >= 0) { return x } else { return -x } }
function f(x) { return sqrt(abs(x)) + 5 * x ^ 3 }
- Output:
enter 11 numbers: 1 2 3 -4 5 6 -7 8 9 10 11 f(11) = too large f(10) = too large f(9) = too large f(8) = too large f(-7) = -1712.35 f(6) = too large f(5) = too large f(-4) = -318 f(3) = 136.732 f(2) = 41.4142 f(1) = 6
BASIC
ANSI 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
- Output:
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
Applesoft BASIC
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
ASIC
Compile with the option Decimal Math (DEC
in command line).
Notes:
- ASIC does not have the fuction that calculates a square root. Thus, the program uses the added subprogram
CALCSQRT:
. - ASIC does not scroll a text screen, but it wraps the text. Thus, the program clears the screen after the user enters numbers.
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
- Output:
Enter the data.
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
Results.
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
BASIC256
dim s(11)
print 'enter 11 numbers'
for i = 0 to 10
input i + ">" , s[i]
next i
for i = 10 to 0 step -1
print "f(" + s[i] + ")=";
x = f(s[i])
if x > 400 then
print "-=< overflow >=-"
else
print x
endif
next i
end
function f(n)
return sqrt(abs(n))+5*n^3
end function
- Output:
enter 11 numbers 0>-4 1>-3 2>-4 3>-2 4>-1 5>- 6>1 7>2 8>3 9>4 10>5 f(5)=--- too large --- f(4)=322 f(3)=136.7320508 f(2)=41.4142136 f(1)=6 f(0)=0 f(-1)=-4 f(-2)=-38.5857864 f(-4)=-318 f(-3)=-133.2679492 f(-4)=-318
Chipmunk Basic
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
- Output:
Same as BASIC256 entry.
Commodore 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
- Output:
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
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
- Output:
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
GW-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%): 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
- Output:
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
Liberty BASIC
' 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
- Output:
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
Minimal BASIC
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
Nascom 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
- Output:
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
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
- Output:
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
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
Run BASIC
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
- Output:
Same as Liberty BASIC entry.
Sinclair ZX81 BASIC
Works with the unexpanded (1k RAM) ZX81
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
- Output:
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
True BASIC
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
VBScript
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)
- Output:
The number series was derived from the example of C.
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
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
- Output:
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
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
C
#include <math.h>
#include <stdio.h>
double f(double x)
{
return sqrt(fabs(x)) + 5 * pow(x, 3);
}
int main()
{
double inputs[11], check = 400, result;
int i;
printf ("\nPlease enter 11 numbers: ");
for (i = 0; i < 11; i++)
{
scanf ("%lf", &inputs[i]);
}
printf ("\n\n\nEvaluating f(x) = |x|^0.5 + 5x^3 for the given inputs:");
for (i = 10; i >= 0; i--)
{
result = f(inputs[i]);
printf ("\nf(%lf) = ", inputs[i]);
if (result > check)
{
printf ("Overflow!");
}
else
{
printf ("%lf", result);
}
}
return 0;
}
- Output:
Please enter 11 numbers: 10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301 Evaluating f(x) = |x|^0.5 + 5x^3 for the given inputs: f(4.301000) = 399.886300 f(4.302000) = Overflow! f(4.303000) = Overflow! f(4.305000) = Overflow! f(4.300000) = 399.608644 f(4.000000) = 322.000000 f(3.000000) = 136.732051 f(2.000000) = 41.414214 f(1.000000) = 6.000000 f(-1.000000) = -4.000000 f(10.000000) = Overflow!
C++
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iomanip>
int main( ) {
std::vector<double> input( 11 ) , results( 11 ) ;
std::cout << "Please enter 11 numbers!\n" ;
for ( int i = 0 ; i < input.size( ) ; i++ )
std::cin >> input[i];
std::transform( input.begin( ) , input.end( ) , results.begin( ) ,
[ ]( double n )-> double { return sqrt( abs( n ) ) + 5 * pow( n , 3 ) ; } ) ;
for ( int i = 10 ; i > -1 ; i-- ) {
std::cout << "f( " << std::setw( 3 ) << input[ i ] << " ) : " ;
if ( results[ i ] > 400 )
std::cout << "too large!" ;
else
std::cout << results[ i ] << " !" ;
std::cout << std::endl ;
}
return 0 ;
}
- Output:
Please enter 11 numbers! 1 2 3 4 5 6 7 8 9 10 11 f( 11 ) : too large! f( 10 ) : too large! f( 9 ) : too large! f( 8 ) : too large! f( 7 ) : too large! f( 6 ) : too large! f( 5 ) : too large! f( 4 ) : 322 ! f( 3 ) : 136.732 ! f( 2 ) : 41.4142 ! f( 1 ) : 6 !
Common Lisp
(defun read-numbers ()
(princ "Enter 11 numbers (space-separated): ")
(let ((numbers '()))
(dotimes (i 11 numbers)
(push (read) numbers))))
(defun trabb-pardo-knuth (func overflowp)
(let ((S (read-numbers)))
(format T "~{~a~%~}"
(substitute-if "Overflow!" overflowp (mapcar func S)))))
(trabb-pardo-knuth (lambda (x) (+ (expt (abs x) 0.5) (* 5 (expt x 3))))
(lambda (x) (> x 400)))
- Output:
Enter 11 numbers (space-separated): 10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301 399.88635 Overflow! Overflow! Overflow! 399.6087 322.0 136.73206 41.414215 6.0 -4.0 Overflow!
D
import std.stdio, std.math, std.conv, std.algorithm, std.array;
double f(in double x) pure nothrow {
return x.abs.sqrt + 5 * x ^^ 3;
}
void main() {
double[] data;
while (true) {
"Please enter eleven numbers on a line: ".write;
data = readln.split.map!(to!double).array;
if (data.length == 11)
break;
writeln("Those aren't eleven numbers.");
}
foreach_reverse (immutable x; data) {
immutable y = x.f;
writefln("f(%0.3f) = %s", x, y > 400 ? "Too large" : y.text);
}
}
- Output:
Please enter eleven numbers on a line: 1 2 3 -4.55 5.1111 6 -7 8 9 10 Those aren't eleven numbers. Please enter eleven numbers on a line: 1 2 3 -4.55 5.1111 6 -7 8 9 10 11 f(11.000) = Too large f(10.000) = Too large f(9.000) = Too large f(8.000) = Too large f(-7.000) = -1712.35 f(6.000) = Too large f(5.111) = Too large f(-4.550) = -468.849 f(3.000) = 136.732 f(2.000) = 41.4142 f(1.000) = 6
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;
- Output:
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.
EasyLang
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
EchoLisp
(define (trabb-fun n)
(+ (* 5 n n n) (sqrt(abs n))))
(define (check-trabb n)
(if (number? n)
(if (<= (trabb-fun n) 400)
(printf "🌱 f(%d) = %d" n (trabb-fun n))
(printf "❌ f(%d) = %d" n (trabb-fun n)))
(error "not a number" n)))
(define (trabb (numlist null))
(while (< (length numlist) 11)
(set! numlist (append numlist
(or
(read default: (shuffle (iota 11))
prompt: (format "Please enter %d more numbers" (- 11 (length numlist))))
(error 'incomplete-list numlist))))) ;; users cancel
(for-each check-trabb (reverse (take numlist 11))))
- Output:
(trabb)
;; input : (0 4 1 8 5 9 10 3 6 7 2)
🌱 f(2) = 41.41421356237309
❌ f(7) = 1717.6457513110645
❌ f(6) = 1082.4494897427833
🌱 f(3) = 136.73205080756887
❌ f(10) = 5003.162277660168
❌ f(9) = 3648
❌ f(5) = 627.2360679774998
❌ f(8) = 2562.828427124746
🌱 f(1) = 6
🌱 f(4) = 322
🌱 f(0) = 0
;; extra credit : let's find the threshold
(lib 'math)
(define (g x) (- (trabb-fun x) 400))
(root g 0 10)
→ 4.301409367213084
Ela
Translation of OCaml version:
open monad io number string
:::IO
take_numbers 0 xs = do
return $ iter xs
where f x = sqrt (toSingle x) + 5.0 * (x ** 3.0)
p x = x < 400.0
iter [] = return ()
iter (x::xs)
| p res = do
putStrLn (format "f({0}) = {1}" x res)
iter xs
| else = do
putStrLn (format "f({0}) :: Overflow" x)
iter xs
where res = f x
take_numbers n xs = do
x <- readAny
take_numbers (n - 1) (x::xs)
do
putStrLn "Please enter 11 numbers:"
take_numbers 11 []
- Output:
Please enter 11 numbers: 1 2 3 4 5 6 7 8 9 10 11 f(11) :: Overflow f(10) :: Overflow f(9) :: Overflow f(8) :: Overflow f(7) :: Overflow f(6) :: Overflow f(5) :: Overflow f(4) = 322 f(3) = 136.732050807569 f(2) = 41.4142135623731 f(1) = 6
Elena
ELENA 6.x :
import extensions;
import extensions'math;
public program()
{
real[] inputs := new real[](11);
console.printLine("Please enter 11 numbers :");
for(int i := 0; i < 11; i += 1)
{
inputs[i] := console.readLine().toReal()
};
console.printLine("Evaluating f(x) = |x|^0.5 + 5x^3 for the given inputs :");
for(int i := 10; i >= 0; i -= 1)
{
real result := sqrt(abs(inputs[i])) + 5 * power(inputs[i], 3);
console.print("f(", inputs[i], ")=");
if (result > 400)
{
console.printLine("Overflow!")
}
else
{
console.printLine(result)
}
}
}
- Output:
Please enter 11 numbers : 1 2 3 4 5 6 7 8 9 10 11 Evaluating f(x) = |x|^0.5 + 5x^3 for the given inputs : f(11.0)=Overflow! f(10.0)=Overflow! f(9.0)=Overflow! f(8.0)=Overflow! f(7.0)=Overflow! f(6.0)=Overflow! f(5.0)=Overflow! f(4.0)=322.0 f(3.0)=136.7320508076 f(2.0)=41.41421356237 f(1.0)=6.0
Elixir
defmodule Trabb_Pardo_Knuth do
def task do
Enum.reverse( get_11_numbers )
|> Enum.each( fn x -> perform_operation( &function(&1), 400, x ) end )
end
defp alert( n ), do: IO.puts "Operation on #{n} overflowed"
defp get_11_numbers do
ns = IO.gets( "Input 11 integers. Space delimited, please: " )
|> String.split
|> Enum.map( &String.to_integer &1 )
if 11 == length( ns ), do: ns, else: get_11_numbers
end
defp function( x ), do: :math.sqrt( abs(x) ) + 5 * :math.pow( x, 3 )
defp perform_operation( fun, overflow, n ), do: perform_operation_check_overflow( n, fun.(n), overflow )
defp perform_operation_check_overflow( n, result, overflow ) when result > overflow, do: alert( n )
defp perform_operation_check_overflow( n, result, _overflow ), do: IO.puts "f(#{n}) => #{result}"
end
Trabb_Pardo_Knuth.task
- Output:
Input 11 integers. Space delimited, please: 0 1 2 3 4 5 6 7 8 9 10 Operation on 10 overflowed Operation on 9 overflowed Operation on 8 overflowed Operation on 7 overflowed Operation on 6 overflowed Operation on 5 overflowed f(4) => 322.0 f(3) => 136.73205080756887 f(2) => 41.41421356237309 f(1) => 6.0 f(0) => 0.0
Erlang
-module( trabb_pardo_knuth ).
-export( [task/0] ).
task() ->
Sequence = get_11_numbers(),
S = lists:reverse( Sequence ),
[perform_operation( fun function/1, 400, X) || X <- S].
alert( N ) -> io:fwrite( "Operation on ~p overflowed~n", [N] ).
get_11_numbers() ->
{ok, Ns} = io:fread( "Input 11 integers. Space delimited, please: ", "~d ~d ~d ~d ~d ~d ~d ~d ~d ~d ~d" ),
11 = erlang:length( Ns ),
Ns.
function( X ) -> math:sqrt( erlang:abs(X) ) + 5 * math:pow( X, 3 ).
perform_operation( Fun, Overflow, N ) -> perform_operation_check_overflow( N, Fun(N), Overflow ).
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] ).
- Output:
5> trabb_pardo_knuth:task(). Input 11 integers. Space delimited, please: 1 2 3 4 5 6 7 8 9 10 11 Operation on 11 overflowed Operation on 10 overflowed Operation on 9 overflowed Operation on 8 overflowed Operation on 7 overflowed Operation on 6 overflowed Operation on 5 overflowed f(4) => 322.0 f(3) => 136.73205080756887 f(2) => 41.41421356237309 f(1) => 6.0
ERRE
!Trabb Pardo-Knuth algorithm
PROGRAM TPK
!VAR I%,Y
DIM A[10]
FUNCTION F(T)
F=SQR(ABS(T))+5*T^3
END FUNCTION
BEGIN
DATA(10,-1,1,2,3,4,4.3,4.305,4.303,4.302,4.301)
FOR I%=0 TO 10 DO
READ(A[I%])
END FOR
FOR I%=10 TO 0 STEP -1 DO
Y=F(A[I%])
PRINT("F(";A[I%];")=";)
IF Y>400 THEN PRINT("--->too large<---")
ELSE PRINT(Y)
END IF
END FOR
END PROGRAM
Numbers to be elaborated is included in the program with a DATA statement. You can substitute this with an input keyboard like this
FOR I%=0 TO 10 DO PRINT("Number #";I%;) INPUT(A[I%]) END FOR
F#
module ``Trabb Pardo - Knuth``
open System
let f (x: float) = sqrt(abs x) + (5.0 * (x ** 3.0))
Console.WriteLine "Enter 11 numbers:"
[for _ in 1..11 -> Convert.ToDouble(Console.ReadLine())]
|> List.rev |> List.map f |> List.iter (function
| n when n <= 400.0 -> Console.WriteLine(n)
| _ -> Console.WriteLine("Overflow"))
- Output:
fsharpi Program.fsx [Loading Program.fsx] Enter 11 numbers: 1 2 3 4 5 6 7 8 9 10 11 Overflow Overflow Overflow Overflow Overflow Overflow Overflow 322 136.732050807569 41.4142135623731 6
Factor
USING: formatting io kernel math math.functions math.parser
prettyprint sequences splitting ;
IN: rosetta-code.trabb-pardo-knuth
CONSTANT: threshold 400
CONSTANT: prompt "Please enter 11 numbers: "
: fn ( x -- y ) [ abs 0.5 ^ ] [ 3 ^ 5 * ] bi + ;
: overflow? ( x -- ? ) threshold > ;
: get-input ( -- seq )
prompt write flush readln " " split dup length 11 =
[ drop get-input ] unless ;
: ?result ( ..a quot: ( ..a -- ..b ) -- ..b )
[ "f(%u) = " sprintf ] swap bi dup overflow?
[ drop "overflow" ] [ "%.3f" sprintf ] if append ; inline
: main ( -- )
get-input reverse
[ string>number [ fn ] ?result print ] each ;
MAIN: main
- Output:
Please enter 11 numbers: 1 2 3 Please enter 11 numbers: 10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301 f(4.301) = 399.886 f(4.302) = overflow f(4.303) = overflow f(4.305) = overflow f(4.3) = 399.609 f(4) = 322.000 f(3) = 136.732 f(2) = 41.414 f(1) = 6.000 f(-1) = -4.000 f(10) = overflow
Forth
: f(x) fdup fsqrt fswap 3e f** 5e f* f+ ;
4e2 fconstant f-too-big
11 Constant #Elements
: float-array ( compile: n -- / run: n -- addr )
create
floats allot
does>
swap floats + ;
#Elements float-array vec
: get-it ( -- )
." Enter " #Elements . ." numbers:" cr
#Elements 0 DO
." > " pad 25 accept cr
pad swap >float 0= abort" Invalid Number"
i vec F!
LOOP ;
: reverse-it ( -- )
#Elements 2/ 0 DO
i vec F@ #Elements i - 1- vec F@
i vec F! #Elements i - 1- vec F!
LOOP ;
: do-it ( -- )
#Elements 0 DO
i vec F@ fdup f. [char] : emit space
f(x) fdup f-too-big f> IF
fdrop ." too large"
ELSE
f.
THEN cr
LOOP ;
: tpk ( -- )
get-it reverse-it do-it ;
- Output:
Gforth 0.7.2, Copyright (C) 1995-2008 Free Software Foundation, Inc. Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license' Type `bye' to exit tpk Enter 11 numbers: > 1 > 2 > 3 > 4 > 5 > 6 > 2.71828 > 3.14159 > 76 > 7 > 8 8. : too large 7. : too large 76. : too large 3.14159 : 156.80344365595 2.71828 : 102.07620267347 6. : too large 5. : too large 4. : 322. 3. : 136.732050807569 2. : 41.4142135623731 1. : 6. ok
Fortran
Fortran 95
program tpk
implicit none
real, parameter :: overflow = 400.0
real :: a(11), res
integer :: i
write(*,*) "Input eleven numbers:"
read(*,*) a
a = a(11:1:-1)
do i = 1, 11
res = f(a(i))
write(*, "(a, f0.3, a)", advance = "no") "f(", a(i), ") = "
if(res > overflow) then
write(*, "(a)") "overflow!"
else
write(*, "(f0.3)") res
end if
end do
contains
real function f(x)
real, intent(in) :: x
f = sqrt(abs(x)) + 5.0*x**3
end function
end program
- Output:
Input eleven numbers: 10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301 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!
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.
C THE TPK ALGORITH - FORTRAN I - 1957 TPK00010
FTPKF(X)=SQRTF(ABSF(X))+5.0*X**3 TPK00020
DIMENSION A(11) TPK00030
READ 100,A TPK00040
100 FORMAT(6F12.4/) TPK00050
DO 3 I=1,11 TPK00060
J=12-I TPK00070
Y=FTPKF(A(J)) TPK00080
IF (Y-400.0)2,2,1 TPK00090
1 PRINT 301,I,A(J) TPK00100
301 FORMAT(I10,F12.7,18H *** TOO LARGE ***) TPK00110
GO TO 10 TPK00120
2 PRINT 302,I,A(J),Y TPK00130
302 FORMAT(I10,2F12.7) TPK00140
3 CONTINUE TPK00150
STOP 0 TPK00160
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
- Output:
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]
Go
Task/Wikipedia
This solution follows the task description by reversing the sequence. It also rejects non-numeric input until 11 numbers are entered.
package main
import (
"fmt"
"log"
"math"
)
func main() {
// prompt
fmt.Print("Enter 11 numbers: ")
// accept sequence
var s [11]float64
for i := 0; i < 11; {
if n, _ := fmt.Scan(&s[i]); n > 0 {
i++
}
}
// reverse sequence
for i, item := range s[:5] {
s[i], s[10-i] = s[10-i], item
}
// iterate
for _, item := range s {
if result, overflow := f(item); overflow {
// send alerts to stderr
log.Printf("f(%g) overflow", item)
} else {
// send normal results to stdout
fmt.Printf("f(%g) = %g\n", item, result)
}
}
}
func f(x float64) (float64, bool) {
result := math.Sqrt(math.Abs(x)) + 5*x*x*x
return result, result > 400
}
- Output:
The input is chosen to show some interesting boundary cases.
Enter 11 numbers: 0 1 4.3 4.4 -1 -5 non-number -1e102 -1e103 -Inf Inf NaN f(NaN) = NaN 2016/04/15 18:38:29 f(+Inf) overflow f(-Inf) = NaN f(-1e+103) = -Inf f(-1e+102) = -5e+306 f(-5) = -622.7639320225002 f(-1) = -4 2016/04/15 18:38:29 f(4.4) overflow f(4.3) = 399.6086441353327 f(1) = 6 f(0) = 0
TPK paper
The original paper had no requirement to reverse the sequence in place, but instead processed the sequence in reverse order.
package main
import (
"fmt"
"math"
)
func f(t float64) float64 {
return math.Sqrt(math.Abs(t)) + 5*math.Pow(t, 3)
}
func main() {
var a [11]float64
for i := range a {
fmt.Scan(&a[i])
}
for i := len(a) - 1; i >= 0; i-- {
if y := f(a[i]); y > 400 {
fmt.Println(i, "TOO LARGE")
} else {
fmt.Println(i, y)
}
}
}
Haskell
import Control.Monad (replicateM, mapM_)
f :: Floating a => a -> a
f x = sqrt (abs x) + 5 * x ** 3
main :: IO ()
main = do
putStrLn "Enter 11 numbers for evaluation"
x <- replicateM 11 readLn
mapM_
((\x ->
if x > 400
then putStrLn "OVERFLOW"
else print x) .
f) $
reverse x
- Output:
Enter 11 numbers for evaluation 1 2 3 4 5 6 7 8 9 10 11 OVERFLOW OVERFLOW OVERFLOW OVERFLOW OVERFLOW OVERFLOW OVERFLOW 322.0 136.73205080756887 41.41421356237309 6.0
Icon and Unicon
The following Unicon-specific solution can be implemented in Icon by replaces reverse(S) with S[*S to 1 by -1].
procedure main()
S := []
writes("Enter 11 numbers: ")
read() ? every !11 do (tab(many(' \t'))|0,put(S, tab(upto(' \t')|0)))
every item := !reverse(S) do
write(item, " -> ", (400 >= f(item)) | "overflows")
end
procedure f(x)
return abs(x)^0.5 + 5*x^3
end
Sample run:
->tpk Enter 11 numbers: 1 2 3 4 5 6 7 8 9 10 11 11 -> overflows 10 -> overflows 9 -> overflows 8 -> overflows 7 -> overflows 6 -> overflows 5 -> overflows 4 -> 322.0 3 -> 136.7320508075689 2 -> 41.41421356237309 1 -> 6.0 ->
Imp77
This was written directly from the Algol 60 description in original paper. One line was added because the current Imp77 compiler doesn't include 'sqrt' in the default library. Imp80 uses MOD(x) instead of |x|. I used the older form of 'cycle' rather than a 'for' loop, to stay in keeping with the 60's style, when Imp's parent language was Atlas Autocode - the implementation below is very similar to what the AA implementation would have looked like, but unfortunately (and unlike Imp77) we have no contemporary AA compiler that we could use to verify the code.
%begin
%external %real %fn %spec sqrt %alias "sqrtf" (%real n) {should be in prims. Using Linux/C's external here.}
%routine TPK(%real %array %name a, b)
! given 11 input values a(0),...,a(10), this routine
! produces 22 output values b(0),...b(21), according
! to the classical TPK algorithm
%real %fn f(%real t)
%result = sqrt(|t|) + 5 * t^3
%end
%real y
%integer i
%cycle i = 10, -1, 0
y = f(a(i))
y = 999 %if y > 400
b(20-2*i) = i
b(21-2*i) = y
%repeat
%end; ! TPK
%real %array a(0:10), b(0:21)
TPK(a,b)
%endofprogram
Io
// Initialize objects to be used
in_num := File standardInput()
nums := List clone
result := Number
// Prompt the user and get numbers from standard input
"Please enter 11 numbers:" println
11 repeat(nums append(in_num readLine() asNumber()))
// Reverse the numbers received
nums reverseInPlace
// Apply the function and tell the user if the result is above
// our limit. Otherwise, tell them the result.
nums foreach(v,
// v needs parentheses around it for abs to properly convert v to its absolute value
result = (v) abs ** 0.5 + 5 * v ** 3
if (result > 400,
"Overflow!" println
,
result println
)
)
- Output:
io tpk.io Please enter 11 numbers: 1 2 3 4 5 6 7 8 9 10 11 Overflow! Overflow! Overflow! Overflow! Overflow! Overflow! Overflow! 322 136.7320508075688679 41.4142135623730923 6
J
Input and output in J is done using "foreigns", in this case it is reading from the keyboard. The calculations are straightforward and applied to the whole set simultaneously. Similarly, overflow detection and changing the value to 'user alert' is also done once for all values.
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.
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
)
A possible use scenario:
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
Note that the result of tpk is persisted in t1 and is also its explicit result rather than being an explicit output.
Here's an alternative approach:
get11numbers=: 3 :0
smoutput 'Enter 11 numbers: '
_&". 1!:1]1
)
f_x=: %:@| + 5 * ^&3
overflow400=: 'user alert'"_`":@.(<:&400)"0
tpk=: overflow400@f_x@|.@get11numbers
And, here's this alternative in action:
tpk''
Enter 11 numbers:
1 2 3 4 5 6 7 8.8 _9 10.123 0
0
user alert
_3642
user alert
user alert
user alert
user alert
322
136.732
41.4142
6
(clearly, other alternatives are also possible).
Note that no error is reported if something other than 11 numbers are provided, since it's not clear what should be done for that case -- we just process all of them.
Java
/**
* Alexander Alvonellos
*/
import java.util.*;
import java.io.*;
public class TPKA {
public static void main(String... args) {
double[] input = new double[11];
double userInput = 0.0;
Scanner in = new Scanner(System.in);
for(int i = 0; i < 11; i++) {
System.out.print("Please enter a number: ");
String s = in.nextLine();
try {
userInput = Double.parseDouble(s);
} catch (NumberFormatException e) {
System.out.println("You entered invalid input, exiting");
System.exit(1);
}
input[i] = userInput;
}
for(int j = 10; j >= 0; j--) {
double x = input[j]; double y = f(x);
if( y < 400.0) {
System.out.printf("f( %.2f ) = %.2f\n", x, y);
} else {
System.out.printf("f( %.2f ) = %s\n", x, "TOO LARGE");
}
}
}
private static double f(double x) {
return Math.pow(Math.abs(x), 0.5) + (5*(Math.pow(x, 3)));
}
}
- Output:
Please enter a number: 1 Please enter a number: 2 Please enter a number: 3 Please enter a number: 4 Please enter a number: 5 Please enter a number: 6 Please enter a number: 7 Please enter a number: 8 Please enter a number: 9 Please enter a number: 10 Please enter a number: 11 f( 11.00 ) = TOO LARGE f( 10.00 ) = TOO LARGE f( 9.00 ) = TOO LARGE f( 8.00 ) = TOO LARGE f( 7.00 ) = TOO LARGE f( 6.00 ) = TOO LARGE f( 5.00 ) = TOO LARGE f( 4.00 ) = 322.00 f( 3.00 ) = 136.73 f( 2.00 ) = 41.41 f( 1.00 ) = 6.00
JavaScript
Spidermonkey
#!/usr/bin/env js
function main() {
var nums = getNumbers(11);
nums.reverse();
for (var i in nums) {
pardoKnuth(nums[i], fn, 400);
}
}
function pardoKnuth(n, f, max) {
var res = f(n);
putstr('f(' + String(n) + ')');
if (res > max) {
print(' is too large');
} else {
print(' = ' + String(res));
}
}
function fn(x) {
return Math.pow(Math.abs(x), 0.5) + 5 * Math.pow(x, 3);
}
function getNumbers(n) {
var nums = [];
print('Enter', n, 'numbers.');
for (var i = 1; i <= n; i++) {
putstr(' ' + i + ': ');
var num = readline();
nums.push(Number(num));
}
return nums;
}
main();
Results:
Enter 11 numbers. 1: 1 2: 2 3: 3 4: 4 5: 5 6: 6 7: 7 8: 8 9: 9 10: 10 11: 11 f(11) is too large f(10) is too large f(9) is too large f(8) is too large f(7) is too large f(6) is too large f(5) is too large f(4) = 322 f(3) = 136.73205080756887 f(2) = 41.41421356237309 f(1) = 6
jq
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.
def f:
def abs: if . < 0 then -. else . end;
def power(x): (x * log) | exp;
. as $x | abs | power(0.5) + (5 * (.*.*. ));
. as $in | split(" ") | map(tonumber)
| if length == 11 then
reverse | map(f | if . > 400 then "TOO LARGE" else . end)
else error("The number of numbers was not 11.")
end
| .[] # print one result per line
- Output:
$ 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:
1 2 3 4 5 6 7 8 9 10 11
"TOO LARGE"
"TOO LARGE"
"TOO LARGE"
"TOO LARGE"
"TOO LARGE"
"TOO LARGE"
"TOO LARGE"
322
136.73205080756887
41.41421356237309
6
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
- Output:
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
Kotlin
// version 1.1.2
fun f(x: Double) = Math.sqrt(Math.abs(x)) + 5.0 * x * x * x
fun main(args: Array<String>) {
val da = DoubleArray(11)
println("Please enter 11 numbers:")
var i = 0
while (i < 11) {
print(" ${"%2d".format(i + 1)}: ")
val d = readLine()!!.toDoubleOrNull()
if (d == null)
println("Not a valid number, try again")
else
da[i++] = d
}
println("\nThe sequence you just entered in reverse is:")
da.reverse()
println(da.contentToString())
println("\nProcessing this sequence...")
for (j in 0..10) {
val v = f(da[j])
print(" ${"%2d".format(j + 1)}: ")
if (v > 400.0)
println("Overflow!")
else
println(v)
}
}
- Output:
Sample session:
Please enter 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 The sequence you just entered in reverse is: [4.301, 4.302, 4.303, 4.305, 4.3, 4.0, 3.0, 2.0, 1.0, -1.0, 10.0] Processing this sequence... 1: 399.88629974772687 2: Overflow! 3: Overflow! 4: Overflow! 5: 399.6086441353327 6: 322.0 7: 136.73205080756887 8: 41.41421356237309 9: 6.0 10: -4.0 11: Overflow!
Ksh
#!/bin/ksh
# Trabb Pardo–Knuth algorithm
# # Variables:
#
integer NUM_ELE=11
typeset -F FUNC_LIMIT=400
# # Functions:
#
# # Function _input(_arr) - Ask for user input, build array
#
function _input {
typeset _arr ; nameref _arr="$1"
typeset _i ; integer _i
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
- Output:
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
Lua
Implementation of task description
function f (x) return math.abs(x)^0.5 + 5*x^3 end
function reverse (t)
local rev = {}
for i, v in ipairs(t) do rev[#t - (i-1)] = v end
return rev
end
local sequence, result = {}
print("Enter 11 numbers...")
for n = 1, 11 do
io.write(n .. ": ")
sequence[n] = io.read()
end
for _, x in ipairs(reverse(sequence)) do
result = f(x)
if result > 400 then print("Overflow!") else print(result) end
end
- Output:
Enter 11 numbers... 1: 1 2: 2 3: 3 4: 4 5: 5 6: 6 7: 7 8: 8 9: 9 10: 10 11: 11 Overflow! Overflow! Overflow! Overflow! Overflow! Overflow! Overflow! 322 136.73205080757 41.414213562373 6
Line-for-line from TPK paper
local a, y = {}
function f (t)
return math.sqrt(math.abs(t)) + 5*t^3
end
for i = 0, 10 do a[i] = io.read() end
for i = 10, 0, -1 do
y = f(a[i])
if y > 400 then print(i, "TOO LARGE")
else print(i, y) end
end
- Output:
1 2 3 4 5 6 7 8 9 10 11 10 TOO LARGE 9 TOO LARGE 8 TOO LARGE 7 TOO LARGE 6 TOO LARGE 5 TOO LARGE 4 TOO LARGE 3 322 2 136.73205080757 1 41.414213562373 0 6
M2000 Interpreter
Module Input11 {
Flush ' empty stack
For I=1 to 11 {
Input "Give me a number ", a
Data a ' add to bottom of stack, use: Push a to add to top, to get reverse order here
}
}
Module Run {
Print "Trabb Pardo–Knuth algorithm"
Print "f(x)=Sqrt(Abs(x))+5*x^3"
if not match("NNNNNNNNN") then Error "Need 11 numbers"
Shiftback 1, -11 ' reverse order 11 elements of stack of values
Def f(x)=Sqrt(Abs(x))+5*x^3
For i=1 to 11 {
Read pop
y=f(pop)
if y>400 Then {
Print format$("f({0}) = Overflow!", pop)
} Else {
Print format$("f({0}) = {1}", pop, y)
}
}
}
Run 10, -1, 1, 2, 3, 4, 4.3, 4.305, 4.303, 4.302, 4.301
Run 1, 2, 3, -4.55,5.1111, 6, -7, 8, 9, 10, 11
Input11
Run
To collect the output in clipboard. Global variables need <= to assign values, and document append values using = or <= (for globals)
Output with "," for decimals (Locale 1032). We can change this using statement Locale 1033
Global a$
Document a$ ' make a$ as a document - string with paragraphs
Module Run {
a$<={Trabb Pardo–Knuth algorithm
f(x)=Sqrt(Abs(x))+5*x^3
}
if not match("NNNNNNNNN") then Error "Need 11 numbers"
Shiftback 1, -11 ' reverse order 11 elements of stack of values
Def f(x)=Sqrt(Abs(x))+5*x^3
For i=1 to 11 {
Read pop
y=f(pop)
if y>400 Then {
a$<=format$("f({0}) = Overflow!", pop)+{
}
} Else {
a$<=format$("f({0}) = {1}", pop, y)+{
}
}
}
}
Run 10, -1, 1, 2, 3, 4, 4.3, 4.305, 4.303, 4.302, 4.301
Run 1, 2, 3, -4.55,5.1111, 6, -7, 8, 9, 10, 11
Clipboard a$
- Output:
Trabb Pardo–Knuth algorithm f(x)=Sqrt(Abs(x))+5*x^3 f(4,301) = 399,886299747727 f(4,302) = Overflow! f(4,303) = Overflow! f(4,305) = Overflow! f(4,3) = 399,608644135333 f(4) = 322 f(3) = 136,732050807569 f(2) = 41,4142135623731 f(1) = 6 f(-1) = -4 f(10) = Overflow! Trabb Pardo–Knuth algorithm f(x)=Sqrt(Abs(x))+5*x^3 f(11) = Overflow! f(10) = Overflow! f(9) = Overflow! f(8) = Overflow! f(-7) = -1712,35424868894 f(6) = Overflow! f(5,1111) = Overflow! f(-4,55) = -468,84880209923 f(3) = 136,732050807569 f(2) = 41,4142135623731 f(1) = 6
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
result := f(item):
if (result > 400) then
print("Alert: Overflow."):
else
print(result):
end if:
end do:
- Usage:
Input:1,2,3,4,5,6,7,8,9,10,11
"Alert: Overflow." "Alert: Overflow." "Alert: Overflow." "Alert: Overflow." "Alert: Overflow." "Alert: Overflow." "Alert: Overflow." 322.0000000 136.7320508 41.41421356 6.
Mathematica /Wolfram Language
numbers=RandomReal[{-2,6},11]
tpk[numbers_,overflowVal_]:=Module[{revNumbers},
revNumbers=Reverse[numbers];
f[x_]:=Abs[x]^0.5+5 x^3;
Do[
If[f[i]>overflowVal,
Print["f[",i,"]= Overflow"]
,
Print["f[",i,"]= ",f[i]]
]
,
{i,revNumbers}
]
]
tpk[numbers,400]
- Output:
{0.470145,1.18367,2.36984,4.86759,2.40274,5.48793,3.30256,5.34393,4.21944,2.23501,-0.0200707} f[-0.0200707]= 0.141631 f[2.23501]= 57.3176 f[4.21944]= 377.663 f[5.34393]= Overflow f[3.30256]= 181.921 f[5.48793]= Overflow f[2.40274]= 70.9068 f[4.86759]= Overflow f[2.36984]= 68.0859 f[1.18367]= 9.38004 f[0.470145]= 1.20527
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
- Output:
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
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
- Output:
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
Modula-2
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.
- Output:
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
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("11 numbers: ").strip.split[0..10].map(parseFloat)
var s = ask()
reverse s
for x in s:
let result = f(x)
echo x, ": ", if result > 400: "TOO LARGE!" else: $result
- Output:
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
Objective-C
//
// TPKA.m
// RosettaCode
//
// Created by Alexander Alvonellos on 5/26/12.
// Trabb Pardo-Knuth algorithm
//
#import <Foundation/Foundation.h>
double f(double x);
double f(double x) {
return pow(abs(x), 0.5) + 5*(pow(x, 3));
}
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSMutableArray *input = [[NSMutableArray alloc] initWithCapacity:0];
printf("%s", "Instructions: please enter 11 numbers.\n");
for(int i = 0; i < 11; i++) {
double userInput = 0.0;
printf("%s", "Please enter a number: ");
scanf("%lf", &userInput);
[input addObject: @(userInput)];
}
for(int i = 10; i >= 0; i--) {
double x = [input[i] doubleValue];
double y = f(x);
printf("f(%.2f) \t=\t", x);
if(y < 400.0) {
printf("%.2f\n", y);
} else {
printf("%s\n", "TOO LARGE");
}
}
}
return 0;
}
- Output:
Instructions: please enter 11 numbers. Please enter a number: 1 Please enter a number: 2 Please enter a number: 3 Please enter a number: 4 Please enter a number: 5 Please enter a number: 6 Please enter a number: 7 Please enter a number: 8 Please enter a number: 9 Please enter a number: 10 Please enter a number: 11 f(11.00) = TOO LARGE f(10.00) = TOO LARGE f(9.00) = TOO LARGE f(8.00) = TOO LARGE f(7.00) = TOO LARGE f(6.00) = TOO LARGE f(5.00) = TOO LARGE f(4.00) = 322.00 f(3.00) = 136.73 f(2.00) = 41.41 f(1.00) = 6.00
OCaml
let f x = sqrt x +. 5.0 *. (x ** 3.0)
let p x = x < 400.0
let () =
print_endline "Please enter 11 Numbers:";
let lst = Array.to_list (Array.init 11 (fun _ -> read_float ())) in
List.iter (fun x ->
let res = f x in
if p res
then Printf.printf "f(%g) = %g\n%!" x res
else Printf.eprintf "f(%g) :: Overflow\n%!" x
) (List.rev lst)
- Output:
$ ocaml trabb_pardo_knuth.ml Please enter 11 Numbers: 1 2 3 4 5 6 7 8 9 10 11 f(11) :: Overflow f(10) :: Overflow f(9) :: Overflow f(8) :: Overflow f(7) :: Overflow f(6) :: Overflow f(5) :: Overflow f(4) = 322 f(3) = 136.732 f(2) = 41.4142 f(1) = 6
We output error messages on stderr.
We flush outputs with "%!"
so that results and error messages do not appear separated.
PARI/GP
{
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])
}
- Output:
11 numbers: 1 2 3 4 5 6 7 8 9 10 11 %1 = ["overflow", "overflow", "overflow", "overflow", "overflow", "overflow", "overflow", 322.0000000000000000000000000, 136.7320508075688772935274463, 41.414 21356237309504880168872, 6.000000000000000000000000000]
PascalABC.NET
function f(x:real) := Sqrt(x) + 5 *x ** 3;
begin
var seq := ReadArrInteger(11).Reverse;
foreach var x in seq do
begin
var fx := f(x);
Println($'f({x}): {fx > 400 ? ''overflow'' : fx.ToString}');
end;
end.
- Output:
1 2 3 4 5 6 7 8 9 10 11 f(11): overflow f(10): overflow f(9): overflow f(8): overflow f(7): overflow f(6): overflow f(5): overflow f(4): 322 f(3): 136.732050807569 f(2): 41.4142135623731 f(1): 6
Perl
print "Enter 11 numbers:\n";
for ( 1..11 ) {
$number = <STDIN>;
chomp $number;
push @sequence, $number;
}
for $n (reverse @sequence) {
my $result = sqrt( abs($n) ) + 5 * $n**3;
printf "f( %6.2f ) %s\n", $n, $result > 400 ? " too large!" : sprintf "= %6.2f", $result
}
- Output:
Enter 11 numbers: 2 1.2 3 3.4 4 4.5 5 7.8 2.7 13 11.2 f( 11.20 ) too large! f( 13.00 ) too large! f( 2.70 ) = 100.06 f( 7.80 ) too large! f( 5.00 ) too large! f( 4.50 ) too large! f( 4.00 ) = 322.00 f( 3.40 ) = 198.36 f( 3.00 ) = 136.73 f( 1.20 ) = 9.74 f( 2.00 ) = 41.41
Phix
function f(atom x) return sqrt(abs(x))+5*power(x,3) end function procedure test(string s, bool fake_prompt=true) if fake_prompt then printf(1,"Enter 11 numbers:%s\n",{s}) end if s = substitute(s,","," ") sequence S = scanf(s,"%f %f %f %f %f %f %f %f %f %f %f") if length(S)!=1 then puts(1,"not 11 numbers") abort(0) end if S = reverse(S[1]) for i=1 to length(S) do atom result = f(S[i]) if result>400 then printf(1,"f(%g):overflow\n",{S[i]}) else printf(1,"f(%g):%g\n",{S[i],result}) end if end for puts(1,"\n") end procedure --test(prompt_string("Enter 11 numbers:"),false) constant tests = {"10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301","1,2,3,4,5,6,7,8,9,10,11", "0.470145,1.18367,2.36984,4.86759,2.40274,5.48793,3.30256,5.34393,4.21944,2.23501,-0.0200707"} papply(tests,test)
- Output:
Enter 11 numbers:10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301 f(4.301):399.886 f(4.302):overflow f(4.303):overflow f(4.305):overflow f(4.3):399.609 f(4):322 f(3):136.732 f(2):41.4142 f(1):6 f(-1):-4 f(10):overflow Enter 11 numbers:1,2,3,4,5,6,7,8,9,10,11 f(11):overflow f(10):overflow f(9):overflow f(8):overflow f(7):overflow f(6):overflow f(5):overflow f(4):322 f(3):136.732 f(2):41.4142 f(1):6 Enter 11 numbers:0.470145,1.18367,2.36984,4.86759,2.40274,5.48793,3.30256,5.34393,4.21944,2.23501,-0.0200707 f(-0.0200707):0.141631 f(2.23501):57.3174 f(4.21944):377.662 f(5.34393):overflow f(3.30256):181.921 f(5.48793):overflow f(2.40274):70.9071 f(4.86759):overflow f(2.36984):68.0862 f(1.18367):9.38002 f(0.470145):1.20527
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;
}
?>
- Output:
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
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.
Example run:
- Output:
$ 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]]
PicoLisp
(de f (X)
(+ (sqrt (abs X)) (* 5 X X X)) )
(trace 'f)
(in NIL
(prin "Input 11 numbers: ")
(for X (reverse (make (do 11 (link (read)))))
(when (> (f X) 400)
(prinl "TOO LARGE") ) ) )
Test:
Input 11 numbers: 1 2 3 4 5 6 7 8 9 10 11
f : 11
f = 6658
TOO LARGE
f : 10
f = 5003
TOO LARGE
f : 9
f = 3648
TOO LARGE
f : 8
f = 2562
TOO LARGE
f : 7
f = 1717
TOO LARGE
f : 6
f = 1082
TOO LARGE
f : 5
f = 627
TOO LARGE
f : 4
f = 322
f : 3
f = 136
f : 2
f = 41
f : 1
f = 6
PL/I
Trabb: Procedure options (main); /* 11 November 2013 */
declare (i, n) fixed binary;
declare s fixed (5,1) controlled;
declare g fixed (15,5);
put ('Please type 11 values:');
do i = 1 to 11;
allocate s;
get (s);
put (s);
end;
put skip(2) ('Results:');
do i = 1 to 11;
g = f(s); put skip list (s);
if g > 400 then put ('Too large'); else put (g);
free s;
end;
f: procedure (x) returns (fixed(15,5));
declare x fixed (5,1);
return (sqrt(abs(x)) + 5*x**3);
end f;
end Trabb;
- Output:
Please type 11 values: 1.0 3.0 2.0 -4.0 -5.0 6.0 7.0 9.0 11.0 1.5 2.4 Results: 2.4 70.66920 1.5 18.09974 11.0 Too large 9.0 Too large 7.0 Too large 6.0 Too large -5.0 -622.76391 -4.0 -318.00000 2.0 41.41421 3.0 136.73205 1.0 6.00000
PL/M
Assuming the existence of suitable external library routines.
TPK: DO;
/* external I/O and real mathematical routines */
WRITE$STRING: PROCEDURE( S ) EXTERNAL; DECLARE S POINTER; END;
WRITE$REAL: PROCEDURE( R ) EXTERNAL; DECLARE R REAL; END;
WRITE$NL: PROCEDURE EXTERNAL; END;
READ$REAL: PROCEDURE( R ) REAL EXTERNAL; DECLARE R POINTER; END;
REAL$ABS: PROCEDURE( R ) REAL EXTERNAL; DECLARE R REAL; END;
REAL$SQRT: PROCEDURE( R ) REAL EXTERNAL; DECLARE R REAL; END;
/* end external routines */
F: PROCEDURE( T ) REAL;
DECLARE T REAL;
RETURN REAL$SQRT(REAL$ABS(T))+5*T*T*T;
END F;
MAIN: PROCEDURE;
DECLARE Y REAL, A( 11 ) REAL, I INTEGER;
DO I = 0 TO 10;
CALL READ$REAL( @A( I ) );
END;
DO I = 10 TO 0 BY -1;
Y = F( A( I ) );
IF Y > 400.0 THEN CALL WRITE$STRING( @( 'TOO LARGE', 0 ) );
ELSE CALL WRITE$REAL( Y );
CALL WRITE$NL();
END;
END MAIN;
END TPK;
- Output:
1 2 3 4 5 6 7 8 9 10 11 TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE TOO LARGE 322.0000 136.7321 41.4142 6.0000
PowerShell
function Get-Tpk
{
[CmdletBinding()]
[OutputType([PSCustomObject])]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[double]
$Number
)
Begin
{
function Get-TpkFunction ([double]$Number)
{
[Math]::Pow([Math]::Abs($Number),(0.5)) + 5 * [Math]::Pow($Number,3)
}
[object[]]$output = @()
}
Process
{
$Number | ForEach-Object {
$n = Get-TpkFunction $_
if ($n -le 400)
{
$result = $n
}
else
{
$result = "Overflow"
}
}
$output += [PSCustomObject]@{
Number = $Number
Result = $result
}
}
End
{
[Array]::Reverse($output)
$output
}
}
$tpk = 1..11 | Get-Tpk
$tpk
- Output:
Number Result ------ ------ 11 Overflow 10 Overflow 9 Overflow 8 Overflow 7 Overflow 6 Overflow 5 Overflow 4 322 3 136.732050807569 2 41.4142135623731 1 6
Sort back to ascending order ignoring Overflow results:
$tpk | where result -ne overflow | sort number
- Output:
Number Result ------ ------ 1 6 2 41.4142135623731 3 136.732050807569 4 322
Python
Functional
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
>>> print(', '.join('%s:%s' % (x, v if v<=400 else "TOO LARGE!")
for x,v in ((y, f(float(y))) for y in input('\nnumbers: ').strip().split()[:11][::-1])))
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
>>>
Procedural
import math
def f(x):
return math.sqrt(abs(x)) + 5 * x**3
def ask_numbers(n=11):
print(f'Enter {n} numbers:')
return (float(input('>')) for _ in range(n))
if __name__ == '__main__':
for x in ask_numbers().reverse():
if (result := f(x)) > 400:
print(f'f({x}): overflow')
else:
print(f'f({x}) = {result}')
- Output:
Enter 11 numbers: >1 >532 >465 >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
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 ( --> )
- Output:
As a dialogue in the Quackery shell.
/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
R
S <- scan(n=11)
f <- function(x) sqrt(abs(x)) + 5*x^3
for (i in rev(S)) {
res <- f(i)
if (res > 400)
print("Too large!")
else
print(res)
}
- Sample output:
> source("~/tpk.R") 1: 1 2 3 4 5 6: 6 7 8 9 10 11: 11 Read 11 items [1] "Too large!" [1] "Too large!" [1] "Too large!" [1] "Too large!" [1] "Too large!" [1] "Too large!" [1] "Too large!" [1] 322 [1] 136.7321 [1] 41.41421 [1] 6
Racket
#lang racket
(define input
(for/list ([i 11])
(printf "Enter a number (~a of 11): " (+ 1 i))
(read)))
(for ([x (reverse input)])
(define res (+ (sqrt (abs x)) (* 5 (expt x 3))))
(if (> res 400)
(displayln "Overflow!")
(printf "f(~a) = ~a\n" x res)))
- Output:
Enter a number (1 of 11): 1 Enter a number (2 of 11): 2 Enter a number (3 of 11): 3 Enter a number (4 of 11): 4 Enter a number (5 of 11): 5 Enter a number (6 of 11): 6 Enter a number (7 of 11): 7 Enter a number (8 of 11): 8 Enter a number (9 of 11): 9 Enter a number (10 of 11): 10 Enter a number (11 of 11): 11 Overflow! Overflow! Overflow! Overflow! Overflow! Overflow! Overflow! f(4) = 322 f(3) = 136.73205080756887 f(2) = 41.41421356237309 f(1) = 6
Raku
(formerly Perl 6)
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 }";
}
- Output:
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!
REXX
The REXX language doesn't have a sqrt function, so a RYO version is included here. [RYO = Roll Your Own.]
It could be noted that almost half of this program is devoted to prompting, parsing and validating of the (input) numbers,
not to mention some hefty code to support right-justified numbers such that they are aligned when displayed.
/*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.*/
maxValue= 400 /*the maximum value f(x) can have. */
wid= 20 /* ··· but only show this many digits.*/
frac= 5 /* ··· show this # of fractional digs.*/
say ' _____' /* ◄─── this SAY displays a vinculum.*/
say 'function: ƒ(x) ≡ √ │x│ + (5 * x^3)'
prompt= 'enter ' N " numbers for the Trabb─Pardo─Knuth algorithm: (or Quit)"
do ask=0; say; /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
say prompt; say; pull $; say /*░*/
if abbrev('QUIT',$,1) then do; say 'quitting.'; exit 1; end /*░*/
ok=0 /*░*/
select /*validate there're N numbers.*/ /*░*/
when $='' then say "no numbers entered" /*░*/
when words($)<N then say "not enough numbers entered" /*░*/
when words($)>N then say "too many numbers entered" /*░*/
otherwise ok=1 /*░*/
end /*select*/ /*░*/
if \ok then iterate /* [↓] W=max width. */ /*░*/
w=0; do v=1 for N; _=word($, v); w=max(w, length(_) ) /*░*/
if datatype(_, 'N') then iterate /*numeric ?*/ /*░*/
say _ "isn't numeric"; iterate ask /*░*/
end /*v*/ /*░*/
leave /*░*/
end /*ask*/ /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
say 'numbers entered: ' $
say
do i=N by -1 for N; #=word($, i) / 1 /*process the numbers in reverse. */
g = fmt( f( # ) ) /*invoke function ƒ with arg number.*/
gw=right( 'ƒ('#") ", w+7) /*nicely formatted ƒ(number). */
if g>maxValue then say gw "is > " maxValue ' ['space(g)"]"
else say gw " = " g
end /*i*/ /* [↑] display the result to terminal.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
f: procedure; parse arg x; return sqrt( abs(x) ) + 5 * x**3
/*──────────────────────────────────────────────────────────────────────────────────────*/
fmt: z=right(translate(format(arg(1), wid, frac), 'e', "E"), wid) /*right adjust; use e*/
if pos(.,z)\==0 then z=left(strip(strip(z,'T',0),"T",.),wid) /*strip trailing 0 &.*/
return right(z, wid - 4*(pos('e', z)==0) ) /*adjust: no exponent*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6
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
- output when prompted, using the input of: 5 3.3 3 2e-1 1 0 -1 -222 -33 4.0004 +5
_____ function: ƒ(x) ≡ √ │x│ + (5 * x^3) enter 11 numbers for the Trabb─Pardo─Knuth algorithm: (or Quit) 5 3.3 3 2e-1 1 0 -1 -222 -33 4.0004 +5 ◄■■■■■■■■■■■ this is what the user entered. numbers entered: 5 3.3 3 2E-1 1 0 -1 -222 -33 4.0004 +5 ƒ(5) is > 400 [627.23607] ƒ(4.0004) = 322.09611 ƒ(-33) = -179679.25544 ƒ(-222) = -54705225.10034 ƒ(-1) = -4 ƒ(0) = 0 ƒ(1) = 6 ƒ(0.2) = 0.48721 ƒ(3) = 136.73205 ƒ(3.3) = 181.50159 ƒ(5) is > 400 [627.23607]
Ring
# Project : Trabb Pardo–Knuth algorithm
decimals(3)
x = list(11)
for n=1 to 11
x[n] = n
next
s = [-5, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6]
for i = 1 to 11
see string(i) + " => " + s[i] + nl
next
see copy("-", 20) + nl
i = i - 1
while i > 0
see "f(" + string(s[i]) + ") = "
x = f(s[i])
if x > 400
see "-=< overflow >=-" + nl
else
see x + nl
ok
i = i - 1
end
func f(n)
return sqrt(fabs(n)) + 5 * pow(n, 3)
Output:
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.414 f(1) = 6 f(0) = 0 f(-1) = -4 f(-2) = -38.586 f(-3) = -133.268 f(-5) = -622.764
RPL
Idiomatic RPL is based on use of stack whenever possible, short code and minimalist UX.
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 |
CONT
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 CONT
command.
- Input:
TPK 100 3 0 1 0 1 0 1 0 1 10
- Output:
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!"
Ruby
def f(x) x.abs ** 0.5 + 5 * x ** 3 end
puts "Please enter 11 numbers:"
nums = 11.times.map{ gets.to_f }
nums.reverse_each do |n|
print "f(#{n}) = "
res = f(n)
puts res > 400 ? "Overflow!" : res
end
- Output:
ruby tpk.rb Please enter 11 numbers: 1 2 3 4 5 6 7 8 9 -1 -4 f(-4.0) = -318.0 f(-1.0) = -4.0 f(9.0) = Overflow! f(8.0) = Overflow! f(7.0) = Overflow! f(6.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
Rust
use std::io::{self, BufRead};
fn op(x: f32) -> Option<f32> {
let y = x.abs().sqrt() + 5.0 * x * x * x;
if y < 400.0 {
Some(y)
} else {
None
}
}
fn main() {
println!("Please enter 11 numbers (one number per line)");
let stdin = io::stdin();
let xs = stdin
.lock()
.lines()
.map(|ox| ox.unwrap().trim().to_string())
.flat_map(|s| str::parse::<f32>(&s))
.take(11)
.collect::<Vec<_>>();
for x in xs.into_iter().rev() {
match op(x) {
Some(y) => println!("{}", y),
None => println!("overflow"),
};
}
}
- Output:
Enter 11 numbers (one number per line) 1 2 3 4 5 6 7 8 9 10 11 overflow overflow overflow overflow overflow overflow overflow 322 136.73206 41.414215 6
Scala
object TPKa extends App {
final val numbers = scala.collection.mutable.MutableList[Double]()
final val in = new java.util.Scanner(System.in)
while (numbers.length < CAPACITY) {
print("enter a number: ")
try {
numbers += in.nextDouble()
}
catch {
case _: Exception =>
in.next()
println("invalid input, try again")
}
}
numbers reverseMap { x =>
val fx = Math.pow(Math.abs(x), .5D) + 5D * (Math.pow(x, 3))
if (fx < THRESHOLD)
print("%8.3f -> %8.3f\n".format(x, fx))
else
print("%8.3f -> %s\n".format(x, Double.PositiveInfinity.toString))
}
private final val THRESHOLD = 400D
private final val CAPACITY = 11
}
Sidef
var nums; do {
nums = Sys.readln("Please type 11 space-separated numbers: ").nums
} while(nums.len != 11)
nums.reverse.each { |n|
var r = (n.abs.sqrt + (5 * n**3));
say "#{n}\t#{ r > 400 ? 'Urk!' : r }";
}
- Output:
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.886299747726800445468371077898575778355 4.302 Urk! 4.303 Urk! 4.305 Urk! 4.3 399.608644135332772087455898679984992632401 4 322 3 136.732050807568877293527446341505872366943 2 41.41421356237309504880168872420969807857 1 6 -1 -4 10 Urk!
Swift
import Foundation
print("Enter 11 numbers for the Trabb─Pardo─Knuth algorithm:")
let f: (Double) -> Double = { sqrt(fabs($0)) + 5 * pow($0, 3) }
(1...11)
.generate()
.map { i -> Double in
print("\(i): ", terminator: "")
guard let s = readLine(), let n = Double(s) else { return 0 }
return n
}
.reverse()
.forEach {
let result = f($0)
print("f(\($0))", result > 400.0 ? "OVERFLOW" : result, separator: "\t")
}
- Output:
Enter 11 numbers for the Trabb─Pardo─Knuth algorithm: 1: 1 2: 2 3: 3 4: 4 5: 5 6: 6 7: 7 8: 8 9: 9 10: 10 11: 11 f(11.0) OVERFLOW f(10.0) OVERFLOW f(9.0) OVERFLOW f(8.0) OVERFLOW f(7.0) OVERFLOW f(6.0) OVERFLOW f(5.0) OVERFLOW f(4.0) 322.0 f(3.0) 136.732050807569 f(2.0) 41.4142135623731 f(1.0) 6.0
Symsyn
|Trabb Pardo–Knuth algorithm
a : 11 0
i
if i LE 10
[] $s
~ $s w
w a.i
+ i
goif
endif
10 i
if i GE 0
call f
if x GT 400
'too large' $s
else
~ x $s
endif
~ i $r
+ ' ' $r
+ $r $s.1
$s []
- i
goif
endif
stop
f a.i t
* t t x
* x t x
* 5 x
abs t
sqrt t y
+ y x
return
Tcl
# Helper procedures
proc f {x} {expr {abs($x)**0.5 + 5*$x**3}}
proc overflow {y} {expr {$y > 400}}
# Read in 11 numbers, with nice prompting
fconfigure stdout -buffering none
for {set n 1} {$n <= 11} {incr n} {
puts -nonewline "number ${n}: "
lappend S [scan [gets stdin] "%f"]
}
# Process and print results in reverse order
foreach x [lreverse $S] {
set result [f $x]
if {[overflow $result]} {
puts "${x}: TOO LARGE!"
} else {
puts "${x}: $result"
}
}
- Sample run:
number 1: 0 number 2: 1 number 3: 2 number 4: 3 number 5: 4 number 6: 5 number 7: 6 number 8: 7 number 9: 8 number 10: 9 number 11: 10 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 0.0: 0.0
UNIX Shell
rc
Uses bc for the math. Should work with both versions of rc: Plan 9 Port By Duff and its clone by Rakitzis
fn f {
printf 'scale=3; sqrt(sqrt(%s^2)) + 5.0 * %s ^ 3 \n' $1 $1 | bc
}
count = 11
list = ()
for (idx in `{seq $count}) {
num = `{read} # get value from stdin
list = ($num $list) # add to list in reverse
}
for (idx in `{seq $count}) {
x = $list($idx)
printf 'f(%s) = ' $x
y = `{f $x}
if (~ `{printf '%s > 400 \n' $y | bc} 1)
echo TOO LARGE!
if not echo $y
}
- Sample run:
; seq 0 10 | rc trabb_pardo-knuth.rc f(10) = TOO LARGE! f(9) = TOO LARGE! f(8) = TOO LARGE! f(7) = TOO LARGE! f(6) = TOO LARGE! f(5) = TOO LARGE! f(4) = 322.000 f(3) = 136.732 f(2) = 41.414 f(1) = 6.0 f(0) = 0
Wren
import "io" for Stdin, Stdout
import "./fmt" for Fmt
var f = Fn.new { |x| x.abs.sqrt + 5*x*x*x }
var s = List.filled(11, 0)
System.print("Please enter 11 numbers:")
var count = 0
while (count < 11) {
Fmt.write(" Number $-2d : ", count + 1)
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)
}
}
- Output:
Entering the same numbers as the Ada example:
Please enter 11 numbers: Number 1 : 10 Number 2 : -1 Number 3 : 1 Number 4 : 2 Number 5 : 3 Number 6 : 4 Number 7 : 4.3 Number 8 : 4.305 Number 9 : 4.303 Number 10 : 4.302 Number 11 : 4.301 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
XPL0
include c:\cxpl\codes;
func real F(X);
real X;
return sqrt(abs(X)) + 5.0*X*X*X;
real Result, S(11); int I;
[Text(0, "Please enter 11 numbers: ");
for I:= 0 to 11-1 do S(I):= RlIn(0);
for I:= 11-1 downto 0 do
[RlOut(0, S(I));
Result:= F(S(I));
if Result > 400.0 then
Text(0, " overflows")
else RlOut(0, Result);
CrLf(0)];
]
- Output:
Please enter 11 numbers: 10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301 4.30100 399.88630 4.30200 overflows 4.30300 overflows 4.30500 overflows 4.30000 399.60864 4.00000 322.00000 3.00000 136.73205 2.00000 41.41421 1.00000 6.00000 -1.00000 -4.00000 10.00000 overflows
zkl
fcn f(x) { x.abs().pow(0.5) + x.pow(3)*5 }
reg ns; do{
ns=ask("11 numbers seperated by spaces: ");
try{ ns=ns.split(" ").filter().apply("toFloat") } catch{}
}while(not ns.isType(List) or ns.len()!=11);
ns.reverse().apply(fcn(x){
fx:=f(x); "f(%7.3f)-->%s".fmt(x, if(fx>400)"Overflow" else fx) })
.pump(Console.println);
- Output:
11 numbers seperated by spaces: 10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301 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 f( 3.000)-->136.732 f( 2.000)-->41.4142 f( 1.000)-->6 f( -1.000)-->-4 f( 10.000)-->Overflow
- Programming Tasks
- Solutions by Programming Task
- 11l
- Ada
- Agena
- ALGOL 60
- ALGOL 68
- ALGOL W
- APL
- Arturo
- AutoIt
- AutoHotkey
- AWK
- BASIC
- ANSI BASIC
- Applesoft BASIC
- ASIC
- BASIC256
- Chipmunk Basic
- Commodore BASIC
- FreeBASIC
- GW-BASIC
- Liberty BASIC
- Minimal BASIC
- Nascom BASIC
- PureBasic
- QBasic
- Run BASIC
- Sinclair ZX81 BASIC
- True BASIC
- VBScript
- XBasic
- Yabasic
- C
- C++
- Common Lisp
- D
- Delphi
- SysUtils,StdCtrls
- EasyLang
- EchoLisp
- Ela
- Elena
- Elixir
- Erlang
- ERRE
- F Sharp
- Factor
- Forth
- Fortran
- FutureBasic
- Go
- Haskell
- Icon
- Unicon
- Imp77
- Io
- J
- Java
- JavaScript
- Jq
- Julia
- Kotlin
- Ksh
- Lua
- M2000 Interpreter
- Maple
- Mathematica
- Wolfram Language
- MATLAB
- Min
- Modula-2
- Nim
- Objective-C
- OCaml
- PARI/GP
- PascalABC.NET
- Perl
- Phix
- PHP
- Picat
- PicoLisp
- PL/I
- PL/M
- PowerShell
- Python
- Quackery
- R
- Racket
- Raku
- REXX
- Ring
- RPL
- Ruby
- Rust
- Scala
- Sidef
- Swift
- Symsyn
- Tcl
- UNIX Shell
- Wren
- Wren-fmt
- XPL0
- Zkl