Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the nth root of big numbers in C++?

Is there a C++ library that can take nth roots of big numbers (numbers than can't fit in an unsigned long long)?

like image 224
Omar Saber Avatar asked Apr 08 '10 05:04

Omar Saber


People also ask

How do you find the 7th root?

√7 = 2.645. Thus, the square root of 7, √7 is approximately equal to 2.645.


1 Answers

It depends how much bigger than 2^64 you want to go, I guess. Just using doubles is good to about 1 part in 10^9. I wrote a test program in C:

#include <stdio.h>
#include <math.h>

int main(int argc, char **argv)
{
    unsigned long long x;
    double dx;
    int i;

    //make x the max possible value
    x = ~0ULL;
    dx = (double)x;
    printf("Starting with dx = %f\n", dx);
    //print the 2th to 20th roots
    for (i = 2; i < 21; i++)
    {
        printf("%dth root %.15f\n", i, pow(dx, 1.0/i));
    }
    return 0;
}

which produced the following output:

Starting with dx = 18446744073709551616.000000
2th root 4294967296.000000000000000
3th root 2642245.949629130773246
4th root 65536.000000000000000
5th root 7131.550214521852467
6th root 1625.498677215435691
7th root 565.293831000991759
8th root 256.000000000000000
9th root 138.247646578215154
10th root 84.448506289465257
11th root 56.421840319745364
12th root 40.317473596635935
13th root 30.338480458853493
14th root 23.775908626191171
15th root 19.248400577313866
16th root 16.000000000000000
17th root 13.592188707483222
18th root 11.757875938204789
19th root 10.327513583579238
20th root 9.189586839976281

Then I compared with Wolfram Alpha for each root to get the error I quoted above.

Depending on your application, perhaps this will be good enough.

like image 64
mtrw Avatar answered Sep 21 '22 17:09

mtrw