tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View 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})"

View 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)