Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
13
Task/Nth-root/Python/nth-root-1.py
Normal file
13
Task/Nth-root/Python/nth-root-1.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
from decimal import Decimal, getcontext
|
||||
|
||||
def nthroot (n, A, precision):
|
||||
getcontext().prec = precision
|
||||
|
||||
n = Decimal(n)
|
||||
x_0 = A / n #step 1: make a while guess.
|
||||
x_1 = 1 #need it to exist before step 2
|
||||
while True:
|
||||
#step 2:
|
||||
x_0, x_1 = x_1, (1 / n)*((n - 1)*x_0 + (A / (x_0 ** (n - 1))))
|
||||
if x_0 == x_1:
|
||||
return x_1
|
||||
3
Task/Nth-root/Python/nth-root-2.py
Normal file
3
Task/Nth-root/Python/nth-root-2.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
print nthroot(5, 34, 10)
|
||||
print nthroot(10,42, 20)
|
||||
print nthroot(2, 5, 400)
|
||||
111
Task/Nth-root/Python/nth-root-3.py
Normal file
111
Task/Nth-root/Python/nth-root-3.py
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
'''Nth Root'''
|
||||
|
||||
from decimal import Decimal, getcontext
|
||||
from operator import eq
|
||||
|
||||
|
||||
# nthRoot :: Int -> Int -> Int -> Real
|
||||
def nthRoot(precision):
|
||||
'''The nth root of x at the given precision.'''
|
||||
def go(n, x):
|
||||
getcontext().prec = precision
|
||||
dcn = Decimal(n)
|
||||
|
||||
def same(ab):
|
||||
return eq(*ab)
|
||||
|
||||
def step(ab):
|
||||
a, b = ab
|
||||
predn = pred(dcn)
|
||||
return (
|
||||
b,
|
||||
reciprocal(dcn) * (
|
||||
predn * a + (
|
||||
x / (a ** predn)
|
||||
)
|
||||
)
|
||||
)
|
||||
return until(same)(step)(
|
||||
(x / dcn, 1)
|
||||
)[0]
|
||||
return lambda n: lambda x: go(n, x)
|
||||
|
||||
|
||||
# --------------------------TEST---------------------------
|
||||
def main():
|
||||
'''Nth roots at various precisions'''
|
||||
|
||||
def xShow(tpl):
|
||||
p, n, x = tpl
|
||||
return rootName(n) + (
|
||||
' of ' + str(x) + ' at precision ' + str(p)
|
||||
)
|
||||
|
||||
def f(tpl):
|
||||
p, n, x = tpl
|
||||
return nthRoot(p)(n)(x)
|
||||
|
||||
print(
|
||||
fTable(main.__doc__ + ':\n')(xShow)(str)(f)(
|
||||
[(10, 5, 34), (20, 10, 42), (30, 2, 5)]
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
# -------------------------DISPLAY-------------------------
|
||||
|
||||
# fTable :: String -> (a -> String) ->
|
||||
# (b -> String) -> (a -> b) -> [a] -> String
|
||||
def fTable(s):
|
||||
'''Heading -> x display function -> fx display function ->
|
||||
f -> xs -> tabular string.
|
||||
'''
|
||||
def go(xShow, fxShow, f, xs):
|
||||
ys = [xShow(x) for x in xs]
|
||||
w = max(map(len, ys))
|
||||
return s + '\n' + '\n'.join(map(
|
||||
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
|
||||
xs, ys
|
||||
))
|
||||
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
|
||||
xShow, fxShow, f, xs
|
||||
)
|
||||
|
||||
|
||||
# -------------------------GENERIC-------------------------
|
||||
|
||||
# rootName :: Int -> String
|
||||
def rootName(n):
|
||||
'''English ordinal suffix.'''
|
||||
return ['identity', 'square root', 'cube root'][n - 1] if (
|
||||
4 > n or 1 > n
|
||||
) else (str(n) + 'th root')
|
||||
|
||||
|
||||
# pred :: Enum a => a -> a
|
||||
def pred(x):
|
||||
'''The predecessor of a value. For numeric types, (- 1).'''
|
||||
return x - 1
|
||||
|
||||
|
||||
# reciprocal :: Num -> Num
|
||||
def reciprocal(x):
|
||||
'''Arithmetic reciprocal of x.'''
|
||||
return 1 / x
|
||||
|
||||
|
||||
# until :: (a -> Bool) -> (a -> a) -> a -> a
|
||||
def until(p):
|
||||
'''The result of repeatedly applying f until p holds.
|
||||
The initial seed value is x.
|
||||
'''
|
||||
def go(f, x):
|
||||
v = x
|
||||
while not p(v):
|
||||
v = f(v)
|
||||
return v
|
||||
return lambda f: lambda x: go(f, x)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue