Trabb Pardo–Knuth algorithm: Difference between revisions

m
(Dialects of BASIC moved to the BASIC section.)
m (→‎{{header|Wren}}: Minor tidy)
(13 intermediate revisions by 8 users not shown)
Line 398:
 
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
{{trans|Commodere BASIC}}
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">
100 REM Trabb Pardo-Knuth algorithm
110 REM Used "magic numbers" because of strict specification of the algorithm.
120 DEF FNF(N) = SQR(ABS(N)) + 5 * N * N * N
130 DIM S(0 TO 10)
140 PRINT "Enter 11 numbers."
150 FOR I = 0 TO 10
160 INPUT PROMPT STR$(I + 1) & ":": S(I)
170 NEXT I
180 PRINT
190 REM Reverse
200 FOR I = 0 TO INT(10 / 2)
210 LET Tmp = S(I)
220 LET S(I) = S(10 - I)
230 LET S(10 - I) = Tmp
240 NEXT I
250 REM Results
260 FOR I = 0 TO 10
270 LET R = FNF(S(I))
280 PRINT USING "f(####.###) = ": S(I);
290 IF R > 400 THEN
300 PRINT "overflow"
310 ELSE
320 PRINT USING "####.###": R
330 END IF
340 NEXT I
350 END
</syntaxhighlight>
{{out}}
<pre>
Enter 11 numbers.
1:-5
2:-3
3:-2
4:-1
5:0
6:1
7:2
8:3
9:4
10:5
11:6
 
f( 6.000) = overflow
f( 5.000) = overflow
f( 4.000) = 322.000
f( 3.000) = 136.732
f( 2.000) = 41.414
f( 1.000) = 6.000
f( .000) = .000
f( -1.000) = -4.000
f( -2.000) = -38.586
f( -3.000) = -133.268
f( -5.000) = -622.764
</pre>
 
==={{header|Applesoft BASIC}}===
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
<syntaxhighlight lang="qbasic">10 REM Trabb Pardo-Knuth algorithm
20 HOME : REM 20 CLS for Chipmunk Basic or GW-BASIC
30 DIM S(10)
40 PRINT "Enter 11 numbers."
50 FOR I = 0 TO 10
60 PRINT I+1;
70 INPUT "=> ";S(I)
80 NEXT I
90 PRINT
100 REM Results
110 FOR I = 10 TO 0 STEP -1
120 PRINT "f( " S(I)") = ";
130 F = S(I)
140 R = SQR(ABS(F))+5*F^3
150 IF R > 400 THEN PRINT "-=< overflow >=-"
160 IF R <= 400 THEN PRINT R
170 NEXT I
180 END</syntaxhighlight>
 
==={{header|ASIC}}===
{{works with|ASIC|5.0}} Compile with the option ''Decimal Math'' (<code>DEC</code> in command line).
Line 559 ⟶ 640:
f(-3)=-133.2679492
f(-4)=-318</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|BASIC256}}
<syntaxhighlight lang="qbasic">10 rem Trabb Pardo-Knuth algorithm
20 cls
30 dim s(10)
40 print "Enter 11 numbers."
50 for i = 0 to 10
60 print i+1;
70 input "=> ";s(i)
80 next i
90 print
160 'Results
170 for i = 10 to 0 step -1
180 print "f( " s(i)") = ";
190 r = f(s(i))
200 if r > 400 then
210 print "-=< overflow >=-"
220 else
230 print r
240 endif
250 next i
260 end
270 function f(n)
280 f = sqr(abs(n))+5*n^3
290 return
</syntaxhighlight>
{{out}}
<pre>Same as BASIC256 entry.</pre>
 
==={{header|Commodore BASIC}}===
Line 1,031 ⟶ 1,142:
LOOP UNTIL i < 1
END</syntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="vb">dim s(10)
print "Enter 11 numbers."
for i = 0 to 10
print i +1;
input " => "; s(i)
next i
print
'Results
for i = 10 to 0 step -1
print "f("; s(i); ") = ";
r = f(s(i))
if r > 400 then
print "-=< overflow >=-"
else
print r
end if
next i
end
 
function f(n)
f = sqr(abs(n)) + 5 * n * n * n
end function</syntaxhighlight>
{{out}}
<pre>Same as Liberty BASIC entry.</pre>
 
==={{header|Sinclair ZX81 BASIC}}===
Line 1,404 ⟶ 1,543:
f(2.000) = 41.4142
f(1.000) = 6</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
procedure DoPardoKnuth(Memo: TMemo; A: array of double);
var Y: double;
var I: integer;
var S: string;
 
function Func(T: double): double;
begin
Result:=Sqrt(abs(T))+ 5*T*T*T;
end;
 
begin
for I:=0 to High(A) do
begin
Y:=Func(A[I]);
if y > 400 then S:='Too Large'
else S:=Format('%f %f',[A[I],Y]);
Memo.Lines.Add(S);
end;
end;
 
procedure ShowPardoKnuth(Memo: TMemo);
begin
DoPardoKnuth(Memo, [1,2,3,4,5,6,7,8,9,10,11]);
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
1.00 6.00
2.00 41.41
3.00 136.73
4.00 322.00
Too Large
Too Large
Too Large
Too Large
Too Large
Too Large
Too Large
Elapsed Time: 12.547 ms.
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
print "Please enter 11 numbers :"
n[] = number strsplit input " "
print ""
print "Evaluating f(x) = |x|^0.5 + 5x^3 for the given inputs :"
for i = len n[] downto 1
r = sqrt abs n[i] + 5 * pow n[i] 3
write "f(" & n[i] & ") = "
if r > 400
print "Overflow!"
else
print r
.
.
# without this section, the input is interactive
input_data
10 -1 1 2 3 4 4.3 4.31 4.32 4.32 4.29
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,506 ⟶ 1,716:
=={{header|Elena}}==
{{trans|C}}
ELENA 56.0x :
<syntaxhighlight lang="elena">import extensions;
import extensions'math;
Line 1,514 ⟶ 1,724:
real[] inputs := new real[](11);
console.printLine("Please enter 11 numbers :");
for(int i := 0,; i < 11,; i += 1)
{
inputs[i] := console.readLine().toReal()
Line 1,520 ⟶ 1,730:
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);
Line 2,735 ⟶ 2,945:
f[1.18367]= 9.38004
f[0.470145]= 1.20527
</pre>
 
=={{header|MATLAB}}==
{{trans|Julia}}
<syntaxhighlight lang="MATLAB}}">
clear all;close all;clc;
 
% Define the function f(x)
f = @(x) sqrt(abs(x)) + 5 * x^3;
 
% Read a line of input, split it into elements, convert to numbers
inputLine = input('', 's');
numbers = str2double(strsplit(inputLine));
 
% Process each number in reverse order
for i = length(numbers):-1:1
value = f(numbers(i));
if value > 400
fprintf('%g: TOO LARGE\n', numbers(i));
else
fprintf('%g: %g\n', numbers(i), value);
end
end
</syntaxhighlight>
{{out}}
<pre>
1 2 3 4 5 6 7 8 9 10 11
11: TOO LARGE
10: TOO LARGE
9: TOO LARGE
8: TOO LARGE
7: TOO LARGE
6: TOO LARGE
5: TOO LARGE
4: 322
3: 136.732
2: 41.4142
1: 6
</pre>
 
Line 2,769 ⟶ 3,017:
41.41421356237309
6.0
</pre>
 
=={{header|Modula-2}}==
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}}
<syntaxhighlight lang="modula2">
MODULE TrabbPardoKnuth;
 
FROM STextIO IMPORT
SkipLine, WriteString, WriteLn;
FROM SRealIO IMPORT
ReadReal, WriteFixed;
FROM SWholeIO IMPORT
WriteInt;
FROM RealMath IMPORT
sqrt;
 
CONST
Size = 11;
 
TYPE
TSequence = ARRAY [1 .. Size] OF REAL;
 
VAR
S: TSequence;
I: CARDINAL;
Result: REAL;
 
PROCEDURE ReverseSequence(VAR S: TSequence);
VAR
I: CARDINAL;
Temp: REAL;
BEGIN
FOR I := 1 TO Size DIV 2 DO
Temp := S[I];
S[I] := S[Size - I + 1];
S[Size - I + 1] := Temp;
END;
END ReverseSequence;
 
PROCEDURE DoOperation(Item: REAL): REAL;
BEGIN
RETURN sqrt(ABS(Item)) + 5.0 * Item * Item * Item;
END DoOperation;
 
BEGIN
WriteString("Enter 11 numbers.");
WriteLn;
FOR I := 1 TO Size DO
WriteInt(I, 2);
WriteString(": ");
ReadReal(S[I]);
SkipLine
END;
ReverseSequence(S);
WriteLn;
FOR I := 1 TO Size DO
Result := DoOperation(S[I]);
WriteString("f(");
WriteFixed(S[I], 3, 8);
WriteString(") = ");
IF Result > 400.0 THEN
WriteString("overflow");
ELSE
WriteFixed(Result, 3, 8)
END;
WriteLn;
END;
END TrabbPardoKnuth.
</syntaxhighlight>
{{out}}
<pre>
Enter 11 numbers.
1: -5
2: -3
3: -2
4: -1
5: 0
6: 1
7: 2
8: 3
9: 4
10: 5
11: 6
 
f( 6.000) = overflow
f( 5.000) = overflow
f( 4.000) = 322.000
f( 3.000) = 136.732
f( 2.000) = 41.414
f( 1.000) = 6.000
f( 0.000) = 0.000
f( -1.000) = -4.000
f( -2.000) = -38.586
f( -3.000) = -133.268
f( -5.000) = -622.764
</pre>
 
Line 3,414 ⟶ 3,757:
f(532.0): overflow
f(1.0) = 6.0</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ $ "bigrat.qky" loadfile ] now!
 
[ $->v drop
2dup vabs 10 vsqrt drop
2swap 2dup 2dup
v* v* 5 n->v v* v+ ] is function ( $ --> n/d )
 
[ $ "Please enter 11 numbers: "
input nest$
reverse
witheach
[ function
400 n->v 2over v< iff
[ 2drop say "overflow" ]
else
[ 7 point$ echo$ ]
sp ]
cr ] is task ( --> )</syntaxhighlight>
 
{{out}}
 
As a dialogue in the Quackery shell.
 
<pre>/O> task
...
Please enter 11 numbers: 10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301
399.8862997 overflow overflow overflow 399.6086441 322 136.7320508 41.4142136 6 -4 overflow
</pre>
 
=={{header|R}}==
Line 3,643 ⟶ 4,017:
f(-5) = -622.764
 
</pre>
 
=={{header|RPL}}==
Idiomatic RPL is based on use of stack whenever possible, short code and minimalist UX.
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ DUP ABS √ SWAP 3 ^ + ≫ ''''FUNC'''' STO
≪ "Push 11 numbers in stack, then CONT"
HALT 11 →LIST → s
≪ DROP 11 1
'''FOR''' j s j GET
'''FUNC'''
DUP 400 >
"Too large!"
ROT '''IFTE'''
-1 '''STEP'''
≫ ≫ ''''TPK'''' STO
|
'''FUNC''' ''( x -- sqrt(abs(x))+x^3 )''
'''TPK''' ''( -- report )''
ask for 11 numbers to be read into a sequence S
reverse sequence S
for each item in sequence S
call a function to do an operation
if result overflows
alert user
else print result
|}
<code>CONT</code> is not an instruction but a command, triggered by a specific keystroke.
On RPL versions used by HP-48 calculators and beyond, the first 2 lines of TPK can be replaced by
≪ { }
1 11 '''START''' "Enter a number" { "?" V } INPUT + '''NEXT''' → s
to slightly improve the collection of the 11 numbers, avoiding the need for the <code>CONT</code> command.
{{in}}
<pre>
TPK 100 3 0 1 0 1 0 1 0 1 10
</pre>
{{out}}
<pre>
11: "Too large!"
10: "28.7320508076"
9: "0"
8: "2"
7: "0"
6: "2"
5: "0"
4: "2"
3: "0"
2: "2"
1: "Too large!"
</pre>
 
Line 3,939 ⟶ 4,371:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "io" for Stdin, Stdout
import "./fmt" for Fmt
 
var f = Fn.new { |x| x.abs.sqrt + 5*x*x*x }
9,485

edits