Trabb Pardo–Knuth algorithm: Difference between revisions
Content added Content deleted
Alpha bravo (talk | contribs) (Added AutoHotkey) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 28: | Line 28: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F f(x) |
||
R sqrt(abs(x)) + 5 * x ^ 3 |
R sqrt(abs(x)) + 5 * x ^ 3 |
||
Line 39: | Line 39: | ||
E |
E |
||
print(‘#.: #.’.format(x, result)) |
print(‘#.: #.’.format(x, result)) |
||
print()</ |
print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 58: | Line 58: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions; |
||
procedure Trabb_Pardo_Knuth is |
procedure Trabb_Pardo_Knuth is |
||
Line 93: | Line 93: | ||
end loop; |
end loop; |
||
end Trabb_Pardo_Knuth;</ |
end Trabb_Pardo_Knuth;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 113: | Line 113: | ||
Tested with Agena 2.9.5 Win32 |
Tested with Agena 2.9.5 Win32 |
||
{{Trans|ALGOL W}} |
{{Trans|ALGOL W}} |
||
< |
<syntaxhighlight lang="agena">scope # TPK algorithm in Agena |
||
local y; |
local y; |
||
local a := []; |
local a := []; |
||
Line 125: | Line 125: | ||
fi |
fi |
||
od |
od |
||
epocs</ |
epocs</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 155: | Line 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. |
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. |
||
<lang>begin |
<syntaxhighlight lang="text">begin |
||
integer i; real y; real array a[0:10]; |
integer i; real y; real array a[0:10]; |
||
real procedure f(t); value t; real t; |
real procedure f(t); value t; real t; |
||
Line 167: | Line 167: | ||
outchar(1, "\n", 1) |
outchar(1, "\n", 1) |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
Compilation and sample run: |
Compilation and sample run: |
||
Line 190: | Line 190: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{Trans|ALGOL W}} which was itself a Translation of ALGOL 60. |
{{Trans|ALGOL W}} which was itself a Translation of ALGOL 60. |
||
< |
<syntaxhighlight lang="algol68">[ 0 : 10 ]REAL a; |
||
PROC f = ( REAL t )REAL: |
PROC f = ( REAL t )REAL: |
||
sqrt(ABS t)+5*t*t*t; |
sqrt(ABS t)+5*t*t*t; |
||
Line 199: | Line 199: | ||
ELSE print( ( fixed( y, -9, 4 ), newline ) ) |
ELSE print( ( fixed( y, -9, 4 ), newline ) ) |
||
FI |
FI |
||
OD</ |
OD</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 218: | Line 218: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
{{Trans|ALGOL 60}} |
{{Trans|ALGOL 60}} |
||
< |
<syntaxhighlight lang="algolw">begin |
||
real y; real array a( 0 :: 10 ); |
real y; real array a( 0 :: 10 ); |
||
real procedure f( real value t ); |
real procedure f( real value t ); |
||
Line 230: | Line 230: | ||
else write( y ); |
else write( y ); |
||
end |
end |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 249: | Line 249: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog 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 |
f←{(0.5*⍨|⍵)+5×⍵*3} ⍝ define a function f |
||
S←,⍎{⍞←⍵ ⋄ (≢⍵)↓⍞}'Please, enter 11 numbers: ' |
S←,⍎{⍞←⍵ ⋄ (≢⍵)↓⍞}'Please, enter 11 numbers: ' |
||
Line 259: | Line 259: | ||
:EndIf |
:EndIf |
||
:EndFor |
:EndFor |
||
∇</ |
∇</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">proc: function [x]-> |
||
((abs x) ^ 0.5) + 5 * x ^ 3 |
((abs x) ^ 0.5) + 5 * x ^ 3 |
||
Line 273: | Line 273: | ||
result: proc n |
result: proc n |
||
print [n ":" (result > 400)? -> "TOO LARGE!" -> result] |
print [n ":" (result > 400)? -> "TOO LARGE!" -> result] |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 295: | Line 295: | ||
* ASIC does not have the fuction that calculates a square root. Thus, the program uses the added subprogram <code>CALCSQRT:</code>. |
* 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. |
* ASIC does not scroll a text screen, but it wraps the text. Thus, the program clears the screen after the user enters numbers. |
||
<lang> |
<syntaxhighlight lang="text"> |
||
REM Trabb Pardo-Knuth algorithm |
REM Trabb Pardo-Knuth algorithm |
||
REM Used "magic numbers" because of strict specification of the algorithm. |
REM Used "magic numbers" because of strict specification of the algorithm. |
||
Line 374: | Line 374: | ||
SQRT@ = MIDDLE@ |
SQRT@ = MIDDLE@ |
||
RETURN |
RETURN |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Enter the data. |
Enter the data. |
||
Line 407: | Line 407: | ||
=={{header|AutoIt}}== |
=={{header|AutoIt}}== |
||
< |
<syntaxhighlight lang="autoit">; Trabb Pardo–Knuth algorithm |
||
; by James1337 (autoit.de) |
; by James1337 (autoit.de) |
||
; AutoIt Version: 3.3.8.1 |
; AutoIt Version: 3.3.8.1 |
||
Line 430: | Line 430: | ||
Func f($x) |
Func f($x) |
||
Return Sqrt(Abs($x)) + 5*$x^3 |
Return Sqrt(Abs($x)) + 5*$x^3 |
||
EndFunc</ |
EndFunc</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Input: "1 2 3 4 5 6 7 8 9 10 11" |
<pre>Input: "1 2 3 4 5 6 7 8 9 10 11" |
||
Line 447: | Line 447: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">seq := [1,2,3,4,5,6,7,8,9,10,11] |
||
MsgBox % result := TPK(seq, 400) |
MsgBox % result := TPK(seq, 400) |
||
return |
return |
||
Line 463: | Line 463: | ||
f(x){ |
f(x){ |
||
return Sqrt(x) + 5* (x**3) |
return Sqrt(x) + 5* (x**3) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>11 : OVERFLOW |
<pre>11 : OVERFLOW |
||
Line 478: | Line 478: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f TRABB_PARDO-KNUTH_ALGORITHM.AWK |
# syntax: GAWK -f TRABB_PARDO-KNUTH_ALGORITHM.AWK |
||
BEGIN { |
BEGIN { |
||
Line 496: | Line 496: | ||
function abs(x) { if (x >= 0) { return x } else { return -x } } |
function abs(x) { if (x >= 0) { return x } else { return -x } } |
||
function f(x) { return sqrt(abs(x)) + 5 * x ^ 3 } |
function f(x) { return sqrt(abs(x)) + 5 * x ^ 3 } |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 516: | Line 516: | ||
==={{header|Minimal BASIC}}=== |
==={{header|Minimal BASIC}}=== |
||
{{works with|QBasic}} |
{{works with|QBasic}} |
||
< |
<syntaxhighlight lang="gwbasic"> |
||
10 REM Trabb Pardo-Knuth algorithm |
10 REM Trabb Pardo-Knuth algorithm |
||
20 REM Used "magic numbers" because of strict specification |
20 REM Used "magic numbers" because of strict specification |
||
Line 544: | Line 544: | ||
260 NEXT I |
260 NEXT I |
||
270 END |
270 END |
||
</syntaxhighlight> |
|||
</lang> |
|||
==={{header|QBasic}}=== |
==={{header|QBasic}}=== |
||
{{works with|QBasic|1.1}} |
{{works with|QBasic|1.1}} |
||
< |
<syntaxhighlight lang="qbasic">FUNCTION f (n!) |
||
f = SQR(ABS(n)) + 5 * n ^ 3 |
f = SQR(ABS(n)) + 5 * n ^ 3 |
||
END FUNCTION |
END FUNCTION |
||
Line 572: | Line 572: | ||
i = i - 1 |
i = i - 1 |
||
LOOP UNTIL i < 1 |
LOOP UNTIL i < 1 |
||
END</ |
END</syntaxhighlight> |
||
==={{header|True BASIC}}=== |
==={{header|True BASIC}}=== |
||
{{works with|QBasic}} |
{{works with|QBasic}} |
||
< |
<syntaxhighlight lang="qbasic">FUNCTION f (n) |
||
LET f = SQR(ABS(n)) + 5 * n ^ 3 |
LET f = SQR(ABS(n)) + 5 * n ^ 3 |
||
END FUNCTION |
END FUNCTION |
||
Line 601: | Line 601: | ||
LET i = i - 1 |
LET i = i - 1 |
||
LOOP UNTIL i < 1 |
LOOP UNTIL i < 1 |
||
END</ |
END</syntaxhighlight> |
||
==={{header|Yabasic}}=== |
==={{header|Yabasic}}=== |
||
< |
<syntaxhighlight lang="yabasic">sub f(n) |
||
return sqr(abs(n)) + 5.0 * n ^ 3.0 |
return sqr(abs(n)) + 5.0 * n ^ 3.0 |
||
end sub |
end sub |
||
Line 625: | Line 625: | ||
endif |
endif |
||
next i |
next i |
||
end</ |
end</syntaxhighlight> |
||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
< |
<syntaxhighlight lang="basic256">dim s(11) |
||
print 'enter 11 numbers' |
print 'enter 11 numbers' |
||
for i = 0 to 10 |
for i = 0 to 10 |
||
Line 647: | Line 647: | ||
function f(n) |
function f(n) |
||
return sqrt(abs(n))+5*n^3 |
return sqrt(abs(n))+5*n^3 |
||
end function</ |
end function</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>enter 11 numbers |
<pre>enter 11 numbers |
||
Line 674: | Line 674: | ||
=={{header|C}}== |
=={{header|C}}== |
||
<syntaxhighlight lang="c"> |
|||
<lang c> |
|||
#include<math.h> |
#include<math.h> |
||
#include<stdio.h> |
#include<stdio.h> |
||
Line 712: | Line 712: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Please enter 11 numbers :10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301 |
<pre>Please enter 11 numbers :10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301 |
||
Line 732: | Line 732: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <iostream> |
#include <iostream> |
||
#include <cmath> |
#include <cmath> |
||
Line 756: | Line 756: | ||
} |
} |
||
return 0 ; |
return 0 ; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Please enter 11 numbers! |
<pre>Please enter 11 numbers! |
||
Line 785: | Line 785: | ||
{{trans|XBasic}} |
{{trans|XBasic}} |
||
{{works with|Commodore BASIC|4.5}} |
{{works with|Commodore BASIC|4.5}} |
||
< |
<syntaxhighlight lang="basic"> |
||
10 REM TRABB PARDO-KNUTH ALGORITHM |
10 REM TRABB PARDO-KNUTH ALGORITHM |
||
20 REM USED "MAGIC NUMBERS" BECAUSE OF STRICT SPECIFICATION OF THE ALGORITHM. |
20 REM USED "MAGIC NUMBERS" BECAUSE OF STRICT SPECIFICATION OF THE ALGORITHM. |
||
Line 809: | Line 809: | ||
220 NEXT I |
220 NEXT I |
||
230 END |
230 END |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 839: | Line 839: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun read-numbers () |
||
(princ "Enter 11 numbers (space-separated): ") |
(princ "Enter 11 numbers (space-separated): ") |
||
(let ((numbers '())) |
(let ((numbers '())) |
||
Line 851: | Line 851: | ||
(trabb-pardo-knuth (lambda (x) (+ (expt (abs x) 0.5) (* 5 (expt x 3)))) |
(trabb-pardo-knuth (lambda (x) (+ (expt (abs x) 0.5) (* 5 (expt x 3)))) |
||
(lambda (x) (> x 400)))</ |
(lambda (x) (> x 400)))</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 868: | Line 868: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.math, std.conv, std.algorithm, std.array; |
||
double f(in double x) pure nothrow { |
double f(in double x) pure nothrow { |
||
Line 888: | Line 888: | ||
writefln("f(%0.3f) = %s", x, y > 400 ? "Too large" : y.text); |
writefln("f(%0.3f) = %s", x, y > 400 ? "Too large" : y.text); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Please enter eleven numbers on a line: 1 2 3 -4.55 5.1111 6 -7 8 9 10 |
<pre>Please enter eleven numbers on a line: 1 2 3 -4.55 5.1111 6 -7 8 9 10 |
||
Line 906: | Line 906: | ||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
(define (trabb-fun n) |
(define (trabb-fun n) |
||
(+ (* 5 n n n) (sqrt(abs n)))) |
(+ (* 5 n n n) (sqrt(abs n)))) |
||
Line 925: | Line 925: | ||
(error 'incomplete-list numlist))))) ;; users cancel |
(error 'incomplete-list numlist))))) ;; users cancel |
||
(for-each check-trabb (reverse (take numlist 11)))) |
(for-each check-trabb (reverse (take numlist 11)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="scheme"> |
||
(trabb) |
(trabb) |
||
;; input : (0 4 1 8 5 9 10 3 6 7 2) |
;; input : (0 4 1 8 5 9 10 3 6 7 2) |
||
Line 948: | Line 948: | ||
(root g 0 10) |
(root g 0 10) |
||
→ 4.301409367213084 |
→ 4.301409367213084 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ela}}== |
=={{header|Ela}}== |
||
Line 954: | Line 954: | ||
Translation of OCaml version: |
Translation of OCaml version: |
||
< |
<syntaxhighlight lang="ela">open monad io number string |
||
:::IO |
:::IO |
||
Line 977: | Line 977: | ||
do |
do |
||
putStrLn "Please enter 11 numbers:" |
putStrLn "Please enter 11 numbers:" |
||
take_numbers 11 []</ |
take_numbers 11 []</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,007: | Line 1,007: | ||
{{trans|C}} |
{{trans|C}} |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
< |
<syntaxhighlight lang="elena">import extensions; |
||
import extensions'math; |
import extensions'math; |
||
Line 1,035: | Line 1,035: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,066: | Line 1,066: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Erlang}} |
{{trans|Erlang}} |
||
< |
<syntaxhighlight lang="elixir">defmodule Trabb_Pardo_Knuth do |
||
def task do |
def task do |
||
Enum.reverse( get_11_numbers ) |
Enum.reverse( get_11_numbers ) |
||
Line 1,089: | Line 1,089: | ||
end |
end |
||
Trabb_Pardo_Knuth.task</ |
Trabb_Pardo_Knuth.task</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,108: | Line 1,108: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module( trabb_pardo_knuth ). |
-module( trabb_pardo_knuth ). |
||
Line 1,132: | Line 1,132: | ||
perform_operation_check_overflow( N, Result, Overflow ) when Result > Overflow -> alert( N ); |
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] ). |
perform_operation_check_overflow( N, Result, _Overflow ) -> io:fwrite( "f(~p) => ~p~n", [N, Result] ). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,151: | Line 1,151: | ||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
!Trabb Pardo-Knuth algorithm |
!Trabb Pardo-Knuth algorithm |
||
PROGRAM TPK |
PROGRAM TPK |
||
Line 1,174: | Line 1,174: | ||
END FOR |
END FOR |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
Numbers to be elaborated is included in the program with a DATA statement. You can substitute |
Numbers to be elaborated is included in the program with a DATA statement. You can substitute |
||
this with an input keyboard like this |
this with an input keyboard like this |
||
Line 1,184: | Line 1,184: | ||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
module ``Trabb Pardo - Knuth`` |
module ``Trabb Pardo - Knuth`` |
||
open System |
open System |
||
Line 1,194: | Line 1,194: | ||
| n when n <= 400.0 -> Console.WriteLine(n) |
| n when n <= 400.0 -> Console.WriteLine(n) |
||
| _ -> Console.WriteLine("Overflow")) |
| _ -> Console.WriteLine("Overflow")) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>fsharpi Program.fsx |
<pre>fsharpi Program.fsx |
||
Line 1,224: | Line 1,224: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: formatting io kernel math math.functions math.parser |
||
prettyprint sequences splitting ; |
prettyprint sequences splitting ; |
||
IN: rosetta-code.trabb-pardo-knuth |
IN: rosetta-code.trabb-pardo-knuth |
||
Line 1,247: | Line 1,247: | ||
[ string>number [ fn ] ?result print ] each ; |
[ string>number [ fn ] ?result print ] each ; |
||
MAIN: main</ |
MAIN: main</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,266: | Line 1,266: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: f(x) fdup fsqrt fswap 3e f** 5e f* f+ ; |
||
4e2 fconstant f-too-big |
4e2 fconstant f-too-big |
||
Line 1,305: | Line 1,305: | ||
: tpk ( -- ) |
: tpk ( -- ) |
||
get-it reverse-it do-it ;</ |
get-it reverse-it do-it ;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,339: | Line 1,339: | ||
===Fortran 95=== |
===Fortran 95=== |
||
{{works with|Fortran|95 and later}} |
{{works with|Fortran|95 and later}} |
||
< |
<syntaxhighlight lang="fortran">program tpk |
||
implicit none |
implicit none |
||
Line 1,368: | Line 1,368: | ||
end function |
end function |
||
end program</ |
end program</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> Input eleven numbers: |
<pre> Input eleven numbers: |
||
Line 1,386: | Line 1,386: | ||
===Fortran I=== |
===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. |
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. |
||
< |
<syntaxhighlight lang="fortran">C THE TPK ALGORITH - FORTRAN I - 1957 TPK00010 |
||
FTPKF(X)=SQRTF(ABSF(X))+5.0*X**3 TPK00020 |
FTPKF(X)=SQRTF(ABSF(X))+5.0*X**3 TPK00020 |
||
DIMENSION A(11) TPK00030 |
DIMENSION A(11) TPK00030 |
||
Line 1,402: | Line 1,402: | ||
3 CONTINUE TPK00150 |
3 CONTINUE TPK00150 |
||
STOP 0 TPK00160 |
STOP 0 TPK00160 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' version 22-07-2017 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
Line 1,441: | Line 1,441: | ||
Print : Print "hit any key to end program" |
Print : Print "hit any key to end program" |
||
Sleep |
Sleep |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 => -5 |
<pre>1 => -5 |
||
Line 1,471: | Line 1,471: | ||
===Task/Wikipedia=== |
===Task/Wikipedia=== |
||
This solution follows the task description by reversing the sequence. It also rejects non-numeric input until 11 numbers are entered. |
This solution follows the task description by reversing the sequence. It also rejects non-numeric input until 11 numbers are entered. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,508: | Line 1,508: | ||
result := math.Sqrt(math.Abs(x)) + 5*x*x*x |
result := math.Sqrt(math.Abs(x)) + 5*x*x*x |
||
return result, result > 400 |
return result, result > 400 |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
The input is chosen to show some interesting boundary cases. |
The input is chosen to show some interesting boundary cases. |
||
Line 1,527: | Line 1,527: | ||
===TPK paper=== |
===TPK paper=== |
||
The original paper had no requirement to reverse the sequence in place, but instead processed the sequence in reverse order. |
The original paper had no requirement to reverse the sequence in place, but instead processed the sequence in reverse order. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,550: | Line 1,550: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Control.Monad (replicateM, mapM_) |
||
f :: Floating a => a -> a |
f :: Floating a => a -> a |
||
Line 1,568: | Line 1,568: | ||
else print x) . |
else print x) . |
||
f) $ |
f) $ |
||
reverse x</ |
reverse x</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Enter 11 numbers for evaluation |
<pre>Enter 11 numbers for evaluation |
||
Line 1,600: | Line 1,600: | ||
<tt>reverse(S)</tt> with <tt>S[*S to 1 by -1]</tt>. |
<tt>reverse(S)</tt> with <tt>S[*S to 1 by -1]</tt>. |
||
< |
<syntaxhighlight lang="unicon">procedure main() |
||
S := [] |
S := [] |
||
writes("Enter 11 numbers: ") |
writes("Enter 11 numbers: ") |
||
Line 1,610: | Line 1,610: | ||
procedure f(x) |
procedure f(x) |
||
return abs(x)^0.5 + 5*x^3 |
return abs(x)^0.5 + 5*x^3 |
||
end</ |
end</syntaxhighlight> |
||
Sample run: |
Sample run: |
||
Line 1,633: | Line 1,633: | ||
=={{header|Io}}== |
=={{header|Io}}== |
||
<syntaxhighlight lang="io"> |
|||
<lang Io> |
|||
// Initialize objects to be used |
// Initialize objects to be used |
||
in_num := File standardInput() |
in_num := File standardInput() |
||
Line 1,657: | Line 1,657: | ||
) |
) |
||
) |
) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,694: | Line 1,694: | ||
No checks are done if the input is actually numbers and if there are actually eleven of them. This doesn't affect the algorithm. |
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. |
Additional checks can be done separately. |
||
< |
<syntaxhighlight lang="j">tpk=: 3 :0 |
||
smoutput 'Enter 11 numbers: ' |
smoutput 'Enter 11 numbers: ' |
||
t1=: ((5 * ^&3) + (^&0.5@* *))"0 |. _999&".;._1 ' ' , 1!:1 [ 1 |
t1=: ((5 * ^&3) + (^&0.5@* *))"0 |. _999&".;._1 ' ' , 1!:1 [ 1 |
||
smoutput 'Values of functions of reversed input: ' , ": t1 |
smoutput 'Values of functions of reversed input: ' , ": t1 |
||
; <@(,&' ')@": ` ((<'user alert ')&[) @. (>&400)"0 t1 |
; <@(,&' ')@": ` ((<'user alert ')&[) @. (>&400)"0 t1 |
||
)</ |
)</syntaxhighlight> |
||
A possible use scenario: |
A possible use scenario: |
||
< |
<syntaxhighlight lang="j"> tpk '' |
||
Enter 11 numbers: |
Enter 11 numbers: |
||
1 2 3 4 5 6 7 8.8 _9 10.123 0 |
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 |
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 |
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. |
Note that the result of tpk is persisted in t1 and is also its explicit result rather than being an explicit output. |
||
Line 1,712: | Line 1,712: | ||
Here's an alternative approach: |
Here's an alternative approach: |
||
< |
<syntaxhighlight lang="j">get11numbers=: 3 :0 |
||
smoutput 'Enter 11 numbers: ' |
smoutput 'Enter 11 numbers: ' |
||
_&". 1!:1]1 |
_&". 1!:1]1 |
||
Line 1,721: | Line 1,721: | ||
overflow400=: 'user alert'"_`":@.(<:&400)"0 |
overflow400=: 'user alert'"_`":@.(<:&400)"0 |
||
tpk=: overflow400@f_x@|.@get11numbers</ |
tpk=: overflow400@f_x@|.@get11numbers</syntaxhighlight> |
||
And, here's this alternative in action: |
And, here's this alternative in action: |
||
< |
<syntaxhighlight lang="j"> tpk'' |
||
Enter 11 numbers: |
Enter 11 numbers: |
||
1 2 3 4 5 6 7 8.8 _9 10.123 0 |
1 2 3 4 5 6 7 8.8 _9 10.123 0 |
||
Line 1,738: | Line 1,738: | ||
136.732 |
136.732 |
||
41.4142 |
41.4142 |
||
6</ |
6</syntaxhighlight> |
||
(clearly, other alternatives are also possible). |
(clearly, other alternatives are also possible). |
||
Line 1,746: | Line 1,746: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">/** |
||
* Alexander Alvonellos |
* Alexander Alvonellos |
||
*/ |
*/ |
||
Line 1,782: | Line 1,782: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,813: | Line 1,813: | ||
=== Spidermonkey === |
=== Spidermonkey === |
||
< |
<syntaxhighlight lang="javascript">#!/usr/bin/env js |
||
function main() { |
function main() { |
||
Line 1,849: | Line 1,849: | ||
main(); |
main(); |
||
</syntaxhighlight> |
|||
</lang> |
|||
Results: |
Results: |
||
Line 1,879: | Line 1,879: | ||
jq does not currently have an interactive mode allowing a prompt to be issued first, |
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. |
and so the initial prompt is implemented here using "echo", in keeping with the jq approach of dovetailing with other command-line tools. |
||
< |
<syntaxhighlight lang="jq">def f: |
||
def abs: if . < 0 then -. else . end; |
def abs: if . < 0 then -. else . end; |
||
def power(x): (x * log) | exp; |
def power(x): (x * log) | exp; |
||
Line 1,889: | Line 1,889: | ||
else error("The number of numbers was not 11.") |
else error("The number of numbers was not 11.") |
||
end |
end |
||
| .[] # print one result per line</ |
| .[] # print one result per line</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight 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 |
jq -M -s -R -f Trabb_Pardo-Knuth_algorithm.jq |
||
> Enter 11 numbers on one line; when done, enter the end-of-file character: |
> Enter 11 numbers on one line; when done, enter the end-of-file character: |
||
Line 1,905: | Line 1,905: | ||
136.73205080756887 |
136.73205080756887 |
||
41.41421356237309 |
41.41421356237309 |
||
6</ |
6</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">f(x) = √abs(x) + 5x^3 |
||
for i in reverse(split(readline())) |
for i in reverse(split(readline())) |
||
v = f(parse(Int, i)) |
v = f(parse(Int, i)) |
||
println("$(i): ", v > 400 ? "TOO LARGE" : v) |
println("$(i): ", v > 400 ? "TOO LARGE" : v) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 2 3 4 5 6 7 8 9 10 11 |
<pre>1 2 3 4 5 6 7 8 9 10 11 |
||
Line 1,928: | Line 1,928: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
fun f(x: Double) = Math.sqrt(Math.abs(x)) + 5.0 * x * x * x |
fun f(x: Double) = Math.sqrt(Math.abs(x)) + 5.0 * x * x * x |
||
Line 1,956: | Line 1,956: | ||
println(v) |
println(v) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,992: | Line 1,992: | ||
=={{header|Ksh}}== |
=={{header|Ksh}}== |
||
< |
<syntaxhighlight lang="ksh"> |
||
#!/bin/ksh |
#!/bin/ksh |
||
Line 2,044: | Line 2,044: | ||
printf "%s\n" "${result}" |
printf "%s\n" "${result}" |
||
fi |
fi |
||
done</ |
done</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
Please input 11 numbers... |
Please input 11 numbers... |
||
Line 2,075: | Line 2,075: | ||
{{trans|XBasic}} |
{{trans|XBasic}} |
||
{{works with|Just BASIC|any}} |
{{works with|Just BASIC|any}} |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
' Trabb Pardo-Knuth algorithm |
' Trabb Pardo-Knuth algorithm |
||
' Used "magic numbers" because of strict specification of the algorithm. |
' Used "magic numbers" because of strict specification of the algorithm. |
||
Line 2,106: | Line 2,106: | ||
f = sqr(abs(n)) + 5 * n * n * n |
f = sqr(abs(n)) + 5 * n * n * n |
||
end function |
end function |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,137: | Line 2,137: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
===Implementation of task description=== |
===Implementation of task description=== |
||
< |
<syntaxhighlight lang="lua">function f (x) return math.abs(x)^0.5 + 5*x^3 end |
||
function reverse (t) |
function reverse (t) |
||
Line 2,154: | Line 2,154: | ||
result = f(x) |
result = f(x) |
||
if result > 400 then print("Overflow!") else print(result) end |
if result > 400 then print("Overflow!") else print(result) end |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Enter 11 numbers... |
<pre>Enter 11 numbers... |
||
Line 2,181: | Line 2,181: | ||
===Line-for-line from TPK paper=== |
===Line-for-line from TPK paper=== |
||
< |
<syntaxhighlight lang="lua">local a, y = {} |
||
function f (t) |
function f (t) |
||
return math.sqrt(math.abs(t)) + 5*t^3 |
return math.sqrt(math.abs(t)) + 5*t^3 |
||
Line 2,190: | Line 2,190: | ||
if y > 400 then print(i, "TOO LARGE") |
if y > 400 then print(i, "TOO LARGE") |
||
else print(i, y) end |
else print(i, y) end |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 |
<pre>1 |
||
Line 2,216: | Line 2,216: | ||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module Input11 { |
Module Input11 { |
||
Flush ' empty stack |
Flush ' empty stack |
||
Line 2,244: | Line 2,244: | ||
Input11 |
Input11 |
||
Run |
Run |
||
</syntaxhighlight> |
|||
</lang> |
|||
To collect the output in clipboard. Global variables need <= to assign values, and document append values using = or <= (for globals) |
To collect the output in clipboard. Global variables need <= to assign values, and document append values using = or <= (for globals) |
||
Line 2,250: | Line 2,250: | ||
Output with "," for decimals (Locale 1032). We can change this using statement Locale 1033 |
Output with "," for decimals (Locale 1032). We can change this using statement Locale 1033 |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Global a$ |
Global a$ |
||
Document a$ ' make a$ as a document - string with paragraphs |
Document a$ ' make a$ as a document - string with paragraphs |
||
Line 2,275: | Line 2,275: | ||
Run 1, 2, 3, -4.55,5.1111, 6, -7, 8, 9, 10, 11 |
Run 1, 2, 3, -4.55,5.1111, 6, -7, 8, 9, 10, 11 |
||
Clipboard a$ |
Clipboard a$ |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,308: | Line 2,308: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="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: |
f:= x -> abs(x)^0.5 + 5*x^3: |
||
for item in seqn do |
for item in seqn do |
||
Line 2,317: | Line 2,317: | ||
print(result): |
print(result): |
||
end if: |
end if: |
||
end do:</ |
end do:</syntaxhighlight> |
||
{{Out|Usage}} |
{{Out|Usage}} |
||
Input:1,2,3,4,5,6,7,8,9,10,11 |
Input:1,2,3,4,5,6,7,8,9,10,11 |
||
Line 2,333: | Line 2,333: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">numbers=RandomReal[{-2,6},11] |
||
tpk[numbers_,overflowVal_]:=Module[{revNumbers}, |
tpk[numbers_,overflowVal_]:=Module[{revNumbers}, |
||
revNumbers=Reverse[numbers]; |
revNumbers=Reverse[numbers]; |
||
Line 2,347: | Line 2,347: | ||
] |
] |
||
] |
] |
||
tpk[numbers,400]</ |
tpk[numbers,400]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,366: | Line 2,366: | ||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.3}} |
{{works with|min|0.19.3}} |
||
< |
<syntaxhighlight lang="min">((0 <) (-1 *) when) :abs |
||
(((abs 0.5 pow) (3 pow 5 * +)) cleave) :fn |
(((abs 0.5 pow) (3 pow 5 * +)) cleave) :fn |
||
"Enter 11 numbers:" puts! |
"Enter 11 numbers:" puts! |
||
(gets float) 11 times |
(gets float) 11 times |
||
(fn (400 <=) (pop "Overflow") unless puts!) 11 times</ |
(fn (400 <=) (pop "Overflow") unless puts!) 11 times</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,401: | Line 2,401: | ||
{{trans|Commodore BASIC}} |
{{trans|Commodore BASIC}} |
||
{{works with|Nascom ROM BASIC|4.7}} |
{{works with|Nascom ROM BASIC|4.7}} |
||
< |
<syntaxhighlight lang="basic"> |
||
10 REM Trabb Pardo-Knuth algorithm |
10 REM Trabb Pardo-Knuth algorithm |
||
20 REM Used "magic numbers" because of strict |
20 REM Used "magic numbers" because of strict |
||
Line 2,427: | Line 2,427: | ||
240 NEXT I |
240 NEXT I |
||
250 END |
250 END |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,458: | Line 2,458: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="nim">import math, rdstdin, strutils, algorithm, sequtils |
||
proc f(x: float): float = x.abs.pow(0.5) + 5 * x.pow(3) |
proc f(x: float): float = x.abs.pow(0.5) + 5 * x.pow(3) |
||
Line 2,469: | Line 2,469: | ||
for x in s: |
for x in s: |
||
let result = f(x) |
let result = f(x) |
||
echo x, ": ", if result > 400: "TOO LARGE!" else: $result</ |
echo x, ": ", if result > 400: "TOO LARGE!" else: $result</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,489: | Line 2,489: | ||
{{works with|Mac OS X|10.6+}} |
{{works with|Mac OS X|10.6+}} |
||
< |
<syntaxhighlight lang="objc">// |
||
// TPKA.m |
// TPKA.m |
||
// RosettaCode |
// RosettaCode |
||
Line 2,530: | Line 2,530: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,561: | Line 2,561: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let f x = sqrt x +. 5.0 *. (x ** 3.0) |
||
let p x = x < 400.0 |
let p x = x < 400.0 |
||
Line 2,572: | Line 2,572: | ||
then Printf.printf "f(%g) = %g\n%!" x res |
then Printf.printf "f(%g) = %g\n%!" x res |
||
else Printf.eprintf "f(%g) :: Overflow\n%!" x |
else Printf.eprintf "f(%g) :: Overflow\n%!" x |
||
) (List.rev lst)</ |
) (List.rev lst)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,605: | Line 2,605: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">{ |
||
print("11 numbers: "); |
print("11 numbers: "); |
||
v=vector(11, n, eval(input())); |
v=vector(11, n, eval(input())); |
||
v=apply(x->x=sqrt(abs(x))+5*x^3;if(x>400,"overflow",x), v); |
v=apply(x->x=sqrt(abs(x))+5*x^3;if(x>400,"overflow",x), v); |
||
vector(11, i, v[12-i]) |
vector(11, i, v[12-i]) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>11 numbers: |
<pre>11 numbers: |
||
Line 2,629: | Line 2,629: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">print "Enter 11 numbers:\n"; |
||
for ( 1..11 ) { |
for ( 1..11 ) { |
||
$number = <STDIN>; |
$number = <STDIN>; |
||
Line 2,639: | Line 2,639: | ||
my $result = sqrt( abs($n) ) + 5 * $n**3; |
my $result = sqrt( abs($n) ) + 5 * $n**3; |
||
printf "f( %6.2f ) %s\n", $n, $result > 400 ? " too large!" : sprintf "= %6.2f", $result |
printf "f( %6.2f ) %s\n", $n, $result > 400 ? " too large!" : sprintf "= %6.2f", $result |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,667: | Line 2,667: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<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> |
<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> |
||
<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> |
<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> |
||
Line 2,693: | Line 2,693: | ||
<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: #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> |
<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}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 2,737: | Line 2,737: | ||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
< |
<syntaxhighlight lang="picat">import util. |
||
go => |
go => |
||
Line 2,745: | Line 2,745: | ||
nl. |
nl. |
||
f(T) = sqrt(abs(T)) + 5*T**3.</ |
f(T) = sqrt(abs(T)) + 5*T**3.</syntaxhighlight> |
||
Example run: |
Example run: |
||
Line 2,755: | Line 2,755: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de f (X) |
||
(+ (sqrt (abs X)) (* 5 X X X)) ) |
(+ (sqrt (abs X)) (* 5 X X X)) ) |
||
Line 2,764: | Line 2,764: | ||
(for X (reverse (make (do 11 (link (read))))) |
(for X (reverse (make (do 11 (link (read))))) |
||
(when (> (f X) 400) |
(when (> (f X) 400) |
||
(prinl "TOO LARGE") ) ) )</ |
(prinl "TOO LARGE") ) ) )</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="picolisp">Input 11 numbers: 1 2 3 4 5 6 7 8 9 10 11 |
||
f : 11 |
f : 11 |
||
f = 6658 |
f = 6658 |
||
Line 2,795: | Line 2,795: | ||
f = 41 |
f = 41 |
||
f : 1 |
f : 1 |
||
f = 6</ |
f = 6</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
Trabb: Procedure options (main); /* 11 November 2013 */ |
Trabb: Procedure options (main); /* 11 November 2013 */ |
||
Line 2,824: | Line 2,824: | ||
end Trabb; |
end Trabb; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,856: | Line 2,856: | ||
=={{header|PL/M}}== |
=={{header|PL/M}}== |
||
Assuming the existence of suitable external library routines. |
Assuming the existence of suitable external library routines. |
||
< |
<syntaxhighlight lang="plm">TPK: DO; |
||
/* external I/O and real mathematical routines */ |
/* external I/O and real mathematical routines */ |
||
WRITE$STRING: PROCEDURE( S ) EXTERNAL; DECLARE S POINTER; END; |
WRITE$STRING: PROCEDURE( S ) EXTERNAL; DECLARE S POINTER; END; |
||
Line 2,883: | Line 2,883: | ||
END MAIN; |
END MAIN; |
||
END TPK;</ |
END TPK;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,901: | Line 2,901: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function Get-Tpk |
function Get-Tpk |
||
{ |
{ |
||
Line 2,951: | Line 2,951: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
$tpk = 1..11 | Get-Tpk |
$tpk = 1..11 | Get-Tpk |
||
$tpk |
$tpk |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 2,973: | Line 2,973: | ||
</pre> |
</pre> |
||
Sort back to ascending order ignoring '''Overflow''' results: |
Sort back to ascending order ignoring '''Overflow''' results: |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
$tpk | where result -ne overflow | sort number |
$tpk | where result -ne overflow | sort number |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 2,987: | Line 2,987: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure.d f(x.d) |
||
ProcedureReturn Pow(Abs(x), 0.5) + 5 * x * x * x |
ProcedureReturn Pow(Abs(x), 0.5) + 5 * x * x * x |
||
EndProcedure |
EndProcedure |
||
Line 3,038: | Line 3,038: | ||
Print(#crlf$ + #crlf$ + "Press ENTER to exit"): Input() |
Print(#crlf$ + #crlf$ + "Press ENTER to exit"): Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,057: | Line 3,057: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Functional=== |
===Functional=== |
||
< |
<syntaxhighlight 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. |
Type "copyright", "credits" or "license()" for more information. |
||
>>> def f(x): return abs(x) ** 0.5 + 5 * x**3 |
>>> def f(x): return abs(x) ** 0.5 + 5 * x**3 |
||
Line 3,066: | Line 3,066: | ||
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: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 |
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 |
||
>>> </ |
>>> </syntaxhighlight> |
||
===Procedural=== |
===Procedural=== |
||
{{Works with|Python|3.10}} |
{{Works with|Python|3.10}} |
||
< |
<syntaxhighlight lang="python">import math |
||
def f(x): |
def f(x): |
||
Line 3,084: | Line 3,084: | ||
print(f'f({x}): overflow') |
print(f'f({x}): overflow') |
||
else: |
else: |
||
print(f'f({x}) = {result}')</ |
print(f'f({x}) = {result}')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Enter 11 numbers: |
<pre>Enter 11 numbers: |
||
Line 3,111: | Line 3,111: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">S <- scan(n=11) |
||
f <- function(x) sqrt(abs(x)) + 5*x^3 |
f <- function(x) sqrt(abs(x)) + 5*x^3 |
||
Line 3,121: | Line 3,121: | ||
else |
else |
||
print(res) |
print(res) |
||
}</ |
}</syntaxhighlight> |
||
{{out|Sample output}} |
{{out|Sample output}} |
||
Line 3,142: | Line 3,142: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
Line 3,155: | Line 3,155: | ||
(displayln "Overflow!") |
(displayln "Overflow!") |
||
(printf "f(~a) = ~a\n" x res))) |
(printf "f(~a) = ~a\n" x res))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,185: | Line 3,185: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line>my @nums = prompt("Please type 11 space-separated numbers: ").words |
||
until @nums == 11; |
until @nums == 11; |
||
for @nums.reverse -> $n { |
for @nums.reverse -> $n { |
||
my $r = $n.abs.sqrt + 5 * $n ** 3; |
my $r = $n.abs.sqrt + 5 * $n ** 3; |
||
say "$n\t{ $r > 400 ?? 'Urk!' !! $r }"; |
say "$n\t{ $r > 400 ?? 'Urk!' !! $r }"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Please type 11 space-separated numbers: 10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301 |
<pre>Please type 11 space-separated numbers: 10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301 |
||
Line 3,209: | Line 3,209: | ||
<br><br>It could be noted that almost half of this program is devoted to prompting, parsing and validating of the (input) numbers, |
<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. |
<br>not to mention some hefty code to support right-justified numbers such that they are aligned when displayed. |
||
< |
<syntaxhighlight 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*/ |
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.*/ |
parse arg N .; if N=='' | N=="," then N=11 /*Not specified? Then use the default.*/ |
||
Line 3,256: | Line 3,256: | ||
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ % 2 |
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 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</ |
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; return g</syntaxhighlight> |
||
{{out|output|text= when prompted, using the input of: <tt> 5 3.3 3 2e-1 1 0 -1 -222 -33 4.0004 +5 </tt>}} |
{{out|output|text= when prompted, using the input of: <tt> 5 3.3 3 2e-1 1 0 -1 -222 -33 4.0004 +5 </tt>}} |
||
<pre> |
<pre> |
||
Line 3,282: | Line 3,282: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Trabb Pardo–Knuth algorithm |
# Project : Trabb Pardo–Knuth algorithm |
||
Line 3,311: | Line 3,311: | ||
func f(n) |
func f(n) |
||
return sqrt(fabs(n)) + 5 * pow(n, 3) |
return sqrt(fabs(n)) + 5 * pow(n, 3) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 3,342: | Line 3,342: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">def f(x) x.abs ** 0.5 + 5 * x ** 3 end |
||
puts "Please enter 11 numbers:" |
puts "Please enter 11 numbers:" |
||
Line 3,351: | Line 3,351: | ||
res = f(n) |
res = f(n) |
||
puts res > 400 ? "Overflow!" : res |
puts res > 400 ? "Overflow!" : res |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,382: | Line 3,382: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust"> |
||
use std::io::{self, BufRead}; |
use std::io::{self, BufRead}; |
||
Line 3,413: | Line 3,413: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,443: | Line 3,443: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">object TPKa extends App { |
||
final val numbers = scala.collection.mutable.MutableList[Double]() |
final val numbers = scala.collection.mutable.MutableList[Double]() |
||
final val in = new java.util.Scanner(System.in) |
final val in = new java.util.Scanner(System.in) |
||
Line 3,468: | Line 3,468: | ||
private final val THRESHOLD = 400D |
private final val THRESHOLD = 400D |
||
private final val CAPACITY = 11 |
private final val CAPACITY = 11 |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="ruby">var nums; do { |
||
nums = Sys.readln("Please type 11 space-separated numbers: ").nums |
nums = Sys.readln("Please type 11 space-separated numbers: ").nums |
||
} while(nums.len != 11) |
} while(nums.len != 11) |
||
Line 3,479: | Line 3,479: | ||
var r = (n.abs.sqrt + (5 * n**3)); |
var r = (n.abs.sqrt + (5 * n**3)); |
||
say "#{n}\t#{ r > 400 ? 'Urk!' : r }"; |
say "#{n}\t#{ r > 400 ? 'Urk!' : r }"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,498: | Line 3,498: | ||
=={{header|Sinclair ZX81 BASIC}}== |
=={{header|Sinclair ZX81 BASIC}}== |
||
Works with the unexpanded (1k RAM) ZX81 |
Works with the unexpanded (1k RAM) ZX81 |
||
< |
<syntaxhighlight lang="basic"> 10 DIM A(11) |
||
20 PRINT "ENTER ELEVEN NUMBERS:" |
20 PRINT "ENTER ELEVEN NUMBERS:" |
||
30 FOR I=1 TO 11 |
30 FOR I=1 TO 11 |
||
Line 3,509: | Line 3,509: | ||
100 GOTO 120 |
100 GOTO 120 |
||
110 PRINT A(I),Y |
110 PRINT A(I),Y |
||
120 NEXT I</ |
120 NEXT I</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>ENTER ELEVEN NUMBERS: |
<pre>ENTER ELEVEN NUMBERS: |
||
Line 3,526: | Line 3,526: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
{{works with|Swift 2.0}} |
{{works with|Swift 2.0}} |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
print("Enter 11 numbers for the Trabb─Pardo─Knuth algorithm:") |
print("Enter 11 numbers for the Trabb─Pardo─Knuth algorithm:") |
||
Line 3,544: | Line 3,544: | ||
print("f(\($0))", result > 400.0 ? "OVERFLOW" : result, separator: "\t") |
print("f(\($0))", result > 400.0 ? "OVERFLOW" : result, separator: "\t") |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,573: | Line 3,573: | ||
=={{header|Symsyn}}== |
=={{header|Symsyn}}== |
||
< |
<syntaxhighlight lang="symsyn"> |
||
|Trabb Pardo–Knuth algorithm |
|Trabb Pardo–Knuth algorithm |
||
Line 3,611: | Line 3,611: | ||
+ y x |
+ y x |
||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl"># Helper procedures |
||
proc f {x} {expr {abs($x)**0.5 + 5*$x**3}} |
proc f {x} {expr {abs($x)**0.5 + 5*$x**3}} |
||
proc overflow {y} {expr {$y > 400}} |
proc overflow {y} {expr {$y > 400}} |
||
Line 3,633: | Line 3,633: | ||
puts "${x}: $result" |
puts "${x}: $result" |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out|Sample run}} |
{{out|Sample run}} |
||
<pre> |
<pre> |
||
Line 3,661: | Line 3,661: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Function tpk(s) |
Function tpk(s) |
||
arr = Split(s," ") |
arr = Split(s," ") |
||
Line 3,682: | Line 3,682: | ||
list = WScript.StdIn.ReadLine |
list = WScript.StdIn.ReadLine |
||
tpk(list) |
tpk(list) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 3,704: | Line 3,704: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "io" for Stdin, Stdout |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 3,732: | Line 3,732: | ||
Fmt.print(" f($6.3f) = overflow", item) |
Fmt.print(" f($6.3f) = overflow", item) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,766: | Line 3,766: | ||
=={{header|XBasic}}== |
=={{header|XBasic}}== |
||
{{works with|Windows XBasic}} |
{{works with|Windows XBasic}} |
||
< |
<syntaxhighlight lang="xbasic"> |
||
' Trabb Pardo-Knuth algorithm |
' Trabb Pardo-Knuth algorithm |
||
PROGRAM "tpkalgorithm" |
PROGRAM "tpkalgorithm" |
||
Line 3,809: | Line 3,809: | ||
END FUNCTION |
END FUNCTION |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,839: | Line 3,839: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; |
||
func real F(X); |
func real F(X); |
||
Line 3,856: | Line 3,856: | ||
else RlOut(0, Result); |
else RlOut(0, Result); |
||
CrLf(0)]; |
CrLf(0)]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,875: | Line 3,875: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn f(x) { x.abs().pow(0.5) + x.pow(3)*5 } |
||
reg ns; do{ |
reg ns; do{ |
||
ns=ask("11 numbers seperated by spaces: "); |
ns=ask("11 numbers seperated by spaces: "); |
||
Line 3,882: | Line 3,882: | ||
ns.reverse().apply(fcn(x){ |
ns.reverse().apply(fcn(x){ |
||
fx:=f(x); "f(%7.3f)-->%s".fmt(x, if(fx>400)"Overflow" else fx) }) |
fx:=f(x); "f(%7.3f)-->%s".fmt(x, if(fx>400)"Overflow" else fx) }) |
||
.pump(Console.println);</ |
.pump(Console.println);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |