Jump to content

Trabb Pardo–Knuth algorithm: Difference between revisions

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

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.