# Nth root

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

A minor optimization would be to calculate the successive *int(n-1)* square roots of a number, then raise the result to the power of *2\*\*(int(n-1) / n)*.

```ruby
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.02439745849988504251081724554193741911462170107
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trizen.gitbook.io/sidef-lang/programming_tasks/n/nth_root.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
