#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Nth_root
#

func nthroot(n, a, precision=1e-5) {
  var x = 1;
  var prev = 0;
  while ((prev-x).abs > precision) {
    prev = x;
    x = (((n-1)*prev + a/(prev**(n-1))) / n -> float);
  }
  return x;
}
 
say nthroot(5,34);  # => 2.02439745850103408259981783529791

#
## A faster approach
#
func nthroot_fast(n, a, precision=1e-5) {
  { a = nthroot(2, a, precision) } * int(n-1);
  a ** (2**int(n-1) / n);
}

say nthroot_fast(5, 34, 1e-64);  # => 2.02439745849988504251081724554194
