Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
27
Task/Nth-root/Ada/nth-root.adb
Normal file
27
Task/Nth-root/Ada/nth-root.adb
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Test_Nth_Root is
|
||||
generic
|
||||
type Real is digits <>;
|
||||
function Nth_Root (Value : Real; N : Positive) return Real;
|
||||
|
||||
function Nth_Root (Value : Real; N : Positive) return Real is
|
||||
type Index is mod 2;
|
||||
X : array (Index) of Real := (Value, Value);
|
||||
K : Index := 0;
|
||||
begin
|
||||
loop
|
||||
X (K + 1) := ( (Real (N) - 1.0) * X (K) + Value / X (K) ** (N-1) ) / Real (N);
|
||||
exit when X (K + 1) >= X (K);
|
||||
K := K + 1;
|
||||
end loop;
|
||||
return X (K + 1);
|
||||
end Nth_Root;
|
||||
|
||||
function Long_Nth_Root is new Nth_Root (Long_Float);
|
||||
begin
|
||||
Put_Line ("1024.0 10th =" & Long_Float'Image (Long_Nth_Root (1024.0, 10)));
|
||||
Put_Line (" 27.0 3rd =" & Long_Float'Image (Long_Nth_Root (27.0, 3)));
|
||||
Put_Line (" 2.0 2nd =" & Long_Float'Image (Long_Nth_Root (2.0, 2)));
|
||||
Put_Line ("5642.0 125th =" & Long_Float'Image (Long_Nth_Root (5642.0, 125)));
|
||||
end Test_Nth_Root;
|
||||
26
Task/Nth-root/AutoIt/nth-root.au3
Normal file
26
Task/Nth-root/AutoIt/nth-root.au3
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
;AutoIt Version: 3.2.10.0
|
||||
$A=4913
|
||||
$n=3
|
||||
$x=20
|
||||
ConsoleWrite ($n& " root of "& $A & " is " &nth_root_it($A,$n,$x))
|
||||
ConsoleWrite ($n& " root of "& $A & " is " &nth_root_rec($A,$n,$x))
|
||||
|
||||
;Iterative
|
||||
Func nth_root_it($A,$n,$x)
|
||||
$x0="0"
|
||||
While StringCompare(string($x0),string($x))
|
||||
ConsoleWrite ($x&@CRLF)
|
||||
$x0=$x
|
||||
$x=((($n-1)*$x)+($A/$x^($n-1)))/$n
|
||||
WEnd
|
||||
Return $x
|
||||
EndFunc
|
||||
|
||||
;Recursive
|
||||
Func nth_root_rec($A,$n,$x)
|
||||
ConsoleWrite ($x&@CRLF)
|
||||
If $x==((($n-1)*$x)+($A/$x^($n-1)))/$n Then
|
||||
Return $x
|
||||
EndIf
|
||||
Return nth_root_rec($A,$n,((($n-1)*$x)+($A/$x^($n-1)))/$n)
|
||||
EndFunc
|
||||
|
|
@ -1,13 +1,17 @@
|
|||
PROCEDURE nth
|
||||
PARAM N : INTEGER; A, P : REAL
|
||||
DIM N : INTEGER; A, P : REAL
|
||||
DIM TEMP0, TEMP1 : REAL
|
||||
A = 144.0
|
||||
P = 0.00001
|
||||
PRINT "Base: "; A
|
||||
PRINT "Number Root"
|
||||
FOR N = 1 TO 10
|
||||
TEMP0 := A
|
||||
TEMP1 := A/N
|
||||
WHILE ( abs(TEMP0 - TEMP1) > P) DO
|
||||
TEMP0 := TEMP1
|
||||
TEMP1 := (( N - 1.0) * TEMP1 + A / TEMP1 ^ (N - 1.0)) / N
|
||||
ENDWHILE
|
||||
PRINT "Root Number Precision"
|
||||
PRINT N, A, P
|
||||
PRINT "The Root is: ";TEMP1
|
||||
PRINT N, TEMP1
|
||||
NEXT N
|
||||
END
|
||||
|
|
|
|||
100
Task/Nth-root/COBOL/nth-root.cob
Normal file
100
Task/Nth-root/COBOL/nth-root.cob
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. Nth-Root.
|
||||
AUTHOR. Bill Gunshannon.
|
||||
INSTALLATION. Home Office.
|
||||
DATE-WRITTEN. 26 Mar 2026.
|
||||
************************************************************
|
||||
** Program Abstract:
|
||||
** Compute the Nth Root of a positive real number.
|
||||
**
|
||||
** Takes initial value from console.
|
||||
**
|
||||
** Prints first ten roots.
|
||||
**
|
||||
** Enter 0 for first value to terminate program.
|
||||
************************************************************
|
||||
|
||||
ENVIRONMENT DIVISION.
|
||||
|
||||
|
||||
DATA DIVISION.
|
||||
|
||||
WORKING-STORAGE SECTION.
|
||||
|
||||
01 Num PIC 9(5).
|
||||
01 Loop PIC 9(5).
|
||||
01 Base PIC 9(5)V9(5).
|
||||
01 Z PIC 9(5)V9(5).
|
||||
01 Precision PIC 9V9(9).
|
||||
|
||||
01 TEMP0 PIC 9(9)V9(9).
|
||||
01 TEMP1 PIC 9(9)V9(9).
|
||||
01 RESULTS.
|
||||
05 Field1 PIC ZZZZ9.
|
||||
05 FILLER VALUE SPACES PIC X(8).
|
||||
05 Field2 PIC ZZZZ.ZZZZZZZZZ.
|
||||
|
||||
01 HEADER.
|
||||
05 FILLER PIC X(72)
|
||||
VALUE " Number Root".
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
|
||||
Main-Program.
|
||||
PERFORM FOREVER
|
||||
|
||||
MOVE 0.000001 to Precision
|
||||
PERFORM Get-Input
|
||||
|
||||
DISPLAY HEADER
|
||||
PERFORM VARYING Loop FROM 1 BY 1 UNTIL Loop = 10
|
||||
MOVE Loop TO Num
|
||||
PERFORM Compute-Root
|
||||
|
||||
MOVE Num TO Field1
|
||||
MOVE TEMP1 TO Field2
|
||||
DISPLAY RESULTS
|
||||
END-PERFORM
|
||||
END-PERFORM.
|
||||
|
||||
Get-Input.
|
||||
DISPLAY "Input Base Number: " WITH NO ADVANCING
|
||||
ACCEPT Base
|
||||
IF Base EQUALS ZERO
|
||||
THEN
|
||||
DISPLAY "Good Bye."
|
||||
STOP RUN
|
||||
END-IF.
|
||||
|
||||
Compute-Root.
|
||||
|
||||
MOVE Base TO TEMP0
|
||||
DIVIDE Base BY Num GIVING TEMP1
|
||||
|
||||
PERFORM WITH TEST AFTER UNTIL FUNCTION ABS(TEMP1 - TEMP0 )
|
||||
< Precision
|
||||
MOVE TEMP1 TO TEMP0
|
||||
COMPUTE TEMP1 = (( Num - 1.0) * TEMP1 + Base /
|
||||
TEMP1 ** (Num - 1.0)) / Num
|
||||
END-PERFORM.
|
||||
|
||||
END-PROGRAM.
|
||||
|
||||
<pre>
|
||||
Output:
|
||||
|
||||
Input Base Number: 144
|
||||
Number Root
|
||||
1 144.000000000
|
||||
2 12.000000000
|
||||
3 5.241482788
|
||||
4 3.464101615
|
||||
5 2.701920077
|
||||
6 2.289428485
|
||||
7 2.033937009
|
||||
8 1.861209718
|
||||
9 1.737072938
|
||||
Input Base Number: 0
|
||||
Good Bye.
|
||||
|
||||
</pre>
|
||||
|
|
@ -1,29 +1,27 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">pow_</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: #004080;">integer</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">e</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">x</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">r</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
with javascript_semantics
|
||||
function pow_(atom x, integer e)
|
||||
atom r = 1
|
||||
for i=1 to e do
|
||||
r *= x
|
||||
end for
|
||||
return r
|
||||
end function
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">nth_root</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">eps</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1e-15</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- relative accuracy</span>
|
||||
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000080;font-style:italic;">-- atom d = ( y / power(x,n-1) - x ) / n</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">pow_</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">)</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">n</span>
|
||||
<span style="color: #000000;">x</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">d</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">eps</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x</span> <span style="color: #000080;font-style:italic;">-- absolute accuracy </span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">></span> <span style="color: #0000FF;">-</span><span style="color: #000000;">e</span> <span style="color: #008080;">and</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">e</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">x</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
function nth_root(atom y, n)
|
||||
atom eps = 1e-15, -- relative accuracy
|
||||
x = 1
|
||||
while 1 do
|
||||
-- atom d = ( y / power(x,n-1) - x ) / n
|
||||
atom d = ( y / pow_(x,n-1) - x ) / n
|
||||
x += d
|
||||
atom e = eps*x -- absolute accuracy
|
||||
if d > -e and d < e then exit end if
|
||||
end while
|
||||
return x
|
||||
end function
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">yn</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">yn</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"nth_root(%d,%d) = %.10g, builtin = %.10g\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nth_root</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">1024</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">27</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">5642</span><span style="color: #0000FF;">,</span><span style="color: #000000;">125</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">4913</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">16</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">16</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">125</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1000000000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1000000000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">test</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
procedure test(sequence yn)
|
||||
atom {y,n} = yn
|
||||
printf(1,"nth_root(%d,%d) = %.10g, builtin = %.10g\n",{y,n,nth_root(y,n),power(y,1/n)})
|
||||
end procedure
|
||||
papply({{1024,10},{27,3},{2,2},{5642,125},{4913,3},{8,3},{16,2},{16,4},{125,3},{1000000000,3},{1000000000,9}},test)
|
||||
|
|
|
|||
39
Task/Nth-root/PowerShell/nth-root.ps1
Normal file
39
Task/Nth-root/PowerShell/nth-root.ps1
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#NoTeS: This sample code does not validate inputs
|
||||
# Thus, if there are errors the 'scary' red-text
|
||||
# error messages will appear.
|
||||
#
|
||||
# This code will not work properly in floating point values of n,
|
||||
# and negative values of A.
|
||||
#
|
||||
# Supports negative values of n by reciprocating the root.
|
||||
|
||||
$epsilon=1E-10 #Sample Epsilon (Precision)
|
||||
|
||||
function power($x,$e){ #As I said in the comment
|
||||
$ret=1
|
||||
for($i=1;$i -le $e;$i++){
|
||||
$ret*=$x
|
||||
}
|
||||
return $ret
|
||||
}
|
||||
function root($y,$n){ #The main Function
|
||||
if (0+$n -lt 0){$tmp=-$n} else {$tmp=$n} #This checks if n is negative.
|
||||
$ans=1
|
||||
|
||||
do{
|
||||
$d = ($y/(power $ans ($tmp-1)) - $ans)/$tmp
|
||||
$ans+=$d
|
||||
} while ($d -lt -$epsilon -or $d -gt $epsilon)
|
||||
|
||||
if (0+$n -lt 0){return 1/$ans} else {return $ans}
|
||||
}
|
||||
|
||||
#Sample Inputs
|
||||
root 625 2
|
||||
root 2401 4
|
||||
root 2 -2
|
||||
root 1.23456789E-20 34
|
||||
root 9.87654321E20 10 #Quite slow here, I admit...
|
||||
|
||||
((root 5 2)+1)/2 #Extra: Computes the golden ratio
|
||||
((root 5 2)-1)/2
|
||||
|
|
@ -1,35 +1,29 @@
|
|||
program nth
|
||||
#
|
||||
integer root
|
||||
|
||||
integer n
|
||||
real number, precision
|
||||
real temp0, temp1
|
||||
|
||||
1 format('Enter the base number: ')
|
||||
2 format('Enter the desired root: ')
|
||||
3 format('Enter the desired precision: ')
|
||||
4 format(F12.6)
|
||||
5 format(I6)
|
||||
write(6,1)
|
||||
read(5,4)number
|
||||
write(6,2)
|
||||
read(5,5)root
|
||||
write(6,3)
|
||||
read(5,4)precision
|
||||
6 format(' number root')
|
||||
7 format(i6,f12.6)
|
||||
precision=0.00001
|
||||
root=1.0
|
||||
number=144.0
|
||||
n=10
|
||||
|
||||
write(6,6)
|
||||
|
||||
for (i=1;i<=n;i=i+1) {
|
||||
|
||||
temp0 = number
|
||||
temp1 = number/root
|
||||
temp1 = number/i
|
||||
|
||||
while ( abs(temp0 - temp1) > precision )
|
||||
while ( abs(temp1 - temp0) > precision )
|
||||
{
|
||||
temp0 = temp1
|
||||
temp1 = ((root - 1.0) * temp1 + number / temp1 ** (root - 1.0)) / root
|
||||
temp1 = ((i - 1.0) * temp1 + number / temp1 ** (i - 1.0)) / i
|
||||
}
|
||||
|
||||
6 format(' number root precision')
|
||||
write(6,6)
|
||||
7 format(f12.6,i6,f12.6)
|
||||
write (6,7)number,root,precision
|
||||
8 format('The root is: ',F12.6)
|
||||
write (6,8)temp1
|
||||
|
||||
write (6,7)i,temp1
|
||||
}
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue