Trabb Pardo–Knuth algorithm: Difference between revisions

m
(add RPL)
m (→‎{{header|Wren}}: Minor tidy)
 
(10 intermediate revisions by 6 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}}
Line 1,484 ⟶ 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,586 ⟶ 1,716:
=={{header|Elena}}==
{{trans|C}}
ELENA 56.0x :
<syntaxhighlight lang="elena">import extensions;
import extensions'math;
Line 1,594 ⟶ 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,600 ⟶ 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,815 ⟶ 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,849 ⟶ 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,757 ⟶ 4,020:
 
=={{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"
Line 3,765 ⟶ 4,029:
≪ DUP ABS √ SWAP 3 ^ + ≫ ''''FUNC'''' STO
≪ "EnterPush number11 +numbers CONT"in {stack, }then CONT"
1HALT 11 '''START''' HALT + '''NEXT'''→LIST → s
≪ DROP 11 1
'''FOR''' j s j GET
'''FUNC'''
IF DUP 400 > THEN
DROP "Too large!"
ROT '''ELSEIFTE'''
-1 '''STEP'''
"F(" s j GET →STR + ")=" +
≫ ≫ ''''TPK'''' STO
SWAP →STR +
'''END'''
-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
Line 3,787 ⟶ 4,048:
call a function to do an operation
if result overflows
alert user
else print result
print result
|}
<code>CONT</code> is not an instruction but a command, triggered by a specific keystroke.
On HP-48 and beyond, the first 2 lines of TPK can be replaced by
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>
Line 3,805 ⟶ 4,065:
<pre>
11: "Too large!"
10: "F(3)=28.7320508076"
9: "F(0)=0"
8: "F(1)=2"
7: "F(0)=0"
6: "F(1)=2"
5: "F(0)=0"
4: "F(1)=2"
3: "F(0)=0"
2: "F(1)=2"
1: "Too large!"
</pre>
Line 4,111 ⟶ 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,482

edits