Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Nth-root/00-META.yaml
Normal file
5
Task/Nth-root/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Simple
|
||||
from: http://rosettacode.org/wiki/Nth_root
|
||||
note: Classic CS problems and programs
|
||||
5
Task/Nth-root/00-TASK.txt
Normal file
5
Task/Nth-root/00-TASK.txt
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
;Task:
|
||||
Implement the algorithm to compute the principal [[wp:Nth root|<big>''n''<sup>th</sup></big> root]] <big><big><big><math>\sqrt[n]A</math></big></big></big> of a positive real number <big>''A''</big>, as explained at the [[wp:Nth root algorithm|Wikipedia page]].
|
||||
|
||||
<br><br>
|
||||
|
||||
11
Task/Nth-root/11l/nth-root.11l
Normal file
11
Task/Nth-root/11l/nth-root.11l
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
F nthroot(a, n)
|
||||
V result = a
|
||||
V x = a / n
|
||||
L abs(result - x) > 10e-15
|
||||
x = result
|
||||
result = (1.0 / n) * (((n - 1) * x) + (a / pow(x, n - 1)))
|
||||
R result
|
||||
|
||||
print(nthroot(34.0, 5))
|
||||
print(nthroot(42.0, 10))
|
||||
print(nthroot(5.0, 2))
|
||||
59
Task/Nth-root/360-Assembly/nth-root.360
Normal file
59
Task/Nth-root/360-Assembly/nth-root.360
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
* Nth root - x**(1/n) - 29/07/2018
|
||||
NTHROOT CSECT
|
||||
USING NTHROOT,R13 base register
|
||||
B 72(R15) skip savearea
|
||||
DC 17F'0' savearea
|
||||
SAVE (14,12) save previous context
|
||||
ST R13,4(R15) link backward
|
||||
ST R15,8(R13) link forward
|
||||
LR R13,R15 set addressability
|
||||
BAL R14,ROOTN call rootn(x,n)
|
||||
LE F0,XN xn=rootn(x,n)
|
||||
LA R0,6 decimals=6
|
||||
BAL R14,FORMATF edit xn
|
||||
MVC PG(13),0(R1) output xn
|
||||
XPRNT PG,L'PG print buffer
|
||||
L R13,4(0,R13) restore previous savearea pointer
|
||||
RETURN (14,12),RC=0 restore registers from calling sav
|
||||
ROOTN MVC ZN,=E'0' zn=0 ----------------------------
|
||||
MVC ZN,N n
|
||||
MVI ZN,X'46' zn=unnormalize(n)
|
||||
LE F0,ZN zn
|
||||
AE F0,=E'0' normalized
|
||||
STE F0,ZN zn=normalize(n)
|
||||
LE F6,=E'0' xm=0
|
||||
LE F0,X x
|
||||
DE F0,ZN /zn
|
||||
STE F0,XN xn=x/zn
|
||||
WHILEA LE F0,XN xn
|
||||
SER F0,F6 xn-xm
|
||||
LPER F0,F0 abs((xn-xm)
|
||||
DE F0,XN /xn
|
||||
CE F0,EPSILON while abs((xn-xm)/xn)>epsilon
|
||||
BNH EWHILEA ~
|
||||
LE F6,XN xm=xn
|
||||
LE F0,ZN zn
|
||||
SE F0,=E'1' zn-1
|
||||
MER F0,F6 f0=(zn-1)*xm
|
||||
L R2,N n
|
||||
BCTR R2,0 n-1
|
||||
LE F2,=E'1' xm
|
||||
POW MER F2,F6 *xm
|
||||
BCT R2,POW f2=xm**(n-1)
|
||||
LE F4,X x
|
||||
DER F4,F2 x/xm**(n-1)
|
||||
AER F0,F4 (zn-1)*xm+x/xm**(n-1)
|
||||
DE F0,ZN /zn
|
||||
STE F0,XN xn=((zn-1)*xm+x/xm**(n-1))/zn
|
||||
B WHILEA endwhile
|
||||
EWHILEA LE F0,XN xn
|
||||
BR R14 return ---------------------------
|
||||
COPY FORMATF format a float
|
||||
X DC E'2' x <== input
|
||||
N DC F'2' n <== input
|
||||
EPSILON DC E'1E-6' imprecision
|
||||
XN DS E xn :: output
|
||||
ZN DS E zn=float(n)
|
||||
PG DC CL80' ' buffer
|
||||
REGEQU
|
||||
END NTHROOT
|
||||
109
Task/Nth-root/AArch64-Assembly/nth-root.aarch64
Normal file
109
Task/Nth-root/AArch64-Assembly/nth-root.aarch64
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
/* ARM assembly AARCH64 Raspberry PI 3B */
|
||||
/* program nroot64.s */
|
||||
/* link with gcc. Use C function for display float */
|
||||
|
||||
/*******************************************/
|
||||
/* Constantes file */
|
||||
/*******************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly*/
|
||||
.include "../includeConstantesARM64.inc"
|
||||
|
||||
/*******************************************/
|
||||
/* Initialized data */
|
||||
/*******************************************/
|
||||
.data
|
||||
szFormat1: .asciz "Root= %+09.15f\n"
|
||||
.align 4
|
||||
qNumberA: .quad 1024
|
||||
/*******************************************/
|
||||
/* UnInitialized data */
|
||||
/*******************************************/
|
||||
.bss
|
||||
.align 4
|
||||
/*******************************************/
|
||||
/* code section */
|
||||
/*******************************************/
|
||||
.text
|
||||
.global main
|
||||
main: // entry of program
|
||||
|
||||
/* root 10ieme de 1024 */
|
||||
ldr x0,qAdriNumberA // number address
|
||||
ldr d0,[x0] // load number in registre d0
|
||||
scvtf d0,d0 // conversion in float
|
||||
mov x0,#10 // N
|
||||
bl nthRoot
|
||||
ldr x0,qAdrszFormat1 // format
|
||||
bl printf // call C function !!!
|
||||
// Attention register dn lost !!!
|
||||
/* square root of 2 */
|
||||
fmov d0,2 // conversion 2 in float register d0
|
||||
mov x0,#2 // N
|
||||
bl nthRoot
|
||||
ldr x0,qAdrszFormat1 // format
|
||||
// d0 contains résult
|
||||
bl printf // call C function !!!
|
||||
|
||||
100: // standard end of the program
|
||||
mov x0, #0 // return code
|
||||
mov x8, #EXIT // request to exit program
|
||||
svc 0 // perform the system call
|
||||
|
||||
qAdrszFormat1: .quad szFormat1
|
||||
qAdriNumberA: .quad qNumberA
|
||||
|
||||
/******************************************************************/
|
||||
/* compute nth root */
|
||||
/******************************************************************/
|
||||
/* x0 contains N */
|
||||
/* d0 contains the value */
|
||||
/* x0 return result */
|
||||
nthRoot:
|
||||
stp x1,lr,[sp,-16]! // save registers
|
||||
stp x2,x3,[sp,-16]! // save registers
|
||||
stp d1,d2,[sp,-16]! // save float registers
|
||||
stp d3,d4,[sp,-16]! // save float registers
|
||||
stp d5,d6,[sp,-16]! // save float registers
|
||||
stp d7,d8,[sp,-16]! // save float registers
|
||||
fmov d6,x0 //
|
||||
scvtf d6,d6 // N conversion in float double précision (64 bits)
|
||||
sub x1,x0,#1 // N - 1
|
||||
fmov d8,x1 //
|
||||
scvtf d4,d8 //conversion in float double précision
|
||||
fmov d2,d0 // a = A
|
||||
fdiv d3,d0,d6 // b = A/n
|
||||
adr x2,dfPrec // load précision
|
||||
ldr d8,[x2]
|
||||
1: // begin loop
|
||||
fmov d2,d3 // a <- b
|
||||
fmul d5,d3,d4 // (N-1)*b
|
||||
|
||||
fmov d1,1 // constante 1 -> float
|
||||
mov x2,0 // loop indice
|
||||
2: // compute pow (n-1)
|
||||
fmul d1,d1,d3 //
|
||||
add x2,x2,1
|
||||
cmp x2,x1 // n -1 ?
|
||||
blt 2b // no -> loop
|
||||
fdiv d7,d0,d1 // A / b pow (n-1)
|
||||
fadd d7,d7,d5 // + (N-1)*b
|
||||
fdiv d3,d7,d6 // / N -> new b
|
||||
fsub d1,d3,d2 // compute gap
|
||||
fabs d1,d1 // absolute value
|
||||
fcmp d1,d8 // compare float maj FPSCR
|
||||
bgt 1b // if gap > précision -> loop
|
||||
fmov d0,d3 // end return result in d0
|
||||
100:
|
||||
ldp d7,d8,[sp],16 // restaur 2 float registers
|
||||
ldp d5,d6,[sp],16 // restaur 2 float registers
|
||||
ldp d3,d4,[sp],16 // restaur 2 float registers
|
||||
ldp d1,d2,[sp],16 // restaur 2 float registers
|
||||
ldp x2,x3,[sp],16 // restaur 2 registers
|
||||
ldp x1,lr,[sp],16 // restaur 2 registers
|
||||
ret // return to address lr x30
|
||||
dfPrec: .double 0f1E-10 // précision
|
||||
/********************************************************/
|
||||
/* File Include fonctions */
|
||||
/********************************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly */
|
||||
.include "../includeARM64.inc"
|
||||
25
Task/Nth-root/ALGOL-68/nth-root.alg
Normal file
25
Task/Nth-root/ALGOL-68/nth-root.alg
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
REAL default p = 0.001;
|
||||
|
||||
PROC nth root = (INT n, LONG REAL a, p)LONG REAL:
|
||||
(
|
||||
[2]LONG REAL x := (a, a/n);
|
||||
|
||||
WHILE ABS(x[2] - x[1]) > p DO
|
||||
x := (x[2], ((n-1)*x[2] + a/x[2]**(n-1))/n )
|
||||
OD;
|
||||
x[2]
|
||||
);
|
||||
|
||||
PRIO ROOT = 8;
|
||||
OP ROOT = (INT n, LONG REAL a)LONG REAL: nth root(n, a, default p);
|
||||
OP ROOT = (INT n, INT a)LONG REAL: nth root(n, a, default p);
|
||||
|
||||
main:
|
||||
(
|
||||
printf(($2(" "gl)$,
|
||||
nth root(10, LONG 7131.5 ** 10, default p),
|
||||
nth root(5, 34, default p)));
|
||||
printf(($2(" "gl)$,
|
||||
10 ROOT ( LONG 7131.5 ** 10 ),
|
||||
5 ROOT 34))
|
||||
)
|
||||
25
Task/Nth-root/ALGOL-W/nth-root.alg
Normal file
25
Task/Nth-root/ALGOL-W/nth-root.alg
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
begin
|
||||
% nth root algorithm %
|
||||
% returns the nth root of A, A must be > 0 %
|
||||
% the required precision should be specified in precision %
|
||||
long real procedure nthRoot( long real value A
|
||||
; integer value n
|
||||
; long real value precision
|
||||
) ;
|
||||
begin
|
||||
long real xk, xd;
|
||||
integer n1;
|
||||
n1 := n - 1;
|
||||
xk := A / n;
|
||||
while begin
|
||||
xd := ( ( A / ( xk ** n1 ) ) - xk ) / n;
|
||||
xk := xk + xd;
|
||||
abs( xd ) > precision
|
||||
end do begin end;
|
||||
xk
|
||||
end nthRoot ;
|
||||
% test cases %
|
||||
r_format := "A"; r_w := 15; r_d := 6; % set output format %
|
||||
write( nthRoot( 7131.5 ** 10, 10, 1'-5 ) );
|
||||
write( nthRoot( 64, 6, 1'-5 ) );
|
||||
end.
|
||||
101
Task/Nth-root/ARM-Assembly/nth-root.arm
Normal file
101
Task/Nth-root/ARM-Assembly/nth-root.arm
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program nroot.s */
|
||||
/* compile with option -mfpu=vfpv3 -mfloat-abi=hard */
|
||||
/* link with gcc. Use C function for display float */
|
||||
|
||||
/* Constantes */
|
||||
.equ EXIT, 1 @ Linux syscall
|
||||
|
||||
/* Initialized data */
|
||||
.data
|
||||
szFormat1: .asciz " %+09.15f\n"
|
||||
.align 4
|
||||
iNumberA: .int 1024
|
||||
|
||||
/* UnInitialized data */
|
||||
.bss
|
||||
.align 4
|
||||
|
||||
/* code section */
|
||||
.text
|
||||
.global main
|
||||
main: @ entry of program
|
||||
push {fp,lr} @ saves registers
|
||||
|
||||
/* root 10ieme de 1024 */
|
||||
ldr r0,iAdriNumberA @ number address
|
||||
ldr r0,[r0]
|
||||
vmov s0,r0 @
|
||||
vcvt.f64.s32 d0, s0 @conversion in float single précision (32 bits)
|
||||
mov r0,#10 @ N
|
||||
bl nthRoot
|
||||
ldr r0,iAdrszFormat1 @ format
|
||||
vmov r2,r3,d0
|
||||
bl printf @ call C function !!!
|
||||
@ Attention register dn lost !!!
|
||||
/* square root of 2 */
|
||||
vmov.f64 d1,#2.0 @ conversion 2 in float register d1
|
||||
mov r0,#2 @ N
|
||||
bl nthRoot
|
||||
ldr r0,iAdrszFormat1 @ format
|
||||
vmov r2,r3,d0
|
||||
bl printf @ call C function !!!
|
||||
|
||||
100: @ standard end of the program
|
||||
mov r0, #0 @ return code
|
||||
pop {fp,lr} @restaur registers
|
||||
mov r7, #EXIT @ request to exit program
|
||||
swi 0 @ perform the system call
|
||||
|
||||
iAdrszFormat1: .int szFormat1
|
||||
iAdriNumberA: .int iNumberA
|
||||
|
||||
/******************************************************************/
|
||||
/* compute nth root */
|
||||
/******************************************************************/
|
||||
/* r0 contains N */
|
||||
/* d0 contains the value */
|
||||
/* d0 return result */
|
||||
nthRoot:
|
||||
push {r1,r2,lr} @ save registers
|
||||
vpush {d1-d8} @ save float registers
|
||||
FMRX r1,FPSCR @ copy FPSCR into r1
|
||||
BIC r1,r1,#0x00370000 @ clears STRIDE and LEN
|
||||
FMXR FPSCR,r1 @ copy r1 back into FPSCR
|
||||
|
||||
vmov s2,r0 @
|
||||
vcvt.f64.s32 d6, s2 @ N conversion in float double précision (64 bits)
|
||||
sub r1,r0,#1 @ N - 1
|
||||
vmov s8,r1 @
|
||||
vcvt.f64.s32 d4, s8 @conversion in float double précision (64 bits)
|
||||
vmov.f64 d2,d0 @ a = A
|
||||
vdiv.F64 d3,d0,d6 @ b = A/n
|
||||
adr r2,dfPrec @ load précision
|
||||
vldr d8,[r2]
|
||||
1: @ begin loop
|
||||
vmov.f64 d2,d3 @ a <- b
|
||||
vmul.f64 d5,d3,d4 @ (N-1)*b
|
||||
|
||||
vmov.f64 d1,#1.0 @ constante 1 -> float
|
||||
mov r2,#0 @ loop indice
|
||||
2: @ compute pow (n-1)
|
||||
vmul.f64 d1,d1,d3 @
|
||||
add r2,#1
|
||||
cmp r2,r1 @ n -1 ?
|
||||
blt 2b @ no -> loop
|
||||
vdiv.f64 d7,d0,d1 @ A / b pow (n-1)
|
||||
vadd.f64 d7,d7,d5 @ + (N-1)*b
|
||||
vdiv.f64 d3,d7,d6 @ / N -> new b
|
||||
vsub.f64 d1,d3,d2 @ compute gap
|
||||
vabs.f64 d1,d1 @ absolute value
|
||||
vcmp.f64 d1,d8 @ compare float maj FPSCR
|
||||
fmstat @ transfert FPSCR -> APSR
|
||||
@ or use VMRS APSR_nzcv, FPSCR
|
||||
bgt 1b @ if gap > précision -> loop
|
||||
vmov.f64 d0,d3 @ end return result in d0
|
||||
|
||||
100:
|
||||
vpop {d1-d8} @ restaur float registers
|
||||
pop {r1,r2,lr} @ restaur arm registers
|
||||
bx lr
|
||||
dfPrec: .double 0f1E-10 @ précision
|
||||
22
Task/Nth-root/AWK/nth-root.awk
Normal file
22
Task/Nth-root/AWK/nth-root.awk
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#!/usr/bin/awk -f
|
||||
BEGIN {
|
||||
# test
|
||||
print nthroot(8,3)
|
||||
print nthroot(16,2)
|
||||
print nthroot(16,4)
|
||||
print nthroot(125,3)
|
||||
print nthroot(3,3)
|
||||
print nthroot(3,2)
|
||||
}
|
||||
|
||||
function nthroot(y,n) {
|
||||
eps = 1e-15; # relative accuracy
|
||||
x = 1;
|
||||
do {
|
||||
d = ( y / ( x^(n-1) ) - x ) / n ;
|
||||
x += d;
|
||||
e = eps*x; # absolute accuracy
|
||||
} while ( d < -e || d > e )
|
||||
|
||||
return x
|
||||
}
|
||||
46
Task/Nth-root/Action-/nth-root.action
Normal file
46
Task/Nth-root/Action-/nth-root.action
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
INCLUDE "H6:REALMATH.ACT"
|
||||
|
||||
PROC NthRoot(REAL POINTER a,n REAL POINTER res)
|
||||
REAL n1,eps,one,tmp1,tmp2,tmp3
|
||||
|
||||
ValR("0.0001",eps)
|
||||
IntToReal(1,one)
|
||||
RealSub(n,one,n1)
|
||||
|
||||
Sqrt(a,res) ;res=sqrt(a)
|
||||
DO
|
||||
Power(res,n,tmp1) ;tmp=res^n
|
||||
RealSub(a,tmp1,tmp2) ;tmp2=a-res^n
|
||||
RealAbs(tmp2,tmp1) ;tmp1=abs(a-res^n)
|
||||
IF RealGreaterOrEqual(eps,tmp1) THEN
|
||||
RETURN
|
||||
FI
|
||||
|
||||
Power(res,n1,tmp1) ;tmp1=res^(n-1)
|
||||
RealDiv(a,tmp1,tmp2) ;tmp2=a/(res^(n-1))
|
||||
RealMult(n1,res,tmp1) ;tmp1=(n-1)*res
|
||||
RealAdd(tmp1,tmp2,tmp3) ;tmp3=((n-1)*res + a/(res^(n-1)))
|
||||
RealDiv(tmp3,n,res) ;res=((n-1)*res + a/(res^(n-1)))/n
|
||||
OD
|
||||
RETURN
|
||||
|
||||
PROC Test(CHAR ARRAY sa,sn)
|
||||
REAL a,n,res
|
||||
|
||||
ValR(sa,a)
|
||||
ValR(sn,n)
|
||||
PrintR(n) Print(" root of ")
|
||||
PrintR(a) Print(" is ")
|
||||
NthRoot(a,n,res)
|
||||
PrintRE(res)
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
Put(125) PutE() ;clear screen
|
||||
MathInit()
|
||||
Test("2","2")
|
||||
Test("81","4")
|
||||
Test("1024","10")
|
||||
Test("7","0.5")
|
||||
Test("12.34","56.78")
|
||||
RETURN
|
||||
27
Task/Nth-root/Ada/nth-root.ada
Normal file
27
Task/Nth-root/Ada/nth-root.ada
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;
|
||||
14
Task/Nth-root/Arturo/nth-root.arturo
Normal file
14
Task/Nth-root/Arturo/nth-root.arturo
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
nthRoot: function [a,n][
|
||||
N: to :floating n
|
||||
result: a
|
||||
x: a / N
|
||||
while [0.000000000000001 < abs result-x][
|
||||
x: result
|
||||
result: (1//n) * add (n-1)*x a/pow x n-1
|
||||
]
|
||||
return result
|
||||
]
|
||||
|
||||
print nthRoot 34.0 5
|
||||
print nthRoot 42.0 10
|
||||
print nthRoot 5.0 2
|
||||
19
Task/Nth-root/AutoHotkey/nth-root.ahk
Normal file
19
Task/Nth-root/AutoHotkey/nth-root.ahk
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
p := 0.000001
|
||||
|
||||
MsgBox, % nthRoot( 10, 7131.5**10, p) "`n"
|
||||
. nthRoot( 5, 34.0 , p) "`n"
|
||||
. nthRoot( 2, 2 , p) "`n"
|
||||
. nthRoot(0.5, 7 , p) "`n"
|
||||
|
||||
|
||||
;---------------------------------------------------------------------------
|
||||
nthRoot(n, A, p) { ; http://en.wikipedia.org/wiki/Nth_root_algorithm
|
||||
;---------------------------------------------------------------------------
|
||||
x1 := A
|
||||
x2 := A / n
|
||||
While Abs(x1 - x2) > p {
|
||||
x1 := x2
|
||||
x2 := ((n-1)*x2+A/x2**(n-1))/n
|
||||
}
|
||||
Return, x2
|
||||
}
|
||||
26
Task/Nth-root/AutoIt/nth-root.autoit
Normal file
26
Task/Nth-root/AutoIt/nth-root.autoit
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
|
||||
11
Task/Nth-root/BASIC/nth-root-1.basic
Normal file
11
Task/Nth-root/BASIC/nth-root-1.basic
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
FUNCTION RootX (tBase AS DOUBLE, tExp AS DOUBLE, diffLimit AS DOUBLE) AS DOUBLE
|
||||
DIM tmp1 AS DOUBLE, tmp2 AS DOUBLE
|
||||
' Initial guess:
|
||||
tmp1 = tBase / tExp
|
||||
DO
|
||||
tmp2 = tmp1
|
||||
' 1# tells compiler that "1" is a double, not an integer
|
||||
tmp1 = (((tExp - 1#) * tmp2) + (tBase / (tmp2 ^ (tExp - 1#)))) / tExp
|
||||
LOOP WHILE (ABS(tmp1 - tmp2) > diffLimit)
|
||||
RootX = tmp1
|
||||
END FUNCTION
|
||||
1
Task/Nth-root/BASIC/nth-root-2.basic
Normal file
1
Task/Nth-root/BASIC/nth-root-2.basic
Normal file
|
|
@ -0,0 +1 @@
|
|||
FUNCTION RootX# (tBase AS DOUBLE, tExp AS DOUBLE, diffLimit AS DOUBLE)
|
||||
1
Task/Nth-root/BASIC/nth-root-3.basic
Normal file
1
Task/Nth-root/BASIC/nth-root-3.basic
Normal file
|
|
@ -0,0 +1 @@
|
|||
PRINT "The "; e; "th root of "; b; " is "; RootX(b, e, .000001)
|
||||
24
Task/Nth-root/BASIC256/nth-root.basic
Normal file
24
Task/Nth-root/BASIC256/nth-root.basic
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
function nth_root(n, a)
|
||||
precision = 0.0001
|
||||
|
||||
dim x(2)
|
||||
x[0] = a
|
||||
x[1] = a /n
|
||||
while abs(x[1] - x[0]) > precision
|
||||
x[0] = x[1]
|
||||
x[1] = ((n -1.0) * x[1] +a / x[1]^(n -1.0)) / n
|
||||
end while
|
||||
return x[1]
|
||||
end function
|
||||
|
||||
print " n 5643 ^ 1 / n nth_root ^ n"
|
||||
print " --------------------------------------"
|
||||
for n = 3 to 11 step 2
|
||||
tmp = nth_root(n, 5643)
|
||||
print " "; n; " "; tmp; chr(9); (tmp ^ n)
|
||||
next n
|
||||
print
|
||||
for n = 25 to 125 step 25
|
||||
tmp = nth_root(n, 5643)
|
||||
print n; " "; tmp; chr(9); (tmp ^ n)
|
||||
next n
|
||||
13
Task/Nth-root/BBC-BASIC/nth-root.basic
Normal file
13
Task/Nth-root/BBC-BASIC/nth-root.basic
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
*FLOAT 64
|
||||
@% = &D0D
|
||||
PRINT "Cube root of 5 is "; FNroot(3, 5, 0)
|
||||
PRINT "125th root of 5643 is "; FNroot(125, 5643, 0)
|
||||
END
|
||||
|
||||
DEF FNroot(n%, a, d)
|
||||
LOCAL x0, x1 : x0 = a / n% : REM Initial guess
|
||||
REPEAT
|
||||
x1 = ((n% - 1)*x0 + a/x0^(n%-1)) / n%
|
||||
SWAP x0, x1
|
||||
UNTIL ABS (x0 - x1) <= d
|
||||
= x0
|
||||
22
Task/Nth-root/BQN/nth-root-1.bqn
Normal file
22
Task/Nth-root/BQN/nth-root-1.bqn
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
_while_ ← {𝔽⍟𝔾∘𝔽_𝕣_𝔾∘𝔽⍟𝔾𝕩}
|
||||
Root ← √
|
||||
Root1 ← ⋆⟜÷˜
|
||||
Root2 ← {
|
||||
n 𝕊 a‿prec:
|
||||
1⊑{
|
||||
p‿x:
|
||||
⟨
|
||||
x
|
||||
((p × n - 1) + a ÷ p ⋆ n - 1) ÷ n
|
||||
⟩
|
||||
} _while_ {
|
||||
p‿x:
|
||||
prec ≤ | p - x
|
||||
} ⟨a, ⌊a÷n⟩;
|
||||
𝕨 𝕊 𝕩: 𝕨 𝕊 𝕩‿1E¯5
|
||||
}
|
||||
|
||||
•Show 3 Root 5
|
||||
•Show 3 Root1 5
|
||||
•Show 3 Root2 5
|
||||
•Show 3 Root2 5‿1E¯16
|
||||
4
Task/Nth-root/BQN/nth-root-2.bqn
Normal file
4
Task/Nth-root/BQN/nth-root-2.bqn
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
1.7099759466766968
|
||||
1.7099759466766968
|
||||
1.7099759641072136
|
||||
1.709975946676697
|
||||
13
Task/Nth-root/Basic09/nth-root.basic
Normal file
13
Task/Nth-root/Basic09/nth-root.basic
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
PROCEDURE nth
|
||||
PARAM N : INTEGER; A, P : REAL
|
||||
DIM TEMP0, TEMP1 : REAL
|
||||
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
|
||||
END
|
||||
30
Task/Nth-root/Bc/nth-root.bc
Normal file
30
Task/Nth-root/Bc/nth-root.bc
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* Take the nth root of 'a' (a positive real number).
|
||||
* 'n' must be an integer.
|
||||
* Result will have 'd' digits after the decimal point.
|
||||
*/
|
||||
define r(a, n, d) {
|
||||
auto e, o, x, y, z
|
||||
|
||||
if (n == 0) return(1)
|
||||
if (a == 0) return(0)
|
||||
|
||||
o = scale
|
||||
scale = d
|
||||
e = 1 / 10 ^ d
|
||||
|
||||
if (n < 0) {
|
||||
n = -n
|
||||
a = 1 / a
|
||||
}
|
||||
|
||||
x = 1
|
||||
while (1) {
|
||||
y = ((n - 1) * x + a / x ^ (n - 1)) / n
|
||||
z = x - y
|
||||
if (z < 0) z = -z
|
||||
if (z < e) break
|
||||
x = y
|
||||
}
|
||||
scale = o
|
||||
return(y)
|
||||
}
|
||||
29
Task/Nth-root/Bracmat/nth-root.bracmat
Normal file
29
Task/Nth-root/Bracmat/nth-root.bracmat
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
( ( root
|
||||
= n a d x0 x1 d2 rnd 10-d
|
||||
. ( rnd { For 'rounding' rational numbers = keep number of digits within bounds. }
|
||||
= N r
|
||||
. !arg:(?N.?r)
|
||||
& div$(!N*!r+1/2.1)*!r^-1
|
||||
)
|
||||
& !arg:(?n,?a,?d)
|
||||
& !a*!n^-1:?x0
|
||||
& 10^(-1*!d):?10-d
|
||||
& whl
|
||||
' ( ( rnd$(((!n+-1)*!x0+!a*!x0^(1+-1*!n))*!n^-1.10^!d)
|
||||
. !x0
|
||||
)
|
||||
: (?x0.?x1)
|
||||
& (!x0+-1*!x1)^2:~<!10-d { Exit loop when required precision is reached. }
|
||||
)
|
||||
& flt$(!x0,!d) { Convert rational number to floating point representation. }
|
||||
)
|
||||
& ( show
|
||||
= N A precision
|
||||
. !arg:(?N,?A,?precision)
|
||||
& out$(str$(!A "^(" !N^-1 ")=" root$(!N,!A,!precision)))
|
||||
)
|
||||
& show$(10,1024,20)
|
||||
& show$(3,27,20)
|
||||
& show$(2,2,100)
|
||||
& show$(125,5642,20)
|
||||
)
|
||||
12
Task/Nth-root/C++/nth-root-1.cpp
Normal file
12
Task/Nth-root/C++/nth-root-1.cpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
double NthRoot(double m_nValue, double index, double guess, double pc)
|
||||
{
|
||||
double result = guess;
|
||||
double result_next;
|
||||
do
|
||||
{
|
||||
result_next = (1.0/index)*((index-1.0)*result+(m_nValue)/(pow(result,(index-1.0))));
|
||||
result = result_next;
|
||||
pc--;
|
||||
}while(pc>1);
|
||||
return result;
|
||||
};
|
||||
4
Task/Nth-root/C++/nth-root-2.cpp
Normal file
4
Task/Nth-root/C++/nth-root-2.cpp
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
double NthRoot(double value, double degree)
|
||||
{
|
||||
return pow(value, (double)(1 / degree));
|
||||
};
|
||||
21
Task/Nth-root/C-sharp/nth-root.cs
Normal file
21
Task/Nth-root/C-sharp/nth-root.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(NthRoot(81,2,.001));
|
||||
Console.WriteLine(NthRoot(1000,3,.001));
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
public static double NthRoot(double A,int n, double p)
|
||||
{
|
||||
double _n= (double) n;
|
||||
double[] x = new double[2];
|
||||
x[0] = A;
|
||||
x[1] = A/_n;
|
||||
while(Math.Abs(x[0] -x[1] ) > p)
|
||||
{
|
||||
x[1] = x[0];
|
||||
x[0] = (1/_n)*(((_n-1)*x[1]) + (A/Math.Pow(x[1],_n-1)));
|
||||
|
||||
}
|
||||
return x[0];
|
||||
}
|
||||
34
Task/Nth-root/C/nth-root.c
Normal file
34
Task/Nth-root/C/nth-root.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include <stdio.h>
|
||||
#include <float.h>
|
||||
|
||||
double pow_ (double x, int e) {
|
||||
int i;
|
||||
double r = 1;
|
||||
for (i = 0; i < e; i++) {
|
||||
r *= x;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
double root (int n, double x) {
|
||||
double d, r = 1;
|
||||
if (!x) {
|
||||
return 0;
|
||||
}
|
||||
if (n < 1 || (x < 0 && !(n&1))) {
|
||||
return 0.0 / 0.0; /* NaN */
|
||||
}
|
||||
do {
|
||||
d = (x / pow_(r, n - 1) - r) / n;
|
||||
r += d;
|
||||
}
|
||||
while (d >= DBL_EPSILON * 10 || d <= -DBL_EPSILON * 10);
|
||||
return r;
|
||||
}
|
||||
|
||||
int main () {
|
||||
int n = 15;
|
||||
double x = pow_(-3.14159, 15);
|
||||
printf("root(%d, %g) = %g\n", n, x, root(n, x));
|
||||
return 0;
|
||||
}
|
||||
98
Task/Nth-root/COBOL/nth-root.cobol
Normal file
98
Task/Nth-root/COBOL/nth-root.cobol
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. Nth-Root.
|
||||
AUTHOR. Bill Gunshannon.
|
||||
INSTALLATION.
|
||||
DATE-WRITTEN. 4 Feb 2020.
|
||||
************************************************************
|
||||
** Program Abstract:
|
||||
** Compute the Nth Root of a positive real number.
|
||||
**
|
||||
** Takes values from console. If Precision is left
|
||||
** blank defaults to 0.001.
|
||||
**
|
||||
** Enter 0 for first value to terminate program.
|
||||
************************************************************
|
||||
|
||||
ENVIRONMENT DIVISION.
|
||||
|
||||
INPUT-OUTPUT SECTION.
|
||||
FILE-CONTROL.
|
||||
SELECT Root-File ASSIGN TO "Root-File"
|
||||
ORGANIZATION IS LINE SEQUENTIAL.
|
||||
|
||||
DATA DIVISION.
|
||||
|
||||
FILE SECTION.
|
||||
|
||||
FD Root-File
|
||||
DATA RECORD IS Parameters.
|
||||
01 Parameters.
|
||||
05 Root PIC 9(5).
|
||||
05 Num PIC 9(5)V9(5).
|
||||
05 Precision PIC 9V9(9).
|
||||
|
||||
|
||||
WORKING-STORAGE SECTION.
|
||||
|
||||
01 TEMP0 PIC 9(9)V9(9).
|
||||
01 TEMP1 PIC 9(9)V9(9).
|
||||
01 RESULTS.
|
||||
05 Field1 PIC ZZZZZ.ZZZZZ.
|
||||
05 FILLER PIC X(5).
|
||||
05 Field2 PIC ZZZZ9.
|
||||
05 FILLER PIC X(14).
|
||||
05 Field3 PIC 9.999999999.
|
||||
|
||||
01 HEADER.
|
||||
05 FILLER PIC X(72)
|
||||
VALUE " Number Root Precision.".
|
||||
|
||||
01 Disp-Root PIC ZZZZZ.ZZZZZ.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
|
||||
Main-Program.
|
||||
PERFORM FOREVER
|
||||
|
||||
PERFORM Get-Input
|
||||
IF Precision = 0.0
|
||||
THEN MOVE 0.001 to Precision
|
||||
END-IF
|
||||
|
||||
PERFORM Compute-Root
|
||||
|
||||
MOVE Root TO Field2
|
||||
MOVE Num TO Field1
|
||||
MOVE Precision TO Field3
|
||||
DISPLAY HEADER
|
||||
DISPLAY RESULTS
|
||||
DISPLAY " "
|
||||
MOVE TEMP1 TO Disp-Root
|
||||
DISPLAY "The Root is: " Disp-Root
|
||||
END-PERFORM.
|
||||
|
||||
Get-Input.
|
||||
DISPLAY "Input Base Number: " WITH NO ADVANCING
|
||||
ACCEPT Num
|
||||
IF Num EQUALS ZERO
|
||||
THEN
|
||||
DISPLAY "Good Bye."
|
||||
STOP RUN
|
||||
END-IF
|
||||
DISPLAY "Input Root: " WITH NO ADVANCING
|
||||
ACCEPT Root
|
||||
DISPLAY "Input Desired Precision: " WITH NO ADVANCING
|
||||
ACCEPT Precision.
|
||||
|
||||
Compute-Root.
|
||||
MOVE Root TO TEMP0
|
||||
DIVIDE Num BY Root GIVING TEMP1
|
||||
|
||||
PERFORM UNTIL FUNCTION ABS(TEMP0 - TEMP1)
|
||||
LESS THAN Precision
|
||||
MOVE TEMP1 TO TEMP0
|
||||
COMPUTE TEMP1 = (( Root - 1.0) * TEMP1 + Num /
|
||||
TEMP1 ** (Root - 1.0)) / Root
|
||||
END-PERFORM.
|
||||
|
||||
END-PROGRAM.
|
||||
20
Task/Nth-root/Chipmunk-Basic/nth-root.basic
Normal file
20
Task/Nth-root/Chipmunk-Basic/nth-root.basic
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
10 rem Nth root
|
||||
20 print "Finding the nth root of 144 to 6 decimal places"
|
||||
30 print " x n root"
|
||||
40 print "------------------------"
|
||||
50 for i = 1 to 8
|
||||
60 print using "### ";144;
|
||||
70 print using "#### ";i;
|
||||
80 print using "###.######";nthroot(i,144,1.000000E-07)
|
||||
90 next i
|
||||
100 end
|
||||
1000 sub nthroot(n,x,precision)
|
||||
1010 rem Returns the nth root of value x to stated precision
|
||||
1020 x0 = x
|
||||
1030 x1 = x/n ' - initial guess
|
||||
1040 while abs(x1-x0) > precision
|
||||
1050 x0 = x1
|
||||
1060 x1 = ((n-1)*x1+x/x1^(n-1))/n
|
||||
1070 wend
|
||||
1080 nthroot = x1
|
||||
1090 end sub
|
||||
23
Task/Nth-root/Clojure/nth-root.clj
Normal file
23
Task/Nth-root/Clojure/nth-root.clj
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
(ns test-project-intellij.core
|
||||
(:gen-class))
|
||||
|
||||
;; define abs & power to avoid needing to bring in the clojure Math library
|
||||
(defn abs [x]
|
||||
" Absolute value"
|
||||
(if (< x 0) (- x) x))
|
||||
|
||||
(defn power [x n]
|
||||
" x to power n, where n = 0, 1, 2, ... "
|
||||
(apply * (repeat n x)))
|
||||
|
||||
(defn calc-delta [A x n]
|
||||
" nth rooth algorithm delta calculation "
|
||||
(/ (- (/ A (power x (- n 1))) x) n))
|
||||
|
||||
(defn nth-root
|
||||
" nth root of algorithm: A = numer, n = root"
|
||||
([A n] (nth-root A n 0.5 1.0)) ; Takes only two arguments A, n and calls version which takes A, n, guess-prev, guess-current
|
||||
([A n guess-prev guess-current] ; version take takes in four arguments (A, n, guess-prev, guess-current)
|
||||
(if (< (abs (- guess-prev guess-current)) 1e-6)
|
||||
guess-current
|
||||
(recur A n guess-current (+ guess-current (calc-delta A guess-current n)))))) ; iterate answer using tail recursion
|
||||
26
Task/Nth-root/CoffeeScript/nth-root-1.coffee
Normal file
26
Task/Nth-root/CoffeeScript/nth-root-1.coffee
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
nth_root = (A, n, precision=0.0000000000001) ->
|
||||
x = 1
|
||||
while true
|
||||
x_new = (1 / n) * ((n - 1) * x + A / Math.pow(x, n - 1))
|
||||
return x_new if Math.abs(x_new - x) < precision
|
||||
x = x_new
|
||||
|
||||
# tests
|
||||
do ->
|
||||
tests = [
|
||||
[8, 3]
|
||||
[16, 4]
|
||||
[32, 5]
|
||||
[343, 3]
|
||||
[1024, 10]
|
||||
[1000000000, 3]
|
||||
[1000000000, 9]
|
||||
[100, 2]
|
||||
[100, 3]
|
||||
[100, 5]
|
||||
[100, 10]
|
||||
]
|
||||
for test in tests
|
||||
[x, n] = test
|
||||
root = nth_root x, n
|
||||
console.log "#{x} root #{n} = #{root} (root^#{n} = #{Math.pow root, n})"
|
||||
12
Task/Nth-root/CoffeeScript/nth-root-2.coffee
Normal file
12
Task/Nth-root/CoffeeScript/nth-root-2.coffee
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
> coffee nth_root.coffee
|
||||
8 root 3 = 2 (root^3 = 8)
|
||||
16 root 4 = 2 (root^4 = 16)
|
||||
32 root 5 = 2 (root^5 = 32)
|
||||
343 root 3 = 7 (root^3 = 343)
|
||||
1024 root 10 = 2 (root^10 = 1024)
|
||||
1000000000 root 3 = 1000 (root^3 = 1000000000)
|
||||
1000000000 root 9 = 10 (root^9 = 1000000000)
|
||||
100 root 2 = 10 (root^2 = 100)
|
||||
100 root 3 = 4.641588833612778 (root^3 = 99.99999999999997)
|
||||
100 root 5 = 2.5118864315095806 (root^5 = 100.0000000000001)
|
||||
100 root 10 = 1.5848931924611134 (root^10 = 99.99999999999993)
|
||||
9
Task/Nth-root/Common-Lisp/nth-root-1.lisp
Normal file
9
Task/Nth-root/Common-Lisp/nth-root-1.lisp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(defun nth-root (n a &optional (epsilon .0001) (guess (1- n)))
|
||||
(assert (and (> n 1) (> a 0)))
|
||||
(flet ((next (x)
|
||||
(/ (+ (* (1- n) x)
|
||||
(/ a (expt x (1- n))))
|
||||
n)))
|
||||
(do* ((xi guess xi+1)
|
||||
(xi+1 (next xi) (next xi)))
|
||||
((< (abs (- xi+1 xi)) epsilon) xi+1))))
|
||||
4
Task/Nth-root/Common-Lisp/nth-root-2.lisp
Normal file
4
Task/Nth-root/Common-Lisp/nth-root-2.lisp
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(let* ((r (nth-root 3 10))
|
||||
(rf (coerce r 'float)))
|
||||
(print (* r r r ))
|
||||
(print (* rf rf rf)))
|
||||
36
Task/Nth-root/Craft-Basic/nth-root.basic
Normal file
36
Task/Nth-root/Craft-Basic/nth-root.basic
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
precision 6
|
||||
|
||||
let a = int(rnd * 5999) + 2
|
||||
|
||||
print "calculating nth root of ", a, "..."
|
||||
|
||||
for n = 1 to 10
|
||||
|
||||
gosub nroot
|
||||
print n, " : ", y
|
||||
|
||||
next n
|
||||
|
||||
end
|
||||
|
||||
sub nroot
|
||||
|
||||
let p = .00001
|
||||
|
||||
let x = a
|
||||
let y = a / n
|
||||
|
||||
do
|
||||
|
||||
if abs(x - y) > p then
|
||||
|
||||
let x = y
|
||||
let y = ((n - 1) * y + a / y ^ (n - 1)) / n
|
||||
|
||||
endif
|
||||
|
||||
wait
|
||||
|
||||
loop abs(x - y) > p
|
||||
|
||||
return
|
||||
13
Task/Nth-root/D/nth-root.d
Normal file
13
Task/Nth-root/D/nth-root.d
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import std.stdio, std.math;
|
||||
|
||||
real nthroot(in int n, in real A, in real p=0.001) pure nothrow {
|
||||
real[2] x = [A, A / n];
|
||||
while (abs(x[1] - x[0]) > p)
|
||||
x = [x[1], ((n - 1) * x[1] + A / (x[1] ^^ (n-1))) / n];
|
||||
return x[1];
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln(nthroot(10, 7131.5 ^^ 10));
|
||||
writeln(nthroot(6, 64));
|
||||
}
|
||||
15
Task/Nth-root/Delphi/nth-root.delphi
Normal file
15
Task/Nth-root/Delphi/nth-root.delphi
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
USES
|
||||
Math;
|
||||
|
||||
function NthRoot(A, Precision: Double; n: Integer): Double;
|
||||
var
|
||||
x_p, X: Double;
|
||||
begin
|
||||
x_p := Sqrt(A);
|
||||
while Abs(A - Power(x_p, n)) > Precision do
|
||||
begin
|
||||
x := (1/n) * (((n-1) * x_p) + (A/(Power(x_p, n - 1))));
|
||||
x_p := x;
|
||||
end;
|
||||
Result := x_p;
|
||||
end;
|
||||
12
Task/Nth-root/E/nth-root.e
Normal file
12
Task/Nth-root/E/nth-root.e
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
def nthroot(n, x) {
|
||||
require(n > 1 && x > 0)
|
||||
def np := n - 1
|
||||
def iter(g) { return (np*g + x/g**np) / n }
|
||||
var g1 := x
|
||||
var g2 := iter(g1)
|
||||
while (!(g1 <=> g2)) {
|
||||
g1 := iter(g1)
|
||||
g2 := iter(iter(g2))
|
||||
}
|
||||
return g1
|
||||
}
|
||||
19
Task/Nth-root/EasyLang/nth-root.easy
Normal file
19
Task/Nth-root/EasyLang/nth-root.easy
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
proc power x n . r .
|
||||
r = 1
|
||||
for i = 1 to n
|
||||
r *= x
|
||||
.
|
||||
.
|
||||
proc nth_root x n . r .
|
||||
r = 2
|
||||
repeat
|
||||
call power r n - 1 p
|
||||
d = (x / p - r) / n
|
||||
r += d
|
||||
until abs d < 0.0001
|
||||
.
|
||||
.
|
||||
call power 3.1416 10 x
|
||||
call nth_root x 10 r
|
||||
numfmt 4 0
|
||||
print r
|
||||
13
Task/Nth-root/Elixir/nth-root.elixir
Normal file
13
Task/Nth-root/Elixir/nth-root.elixir
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
defmodule RC do
|
||||
def nth_root(n, x, precision \\ 1.0e-5) do
|
||||
f = fn(prev) -> ((n - 1) * prev + x / :math.pow(prev, (n-1))) / n end
|
||||
fixed_point(f, x, precision, f.(x))
|
||||
end
|
||||
|
||||
defp fixed_point(_, guess, tolerance, next) when abs(guess - next) < tolerance, do: next
|
||||
defp fixed_point(f, _, tolerance, next), do: fixed_point(f, next, tolerance, f.(next))
|
||||
end
|
||||
|
||||
Enum.each([{2, 2}, {4, 81}, {10, 1024}, {1/2, 7}], fn {n, x} ->
|
||||
IO.puts "#{n} root of #{x} is #{RC.nth_root(n, x)}"
|
||||
end)
|
||||
6
Task/Nth-root/Erlang/nth-root-1.erl
Normal file
6
Task/Nth-root/Erlang/nth-root-1.erl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
fixed_point(F, Guess, Tolerance) ->
|
||||
fixed_point(F, Guess, Tolerance, F(Guess)).
|
||||
fixed_point(_, Guess, Tolerance, Next) when abs(Guess - Next) < Tolerance ->
|
||||
Next;
|
||||
fixed_point(F, _, Tolerance, Next) ->
|
||||
fixed_point(F, Next, Tolerance, F(Next)).
|
||||
4
Task/Nth-root/Erlang/nth-root-2.erl
Normal file
4
Task/Nth-root/Erlang/nth-root-2.erl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
nth_root(N, X) -> nth_root(N, X, 1.0e-5).
|
||||
nth_root(N, X, Precision) ->
|
||||
F = fun(Prev) -> ((N - 1) * Prev + X / math:pow(Prev, (N-1))) / N end,
|
||||
fixed_point(F, X, Precision).
|
||||
1
Task/Nth-root/Excel/nth-root.excel
Normal file
1
Task/Nth-root/Excel/nth-root.excel
Normal file
|
|
@ -0,0 +1 @@
|
|||
=A1^(1/B1)
|
||||
21
Task/Nth-root/F-Sharp/nth-root.fs
Normal file
21
Task/Nth-root/F-Sharp/nth-root.fs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
let nthroot n A =
|
||||
let rec f x =
|
||||
let m = n - 1.
|
||||
let x' = (m * x + A/x**m) / n
|
||||
match abs(x' - x) with
|
||||
| t when t < abs(x * 1e-9) -> x'
|
||||
| _ -> f x'
|
||||
f (A / double n)
|
||||
|
||||
[<EntryPoint>]
|
||||
let main args =
|
||||
if args.Length <> 2 then
|
||||
eprintfn "usage: nthroot n A"
|
||||
exit 1
|
||||
let (b, n) = System.Double.TryParse(args.[0])
|
||||
let (b', A) = System.Double.TryParse(args.[1])
|
||||
if (not b) || (not b') then
|
||||
eprintfn "error: parameter must be a number"
|
||||
exit 1
|
||||
printf "%A" (nthroot n A)
|
||||
0
|
||||
12
Task/Nth-root/Factor/nth-root.factor
Normal file
12
Task/Nth-root/Factor/nth-root.factor
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
USING: kernel locals math math.functions prettyprint ;
|
||||
|
||||
:: th-root ( a n -- a^1/n )
|
||||
a [
|
||||
a over n 1 - ^ /f
|
||||
over n 1 - *
|
||||
+ n /f
|
||||
swap over 1e-5 ~ not
|
||||
] loop ;
|
||||
|
||||
34 5 th-root . ! 2.024397458499888
|
||||
34 5 recip ^ . ! 2.024397458499888
|
||||
11
Task/Nth-root/Forth/nth-root.fth
Normal file
11
Task/Nth-root/Forth/nth-root.fth
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
: th-root { F: a F: n -- a^1/n }
|
||||
a
|
||||
begin
|
||||
a fover n 1e f- f** f/
|
||||
fover n 1e f- f*
|
||||
f+ n f/
|
||||
fswap fover 1e-5 f~
|
||||
until ;
|
||||
|
||||
34e 5e th-root f. \ 2.02439745849989
|
||||
34e 5e 1/f f** f. \ 2.02439745849989
|
||||
42
Task/Nth-root/Fortran/nth-root.f
Normal file
42
Task/Nth-root/Fortran/nth-root.f
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
program NthRootTest
|
||||
implicit none
|
||||
|
||||
print *, nthroot(10, 7131.5**10)
|
||||
print *, nthroot(5, 34.0)
|
||||
|
||||
contains
|
||||
|
||||
function nthroot(n, A, p)
|
||||
real :: nthroot
|
||||
integer, intent(in) :: n
|
||||
real, intent(in) :: A
|
||||
real, intent(in), optional :: p
|
||||
|
||||
real :: rp, x(2)
|
||||
|
||||
if ( A < 0 ) then
|
||||
stop "A < 0" ! we handle only real positive numbers
|
||||
elseif ( A == 0 ) then
|
||||
nthroot = 0
|
||||
return
|
||||
end if
|
||||
|
||||
if ( present(p) ) then
|
||||
rp = p
|
||||
else
|
||||
rp = 0.001
|
||||
end if
|
||||
|
||||
x(1) = A
|
||||
x(2) = A/n ! starting "guessed" value...
|
||||
|
||||
do while ( abs(x(2) - x(1)) > rp )
|
||||
x(1) = x(2)
|
||||
x(2) = ((n-1.0)*x(2) + A/(x(2) ** (n-1.0)))/real(n)
|
||||
end do
|
||||
|
||||
nthroot = x(2)
|
||||
|
||||
end function nthroot
|
||||
|
||||
end program NthRootTest
|
||||
41
Task/Nth-root/FreeBASIC/nth-root.basic
Normal file
41
Task/Nth-root/FreeBASIC/nth-root.basic
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
' version 14-01-2019
|
||||
' compile with: fbc -s console
|
||||
|
||||
Function nth_root(n As Integer, number As Double) As Double
|
||||
|
||||
Dim As Double a1 = number / n, a2 , a3
|
||||
|
||||
Do
|
||||
a3 = Abs(a2 - a1)
|
||||
a2 = ((n -1) * a1 + number / a1 ^ (n -1)) / n
|
||||
Swap a1, a2
|
||||
Loop Until Abs(a2 - a1) = a3
|
||||
|
||||
Return a1
|
||||
|
||||
End Function
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As UInteger n
|
||||
Dim As Double tmp
|
||||
|
||||
Print
|
||||
Print " n 5643 ^ 1 / n nth_root ^ n"
|
||||
Print " ------------------------------------"
|
||||
For n = 3 To 11 Step 2
|
||||
tmp = nth_root(n, 5643)
|
||||
Print Using " ### ###.######## ####.########"; n; tmp; tmp ^ n
|
||||
Next
|
||||
|
||||
Print
|
||||
For n = 25 To 125 Step 25
|
||||
tmp = nth_root(n, 5643)
|
||||
Print Using " ### ###.######## ####.########"; n; tmp; tmp ^ n
|
||||
Next
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
22
Task/Nth-root/FutureBasic/nth-root.basic
Normal file
22
Task/Nth-root/FutureBasic/nth-root.basic
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
window 1
|
||||
|
||||
local fn NthRoot( root as long, a as long, precision as double ) as double
|
||||
double x0, x1
|
||||
|
||||
x0 = a : x1 = a /root
|
||||
while ( abs( x1 - x0 ) > precision )
|
||||
x0 = x1
|
||||
x1 = ( ( root -1.0 ) * x1 + a / x1 ^ ( root -1.0 ) ) /root
|
||||
wend
|
||||
end fn = x1
|
||||
|
||||
print " 125th Root of 5643 Precision .001",, using "#.###############"; fn NthRoot( 125, 5642, 0.001 )
|
||||
print " 125th Root of 5643 Precision .001",, using "#.###############"; fn NthRoot( 125, 5642, 0.001 )
|
||||
print " 125th Root of 5643 Precision .00001", using "#.###############"; fn NthRoot( 125, 5642, 0.00001 )
|
||||
print " Cube Root of 27 Precision .00001", using "#.###############"; fn NthRoot( 3, 27, 0.00001 )
|
||||
print "Square Root of 2 Precision .00001", using "#.###############"; fn NthRoot( 2, 2, 0.00001 )
|
||||
print "Square Root of 2 Precision .00001", using "#.###############"; sqr(2) // Processor floating point calc deviation
|
||||
print " 10th Root of 1024 Precision .00001", using "#.###############"; fn NthRoot( 10, 1024, 0.00001 )
|
||||
print " 5th Root of 34 Precision .00001", using "#.###############"; fn NthRoot( 5, 34, 0.00001 )
|
||||
|
||||
HandleEvents
|
||||
19
Task/Nth-root/Go/nth-root-1.go
Normal file
19
Task/Nth-root/Go/nth-root-1.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
func root(a float64, n int) float64 {
|
||||
n1 := n - 1
|
||||
n1f, rn := float64(n1), 1/float64(n)
|
||||
x, x0 := 1., 0.
|
||||
for {
|
||||
potx, t2 := 1/x, a
|
||||
for b := n1; b > 0; b >>= 1 {
|
||||
if b&1 == 1 {
|
||||
t2 *= potx
|
||||
}
|
||||
potx *= potx
|
||||
}
|
||||
x0, x = x, rn*(n1f*x+t2)
|
||||
if math.Abs(x-x0)*1e15 < x {
|
||||
break
|
||||
}
|
||||
}
|
||||
return x
|
||||
}
|
||||
65
Task/Nth-root/Go/nth-root-2.go
Normal file
65
Task/Nth-root/Go/nth-root-2.go
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import "math/big"
|
||||
|
||||
func Root(a *big.Float, n uint64) *big.Float {
|
||||
limit := Exp(New(2), 256)
|
||||
n1 := n-1
|
||||
n1f, rn := New(float64(n1)), Div(New(1.0), New(float64(n)))
|
||||
x, x0 := New(1.0), Zero()
|
||||
_ = x0
|
||||
for {
|
||||
potx, t2 := Div(New(1.0), x), a
|
||||
for b:=n1; b>0; b>>=1 {
|
||||
if b&1 == 1 {
|
||||
t2 = Mul(t2, potx)
|
||||
}
|
||||
potx = Mul(potx, potx)
|
||||
}
|
||||
x0, x = x, Mul(rn, Add(Mul(n1f, x), t2) )
|
||||
if Lesser(Mul(Abs(Sub(x, x0)), limit), x) { break }
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func Abs(a *big.Float) *big.Float {
|
||||
return Zero().Abs(a)
|
||||
}
|
||||
|
||||
func Exp(a *big.Float, e uint64) *big.Float {
|
||||
result := Zero().Copy(a)
|
||||
for i:=uint64(0); i<e-1; i++ {
|
||||
result = Mul(result, a)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func New(f float64) *big.Float {
|
||||
r := big.NewFloat(f)
|
||||
r.SetPrec(256)
|
||||
return r
|
||||
}
|
||||
|
||||
func Div(a, b *big.Float) *big.Float {
|
||||
return Zero().Quo(a, b)
|
||||
}
|
||||
|
||||
func Zero() *big.Float {
|
||||
r := big.NewFloat(0.0)
|
||||
r.SetPrec(256)
|
||||
return r
|
||||
}
|
||||
|
||||
func Mul(a, b *big.Float) *big.Float {
|
||||
return Zero().Mul(a, b)
|
||||
}
|
||||
|
||||
func Add(a, b *big.Float) *big.Float {
|
||||
return Zero().Add(a, b)
|
||||
}
|
||||
|
||||
func Sub(a, b *big.Float) *big.Float {
|
||||
return Zero().Sub(a, b)
|
||||
}
|
||||
|
||||
func Lesser(x, y *big.Float) bool {
|
||||
return x.Cmp(y) == -1
|
||||
}
|
||||
13
Task/Nth-root/Groovy/nth-root-1.groovy
Normal file
13
Task/Nth-root/Groovy/nth-root-1.groovy
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import static Constants.tolerance
|
||||
import static java.math.RoundingMode.HALF_UP
|
||||
|
||||
def root(double base, double n) {
|
||||
double xOld = 1
|
||||
double xNew = 0
|
||||
while (true) {
|
||||
xNew = ((n - 1) * xOld + base/(xOld)**(n - 1))/n
|
||||
if ((xNew - xOld).abs() < tolerance) { break }
|
||||
xOld = xNew
|
||||
}
|
||||
(xNew as BigDecimal).setScale(7, HALF_UP)
|
||||
}
|
||||
21
Task/Nth-root/Groovy/nth-root-2.groovy
Normal file
21
Task/Nth-root/Groovy/nth-root-2.groovy
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
class Constants {
|
||||
static final tolerance = 0.00001
|
||||
}
|
||||
|
||||
print '''
|
||||
Base Power Calc'd Root Actual Root
|
||||
------- ------ ----------- -----------
|
||||
'''
|
||||
def testCases = [
|
||||
[b:32.0, n:5.0, r:2.0],
|
||||
[b:81.0, n:4.0, r:3.0],
|
||||
[b:Math.PI**2, n:4.0, r:Math.PI**(0.5)],
|
||||
[b:7.0, n:0.5, r:49.0],
|
||||
]
|
||||
|
||||
testCases.each {
|
||||
def r = root(it.b, it.n)
|
||||
printf('%7.4f %6.4f %11.4f %11.4f\n',
|
||||
it.b, it.n, r, it.r)
|
||||
assert (r - it.r).abs() <= tolerance
|
||||
}
|
||||
1
Task/Nth-root/Haskell/nth-root-1.hs
Normal file
1
Task/Nth-root/Haskell/nth-root-1.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
n `nthRoot` x = fst $ until (uncurry(==)) (\(_,x0) -> (x0,((n-1)*x0+x/x0**(n-1))/n)) (x,x/n)
|
||||
28
Task/Nth-root/Haskell/nth-root-2.hs
Normal file
28
Task/Nth-root/Haskell/nth-root-2.hs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
nthRoot :: Double -> Double -> Double
|
||||
nthRoot n x =
|
||||
fst $
|
||||
until
|
||||
(uncurry (==))
|
||||
(((,) <*> ((/ n) . ((+) . (pn *) <*> (x /) . (** pn)))) . snd)
|
||||
(x, x / n)
|
||||
where
|
||||
pn = pred n
|
||||
|
||||
-------------------------- TESTS --------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
putStrLn $
|
||||
fTable
|
||||
"Nth roots:"
|
||||
(\(a, b) -> show a ++ " `nthRoot` " ++ show b)
|
||||
show
|
||||
(uncurry nthRoot)
|
||||
[(2, 2), (5, 34), (10, 734 ^ 10), (0.5, 7)]
|
||||
|
||||
-------------------- FORMAT OF RESULTS --------------------
|
||||
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
|
||||
fTable s xShow fxShow f xs =
|
||||
let w = maximum (length . xShow <$> xs)
|
||||
rjust n c = drop . length <*> (replicate n c ++)
|
||||
in unlines $
|
||||
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs
|
||||
19
Task/Nth-root/HicEst/nth-root.hicest
Normal file
19
Task/Nth-root/HicEst/nth-root.hicest
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
WRITE(Messagebox) NthRoot(5, 34)
|
||||
WRITE(Messagebox) NthRoot(10, 7131.5^10)
|
||||
|
||||
FUNCTION NthRoot(n, A)
|
||||
REAL :: prec = 0.001
|
||||
|
||||
IF( (n > 0) * (A > 0) ) THEN
|
||||
NthRoot = A / n
|
||||
DO i = 1, 1/prec
|
||||
x = ((n-1)*NthRoot + A/(NthRoot^(n-1))) / n
|
||||
IF( ABS(x - NthRoot) <= prec ) THEN
|
||||
RETURN
|
||||
ENDIF
|
||||
NthRoot = x
|
||||
ENDDO
|
||||
ENDIF
|
||||
|
||||
WRITE(Messagebox, Name) 'Cannot solve problem for:', prec, n, A
|
||||
END
|
||||
23
Task/Nth-root/Icon/nth-root.icon
Normal file
23
Task/Nth-root/Icon/nth-root.icon
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
procedure main()
|
||||
showroot(125,3)
|
||||
showroot(27,3)
|
||||
showroot(1024,10)
|
||||
showroot(39.0625,4)
|
||||
showroot(7131.5^10,10)
|
||||
end
|
||||
|
||||
procedure showroot(a,n)
|
||||
printf("%i-th root of %i = %i\n",n,a,root(a,n))
|
||||
end
|
||||
|
||||
procedure root(a,n,p) #: finds the n-th root of the number a to precision p
|
||||
if n < 0 | type(n) !== "integer" then runerr(101,n)
|
||||
if a < 0 then runerr(205,a)
|
||||
/p := 1e-14 # precision
|
||||
xn := a / real(n) # initial guess
|
||||
while abs(a - xn^n) > p do
|
||||
xn := ((n - 1) * (xi := xn) + a / (xi ^ (n-1))) / real(n)
|
||||
return xn
|
||||
end
|
||||
|
||||
link printf
|
||||
5
Task/Nth-root/J/nth-root.j
Normal file
5
Task/Nth-root/J/nth-root.j
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
'`N X NP' =. (0 { [)`(1 { [)`(2 { [)
|
||||
iter =. N %~ (NP * ]) + X % ] ^ NP
|
||||
nth_root =: (, , _1+[) iter^:_ f. ]
|
||||
10 nth_root 7131.5^10
|
||||
7131.5
|
||||
18
Task/Nth-root/Java/nth-root-1.java
Normal file
18
Task/Nth-root/Java/nth-root-1.java
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
public static double nthroot(int n, double A) {
|
||||
return nthroot(n, A, .001);
|
||||
}
|
||||
public static double nthroot(int n, double A, double p) {
|
||||
if(A < 0) {
|
||||
System.err.println("A < 0");// we handle only real positive numbers
|
||||
return -1;
|
||||
} else if(A == 0) {
|
||||
return 0;
|
||||
}
|
||||
double x_prev = A;
|
||||
double x = A / n; // starting "guessed" value...
|
||||
while(Math.abs(x - x_prev) > p) {
|
||||
x_prev = x;
|
||||
x = ((n - 1.0) * x + A / Math.pow(x, n - 1.0)) / n;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
15
Task/Nth-root/Java/nth-root-2.java
Normal file
15
Task/Nth-root/Java/nth-root-2.java
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
public static double nthroot(int n, double x) {
|
||||
assert (n > 1 && x > 0);
|
||||
int np = n - 1;
|
||||
double g1 = x;
|
||||
double g2 = iter(g1, np, n, x);
|
||||
while (g1 != g2) {
|
||||
g1 = iter(g1, np, n, x);
|
||||
g2 = iter(iter(g2, np, n, x), np, n, x);
|
||||
}
|
||||
return g1;
|
||||
}
|
||||
|
||||
private static double iter(double g, int np, int n, double x) {
|
||||
return (np * g + x / Math.pow(g, np)) / n;
|
||||
}
|
||||
11
Task/Nth-root/JavaScript/nth-root.js
Normal file
11
Task/Nth-root/JavaScript/nth-root.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function nthRoot(num, nArg, precArg) {
|
||||
var n = nArg || 2;
|
||||
var prec = precArg || 12;
|
||||
|
||||
var x = 1; // Initial guess.
|
||||
for (var i=0; i<prec; i++) {
|
||||
x = 1/n * ((n-1)*x + (num / Math.pow(x, n-1)));
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
31
Task/Nth-root/Jq/nth-root-1.jq
Normal file
31
Task/Nth-root/Jq/nth-root-1.jq
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
# An iterative algorithm for finding: self ^ (1/n) to the given
|
||||
# absolute precision if "precision" > 0, or to within the precision
|
||||
# allowed by IEEE 754 64-bit numbers.
|
||||
|
||||
# The following implementation handles underflow caused by poor estimates.
|
||||
def iterative_nth_root(n; precision):
|
||||
def abs: if . < 0 then -. else . end;
|
||||
def sq: .*.;
|
||||
def pow(p): . as $in | reduce range(0;p) as $i (1; . * $in);
|
||||
def _iterate: # state: [A, x1, x2, prevdelta]
|
||||
.[0] as $A | .[1] as $x1 | .[2] as $x2 | .[3] as $prevdelta
|
||||
| ( $x2 | pow(n-1)) as $power
|
||||
| if $power <= 2.155094094640383e-309
|
||||
then [$A, $x1, ($x1 + $x2)/2, n] | _iterate
|
||||
else (((n-1)*$x2 + ($A/$power))/n) as $x1
|
||||
| (($x1 - $x2)|abs) as $delta
|
||||
| if (precision == 0 and $delta == $prevdelta and $delta < 1e-15)
|
||||
or (precision > 0 and $delta <= precision) or $delta == 0 then $x1
|
||||
else [$A, $x2, $x1, $delta] | _iterate
|
||||
end
|
||||
end
|
||||
;
|
||||
if n == 1 then .
|
||||
elif . == 0 then 0
|
||||
elif . < 0 then error("iterative_nth_root: input \(.) < 0")
|
||||
elif n != (n|floor) then error("iterative_nth_root: argument \(n) is not an integer")
|
||||
elif n == 0 then error("iterative_nth_root(0): domain error")
|
||||
elif n < 0 then 1/iterative_nth_root(-n; precision)
|
||||
else [., ., (./n), n, 0] | _iterate
|
||||
end
|
||||
;
|
||||
10
Task/Nth-root/Jq/nth-root-2.jq
Normal file
10
Task/Nth-root/Jq/nth-root-2.jq
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def demo(x):
|
||||
def nth_root(n): log / n | exp;
|
||||
def lpad(n): tostring | (n - length) * " " + .;
|
||||
. as $in
|
||||
| "\(x)^(1/\(lpad(5))): \(x|nth_root($in)|lpad(18)) vs \(x|iterative_nth_root($in; 1e-10)|lpad(18)) vs \(x|iterative_nth_root($in; 0))"
|
||||
;
|
||||
|
||||
# 5^m for various values of n:
|
||||
"5^(1/ n): builtin precision=1e-10 precision=0",
|
||||
( (1,-5,-3,-1,1,3,5,1000,10000) | demo(5))
|
||||
11
Task/Nth-root/Jq/nth-root-3.jq
Normal file
11
Task/Nth-root/Jq/nth-root-3.jq
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
$ jq -n -r -f nth_root_machine_precision.jq
|
||||
5^(1/ n): builtin precision=1e-10 precision=0
|
||||
5^(1/ 1): 4.999999999999999 vs 5 vs 5
|
||||
5^(1/ -5): 0.7247796636776955 vs 0.7247796636776956 vs 0.7247796636776955
|
||||
5^(1/ -3): 0.5848035476425733 vs 0.5848035476425731 vs 0.5848035476425731
|
||||
5^(1/ -1): 0.2 vs 0.2 vs 0.2
|
||||
5^(1/ 1): 4.999999999999999 vs 5 vs 5
|
||||
5^(1/ 3): 1.709975946676697 vs 1.709975946676697 vs 1.709975946676697
|
||||
5^(1/ 5): 1.3797296614612147 vs 1.3797296614612147 vs 1.379729661461215
|
||||
5^(1/ 1000): 1.0016107337527294 vs 1.0016107337527294 vs 1.0016107337527294
|
||||
5^(1/10000): 1.0001609567433902 vs 1.0001609567433902 vs 1.0001609567433902
|
||||
17
Task/Nth-root/Julia/nth-root.julia
Normal file
17
Task/Nth-root/Julia/nth-root.julia
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
function nthroot(n::Integer, r::Real)
|
||||
r < 0 || n == 0 && throw(DomainError())
|
||||
n < 0 && return 1 / nthroot(-n, r)
|
||||
r > 0 || return 0
|
||||
x = r / n
|
||||
prevdx = r
|
||||
while true
|
||||
y = x ^ (n - 1)
|
||||
dx = (r - y * x) / (n * y)
|
||||
abs(dx) ≥ abs(prevdx) && return x
|
||||
x += dx
|
||||
prevdx = dx
|
||||
end
|
||||
end
|
||||
|
||||
@show nthroot.(-5:2:5, 5.0)
|
||||
@show nthroot.(-5:2:5, 5.0) - 5.0 .^ (1 ./ (-5:2:5))
|
||||
21
Task/Nth-root/Kotlin/nth-root.kotlin
Normal file
21
Task/Nth-root/Kotlin/nth-root.kotlin
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
// version 1.0.6
|
||||
|
||||
fun nthRoot(x: Double, n: Int): Double {
|
||||
if (n < 2) throw IllegalArgumentException("n must be more than 1")
|
||||
if (x <= 0.0) throw IllegalArgumentException("x must be positive")
|
||||
val np = n - 1
|
||||
fun iter(g: Double) = (np * g + x / Math.pow(g, np.toDouble())) / n
|
||||
var g1 = x
|
||||
var g2 = iter(g1)
|
||||
while (g1 != g2) {
|
||||
g1 = iter(g1)
|
||||
g2 = iter(iter(g2))
|
||||
}
|
||||
return g1
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val numbers = arrayOf(1728.0 to 3, 1024.0 to 10, 2.0 to 2)
|
||||
for (number in numbers)
|
||||
println("${number.first} ^ 1/${number.second}\t = ${nthRoot(number.first, number.second)}")
|
||||
}
|
||||
22
Task/Nth-root/Lambdatalk/nth-root.lambdatalk
Normal file
22
Task/Nth-root/Lambdatalk/nth-root.lambdatalk
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
{def root
|
||||
{def good-enough? {lambda {next guess tol}
|
||||
{< {abs {- next guess}} tol} }}
|
||||
{def improve {lambda {guess num deg}
|
||||
{/ {+ {* {- deg 1} guess}
|
||||
{/ num {pow guess {- deg 1}}}} deg} }}
|
||||
{def *root {lambda {guess num deg tol}
|
||||
{let { {guess guess} {num num} {deg deg} {tol tol}
|
||||
{next {improve guess num deg}}
|
||||
} {if {good-enough? next guess tol}
|
||||
then guess
|
||||
else {*root next num deg tol}} }}}
|
||||
{lambda {num deg tol}
|
||||
{*root 1.0 num deg tol} }}
|
||||
-> root
|
||||
|
||||
{root {pow 2 10} 10 0.1}
|
||||
-> 2.0473293223683866
|
||||
{root {pow 2 10} 10 0.01}
|
||||
-> 2.004632048354822
|
||||
{root {pow 2 10} 10 0.001}
|
||||
-> 2.000047868581671
|
||||
19
Task/Nth-root/Langur/nth-root.langur
Normal file
19
Task/Nth-root/Langur/nth-root.langur
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
writeln "operator"
|
||||
writeln( (7131.5 ^ 10) ^/ 10 )
|
||||
writeln 64 ^/ 6
|
||||
writeln()
|
||||
|
||||
# To make the example from the D language work, we set a low maximum for the number of digits after a decimal point in division.
|
||||
mode divMaxScale = 7
|
||||
|
||||
val .nthroot = f(.n, .A, .p) {
|
||||
var .x = [.A, .A / .n]
|
||||
while abs(.x[2]-.x[1]) > .p {
|
||||
.x = [.x[2], ((.n-1) x .x[2] + .A / (.x[2] ^ (.n-1))) / .n]
|
||||
}
|
||||
simplify .x[2]
|
||||
}
|
||||
|
||||
writeln "calculation"
|
||||
writeln .nthroot(10, 7131.5 ^ 10, 0.001)
|
||||
writeln .nthroot(6, 64, 0.001)
|
||||
21
Task/Nth-root/Liberty-BASIC/nth-root.basic
Normal file
21
Task/Nth-root/Liberty-BASIC/nth-root.basic
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
print "First estimate is: ", using( "#.###############", NthRoot( 125, 5642, 0.001 ));
|
||||
print " ... and better is: ", using( "#.###############", NthRoot( 125, 5642, 0.00001))
|
||||
print "125'th root of 5642 by LB's exponentiation operator is "; using( "#.###############", 5642^(1 /125))
|
||||
|
||||
print "27^(1 / 3)", using( "#.###############", NthRoot( 3, 27, 0.00001))
|
||||
print "2^(1 / 2)", using( "#.###############", NthRoot( 2, 2, 0.00001))
|
||||
print "1024^(1 /10)", using( "#.###############", NthRoot( 10, 1024, 0.00001))
|
||||
|
||||
wait
|
||||
|
||||
function NthRoot( n, A, p)
|
||||
x( 0) =A
|
||||
x( 1) =A /n
|
||||
while abs( x( 1) -x( 0)) >p
|
||||
x( 0) =x( 1)
|
||||
x( 1) =( ( n -1.0) *x( 1) +A /x( 1)^( n -1.0)) /n
|
||||
wend
|
||||
NthRoot =x( 1)
|
||||
end function
|
||||
|
||||
end
|
||||
3
Task/Nth-root/Lingo/nth-root-1.lingo
Normal file
3
Task/Nth-root/Lingo/nth-root-1.lingo
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
on nthRoot (x, root)
|
||||
return power(x, 1.0/root)
|
||||
end
|
||||
3
Task/Nth-root/Lingo/nth-root-2.lingo
Normal file
3
Task/Nth-root/Lingo/nth-root-2.lingo
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
the floatPrecision = 8 -- only about display/string cast of floats
|
||||
put nthRoot(4, 4)
|
||||
-- 1.41421356
|
||||
11
Task/Nth-root/Logo/nth-root.logo
Normal file
11
Task/Nth-root/Logo/nth-root.logo
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
to about :a :b
|
||||
output and [:a - :b < 1e-5] [:a - :b > -1e-5]
|
||||
end
|
||||
|
||||
to root :n :a [:guess :a]
|
||||
localmake "next ((:n-1) * :guess + :a / power :guess (:n-1)) / n
|
||||
if about :guess :next [output :next]
|
||||
output (root :n :a :next)
|
||||
end
|
||||
|
||||
show root 5 34 ; 2.02439745849989
|
||||
3
Task/Nth-root/Lua/nth-root.lua
Normal file
3
Task/Nth-root/Lua/nth-root.lua
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
function nroot(root, num)
|
||||
return num^(1/root)
|
||||
end
|
||||
24
Task/Nth-root/M2000-Interpreter/nth-root.m2000
Normal file
24
Task/Nth-root/M2000-Interpreter/nth-root.m2000
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
Module Checkit {
|
||||
Function Root (a, n%, d as double=1.e-4) {
|
||||
if n%=0 then Error "Division by zero: 1/0"
|
||||
if a<=0 then Error "Negative or zero number"
|
||||
if n%=1 then = a : exit
|
||||
Flush
|
||||
n2=1-1/n%:a/=n%:n%--:Push a
|
||||
{ Push 1: For i=1 to n% {Over 2 :Push Number*Number}
|
||||
Over 2 : Push n2*Number + a/Number
|
||||
Shift 2: Over 2, 2 :if Abs(Number-Number)>d Then loop
|
||||
Drop
|
||||
} Read a : = a
|
||||
}
|
||||
Print "square root single"
|
||||
Print root(1.3346767~, 2, 1.e-9)
|
||||
Print "square double"
|
||||
Print root(1.3346767, 2, 1.e-9)
|
||||
Print "square root decimal"
|
||||
Print root(1.3346767@, 2, 1.e-9)
|
||||
Print "internal square root, double"
|
||||
Print 1.3346767^(1/2)
|
||||
Print sqrt(1.3346767)
|
||||
}
|
||||
Checkit
|
||||
13
Task/Nth-root/MATLAB/nth-root-1.m
Normal file
13
Task/Nth-root/MATLAB/nth-root-1.m
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
function answer = nthRoot(number,root)
|
||||
|
||||
format long
|
||||
|
||||
answer = number / root;
|
||||
guess = number;
|
||||
|
||||
while not(guess == answer)
|
||||
guess = answer;
|
||||
answer = (1/root)*( ((root - 1)*guess) + ( number/(guess^(root - 1)) ) );
|
||||
end
|
||||
|
||||
end
|
||||
5
Task/Nth-root/MATLAB/nth-root-2.m
Normal file
5
Task/Nth-root/MATLAB/nth-root-2.m
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
>> nthRoot(2,2)
|
||||
|
||||
ans =
|
||||
|
||||
1.414213562373095
|
||||
5
Task/Nth-root/Maple/nth-root.maple
Normal file
5
Task/Nth-root/Maple/nth-root.maple
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
root(1728, 3);
|
||||
|
||||
root(1024, 10);
|
||||
|
||||
root(2.0, 2);
|
||||
1
Task/Nth-root/Mathematica/nth-root.math
Normal file
1
Task/Nth-root/Mathematica/nth-root.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
Root[A,n]
|
||||
13
Task/Nth-root/Maxima/nth-root.maxima
Normal file
13
Task/Nth-root/Maxima/nth-root.maxima
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
nth_root(a, n) := block(
|
||||
[x, y, d, p: fpprec],
|
||||
fpprec: p + 10,
|
||||
x: bfloat(a),
|
||||
eps: 10.0b0^-p,
|
||||
y: do (
|
||||
d: bfloat((a / x^(n - 1) - x) / n),
|
||||
if abs(d) < eps * x then return(x),
|
||||
x: x + d
|
||||
),
|
||||
fpprec: p,
|
||||
bfloat(y)
|
||||
)$
|
||||
17
Task/Nth-root/Metafont/nth-root.metafont
Normal file
17
Task/Nth-root/Metafont/nth-root.metafont
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
vardef mnthroot(expr n, A) =
|
||||
x0 := A / n;
|
||||
m := n - 1;
|
||||
forever:
|
||||
x1 := (m*x0 + A/(x0 ** m)) / n;
|
||||
exitif abs(x1 - x0) < abs(x0 * 0.0001);
|
||||
x0 := x1;
|
||||
endfor;
|
||||
x1
|
||||
enddef;
|
||||
|
||||
primarydef n nthroot A = mnthroot(n, A) enddef;
|
||||
|
||||
show 5 nthroot 34; % 2.0244
|
||||
show 0.5 nthroot 7; % 49.00528
|
||||
|
||||
bye
|
||||
78
Task/Nth-root/NetRexx/nth-root.netrexx
Normal file
78
Task/Nth-root/NetRexx/nth-root.netrexx
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
/*NetRexx program to calculate the Nth root of X, with DIGS accuracy. */
|
||||
class nth_root
|
||||
|
||||
method main(args=String[]) static
|
||||
if args.length < 2 then
|
||||
do
|
||||
say "at least 2 arguments expected"
|
||||
exit
|
||||
end
|
||||
x = args[0]
|
||||
root = args[1]
|
||||
if args.length > 2 then digs = args[2]
|
||||
|
||||
if root=='' then root=2
|
||||
if digs = null, digs = '' then digs=20
|
||||
numeric digits digs
|
||||
say ' x = ' x
|
||||
say ' root = ' root
|
||||
say 'digits = ' digs
|
||||
say 'answer = ' root(x,root,digs)
|
||||
|
||||
method root(x,r,digs) static --procedure; parse arg x,R 1 oldR /*assign 2nd arg-->r and rOrig. */
|
||||
/*this subroutine will use the */
|
||||
/*digits from the calling prog. */
|
||||
/*The default digits is 9. */
|
||||
R = r
|
||||
oldR = r
|
||||
if r=0 then do
|
||||
say
|
||||
say '*** error! ***'
|
||||
say "a root of zero can't be specified."
|
||||
say
|
||||
return '[n/a]'
|
||||
end
|
||||
|
||||
R=R.abs() /*use absolute value of root. */
|
||||
|
||||
if x<0 & (R//2==0) then do
|
||||
say
|
||||
say '*** error! ***'
|
||||
say "an even root can't be calculated for a" -
|
||||
'negative number,'
|
||||
say 'the result would be complex.'
|
||||
say
|
||||
return '[n/a]'
|
||||
end
|
||||
|
||||
if x=0 | r=1 then return x/1 /*handle couple of special cases.*/
|
||||
Rm1=R-1 /*just a fast version of ROOT-1 */
|
||||
oldDigs=digs /*get the current number of digs.*/
|
||||
dm=oldDigs+5 /*we need a little guard room. */
|
||||
ax=x.abs() /*the absolute value of X. */
|
||||
g=(ax+1)/r**r /*take a good stab at 1st guess. */
|
||||
-- numeric fuzz 3 /*fuzz digits for higher roots. */
|
||||
d=5 /*start with only five digits. */
|
||||
/*each calc doubles precision. */
|
||||
|
||||
loop forever
|
||||
|
||||
d=d+d
|
||||
if d>dm then d = dm /*double the digits, but not>DM. */
|
||||
numeric digits d /*tell REXX to use D digits. */
|
||||
old=0 /*assume some kind of old guess. */
|
||||
|
||||
loop forever
|
||||
_=(Rm1*g**R+ax)/R/g**rm1 /*this is the nitty-gritty stuff.*/
|
||||
if _=g | _=old then leave /*computed close to this before? */
|
||||
old=g /*now, keep calculation for OLD. */
|
||||
g=_ /*set calculation to guesstimate.*/
|
||||
end
|
||||
|
||||
if d==dm then leave /*found the root for DM digits ? */
|
||||
end
|
||||
|
||||
_=g*x.sign() /*correct the sign (maybe). */
|
||||
if oldR<0 then return _=1/_ /*root < 0 ? Reciprocal it is.*/
|
||||
numeric digits oldDigs /*re-instate the original digits.*/
|
||||
return _/1 /*normalize the number to digs. */
|
||||
11
Task/Nth-root/NewLISP/nth-root.l
Normal file
11
Task/Nth-root/NewLISP/nth-root.l
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(define (nth-root n a)
|
||||
(let ((x1 a)
|
||||
(x2 (div a n)))
|
||||
(until (= x1 x2)
|
||||
(setq x1 x2
|
||||
x2 (div
|
||||
(add
|
||||
(mul x1 (- n 1))
|
||||
(div a (pow x1 (- n 1))))
|
||||
n)))
|
||||
x2))
|
||||
13
Task/Nth-root/Nim/nth-root.nim
Normal file
13
Task/Nth-root/Nim/nth-root.nim
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import math
|
||||
|
||||
proc nthRoot(a: float; n: int): float =
|
||||
var n = float(n)
|
||||
result = a
|
||||
var x = a / n
|
||||
while abs(result-x) > 1e-15:
|
||||
x = result
|
||||
result = (1/n) * (((n-1)*x) + (a / pow(x, n-1)))
|
||||
|
||||
echo nthRoot(34.0, 5)
|
||||
echo nthRoot(42.0, 10)
|
||||
echo nthRoot(5.0, 2)
|
||||
12
Task/Nth-root/OCaml/nth-root.ocaml
Normal file
12
Task/Nth-root/OCaml/nth-root.ocaml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
let nthroot ~n ~a ?(tol=0.001) () =
|
||||
let nf = float n in let nf1 = nf -. 1.0 in
|
||||
let rec iter x =
|
||||
let x' = (nf1 *. x +. a /. (x ** nf1)) /. nf in
|
||||
if tol > abs_float (x -. x') then x' else iter x' in
|
||||
iter 1.0
|
||||
;;
|
||||
|
||||
let () =
|
||||
Printf.printf "%g\n" (nthroot 10 (7131.5 ** 10.0) ());
|
||||
Printf.printf "%g\n" (nthroot 5 34.0 ());
|
||||
;;
|
||||
18
Task/Nth-root/Objeck/nth-root.objeck
Normal file
18
Task/Nth-root/Objeck/nth-root.objeck
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
class NthRoot {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
NthRoot(5, 34, .001)->PrintLine();
|
||||
}
|
||||
|
||||
function : NthRoot(n : Int, A: Float, p : Float) ~ Float {
|
||||
x := Float->New[2];
|
||||
x[0] := A;
|
||||
x[1] := A / n;
|
||||
|
||||
while((x[1] - x[0])->Abs() > p) {
|
||||
x[0] := x[1];
|
||||
x[1] := ((n - 1.0) * x[1] + A / x[1]->Power(n - 1.0)) / n;
|
||||
};
|
||||
|
||||
return x[1];
|
||||
}
|
||||
}
|
||||
1
Task/Nth-root/Octave/nth-root-1.octave
Normal file
1
Task/Nth-root/Octave/nth-root-1.octave
Normal file
|
|
@ -0,0 +1 @@
|
|||
r = A.^(1./n)
|
||||
12
Task/Nth-root/Octave/nth-root-2.octave
Normal file
12
Task/Nth-root/Octave/nth-root-2.octave
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
function r = m_nthroot(n, A)
|
||||
x0 = A / n;
|
||||
m = n - 1;
|
||||
while(1)
|
||||
x1 = (m*x0 + A./ x0 .^ m) / n;
|
||||
if ( abs(x1-x0) < abs(x0 * 1e-9) )
|
||||
r = x1;
|
||||
return
|
||||
endif
|
||||
x0 = x1;
|
||||
endwhile
|
||||
endfunction
|
||||
8
Task/Nth-root/Octave/nth-root-3.octave
Normal file
8
Task/Nth-root/Octave/nth-root-3.octave
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
function r = m_nthroot(n, A)
|
||||
r = A / n;
|
||||
m = n - 1;
|
||||
do
|
||||
d = (A ./ r .^ m - r) / n;
|
||||
r+= d;
|
||||
until (abs(d) < abs(r * 1e-9))
|
||||
endfunction
|
||||
6
Task/Nth-root/Octave/nth-root-4.octave
Normal file
6
Task/Nth-root/Octave/nth-root-4.octave
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
m_nthroot(10, 7131.5 .^ 10)
|
||||
nthroot(7131.5 .^ 10, 10)
|
||||
m_nthroot(5, 34)
|
||||
nthroot(34, 5)
|
||||
m_nthroot(0.5, 7)
|
||||
nthroot(7, .5)
|
||||
2
Task/Nth-root/Oforth/nth-root.fth
Normal file
2
Task/Nth-root/Oforth/nth-root.fth
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Float method: nthroot(n)
|
||||
1.0 doWhile: [ self over n 1 - pow / over - n / tuck + swap 0.0 <> ] ;
|
||||
19
Task/Nth-root/Oz/nth-root.oz
Normal file
19
Task/Nth-root/Oz/nth-root.oz
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
declare
|
||||
fun {NthRoot NInt A}
|
||||
N = {Int.toFloat NInt}
|
||||
|
||||
fun {Next X}
|
||||
( (N-1.0)*X + A / {Pow X N-1.0} ) / N
|
||||
end
|
||||
in
|
||||
{Until Value.'==' Next A/N}
|
||||
end
|
||||
|
||||
fun {Until P F X}
|
||||
case {F X}
|
||||
of NX andthen {P NX X} then X
|
||||
[] NX then {Until P F NX}
|
||||
end
|
||||
end
|
||||
in
|
||||
{Show {NthRoot 2 2.0}}
|
||||
1
Task/Nth-root/PARI-GP/nth-root.parigp
Normal file
1
Task/Nth-root/PARI-GP/nth-root.parigp
Normal file
|
|
@ -0,0 +1 @@
|
|||
root(n,A)=A^(1/n);
|
||||
11
Task/Nth-root/PHP/nth-root.php
Normal file
11
Task/Nth-root/PHP/nth-root.php
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function nthroot($number, $root, $p = P)
|
||||
{
|
||||
$x[0] = $number;
|
||||
$x[1] = $number/$root;
|
||||
while(abs($x[1]-$x[0]) > $p)
|
||||
{
|
||||
$x[0] = $x[1];
|
||||
$x[1] = (($root-1)*$x[1] + $number/pow($x[1], $root-1))/$root;
|
||||
}
|
||||
return $x[1];
|
||||
}
|
||||
13
Task/Nth-root/PL-I/nth-root.pli
Normal file
13
Task/Nth-root/PL-I/nth-root.pli
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/* Finds the N-th root of the number A */
|
||||
root: procedure (A, N) returns (float);
|
||||
declare A float, N fixed binary;
|
||||
declare (xi, xip1) float;
|
||||
|
||||
xi = 1; /* An initial guess */
|
||||
do forever;
|
||||
xip1 = ((n-1)*xi + A/xi**(n-1) ) / n;
|
||||
if abs(xip1-xi) < 1e-5 then leave;
|
||||
xi = xip1;
|
||||
end;
|
||||
return (xi);
|
||||
end root;
|
||||
14
Task/Nth-root/Perl/nth-root-1.pl
Normal file
14
Task/Nth-root/Perl/nth-root-1.pl
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
use strict;
|
||||
|
||||
sub nthroot ($$)
|
||||
{
|
||||
my ( $n, $A ) = @_;
|
||||
|
||||
my $x0 = $A / $n;
|
||||
my $m = $n - 1.0;
|
||||
while(1) {
|
||||
my $x1 = ($m * $x0 + $A / ($x0 ** $m)) / $n;
|
||||
return $x1 if abs($x1 - $x0) < abs($x0 * 1e-9);
|
||||
$x0 = $x1;
|
||||
}
|
||||
}
|
||||
3
Task/Nth-root/Perl/nth-root-2.pl
Normal file
3
Task/Nth-root/Perl/nth-root-2.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
print nthroot(5, 34), "\n";
|
||||
print nthroot(10, 7131.5 ** 10), "\n";
|
||||
print nthroot(0.5, 7), "\n";
|
||||
29
Task/Nth-root/Phix/nth-root.phix
Normal file
29
Task/Nth-root/Phix/nth-root.phix
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
(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>
|
||||
|
||||
<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>
|
||||
|
||||
<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>
|
||||
<!--
|
||||
27
Task/Nth-root/Phixmonti/nth-root.phixmonti
Normal file
27
Task/Nth-root/Phixmonti/nth-root.phixmonti
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
def nthroot
|
||||
var n var y
|
||||
1e-15 var eps /# relative accuracy #/
|
||||
1 var x
|
||||
true
|
||||
while
|
||||
y x n 1 - power / x - n / var d
|
||||
x d + var x
|
||||
eps x * var e /# absolute accuracy #/
|
||||
d 0 e - < d e > or
|
||||
endwhile
|
||||
x
|
||||
enddef
|
||||
|
||||
def printList
|
||||
len for get print endfor
|
||||
enddef
|
||||
|
||||
10 1024 3 27 2 2 125 5642 4 16 stklen tolist
|
||||
|
||||
len 1 swap 2 3 tolist
|
||||
for
|
||||
var i
|
||||
i get swap i 1 + get rot var e var b
|
||||
"The " e "th root of " b " is " b 1 e / power " (" b e nthroot ")" 9 tolist
|
||||
printList drop nl
|
||||
endfor
|
||||
30
Task/Nth-root/Picat/nth-root.picat
Normal file
30
Task/Nth-root/Picat/nth-root.picat
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
go =>
|
||||
L = [[2,2],
|
||||
[34,5],
|
||||
[34**5,5],
|
||||
[7131.5**10],
|
||||
[7,0.5],
|
||||
[1024,10],
|
||||
[5642, 125]
|
||||
],
|
||||
foreach([A,N] in L)
|
||||
R = nthroot(A,N),
|
||||
printf("nthroot(%8w,%8w) %20w (check: %w)\n",A,N,R,A**(1/N))
|
||||
end,
|
||||
nl.
|
||||
|
||||
%
|
||||
% x^n = a
|
||||
%
|
||||
% Given a and n, find x (to Precision)
|
||||
%
|
||||
nthroot(A,N) = nthroot(A,N,0.000001).
|
||||
|
||||
nthroot(A,N,Precision) = X1 =>
|
||||
NF = N * 1.0, % float version of N
|
||||
X0 = A / NF,
|
||||
X1 = 1.0,
|
||||
do
|
||||
X0 := X1,
|
||||
X1 := (1.0 / NF)*((NF - 1.0)*X0 + (A / (X0 ** (NF - 1))))
|
||||
while( abs(X0-X1) > Precision).
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue