简体   繁体   中英

Square root of a 100 digit number in C++

'unsigned long long' can solve upto 15 digits.

Is there a way to find square-root of a 100 digit number ?

You could also use Boost.Multiprecision library. This library provides wrappers for some popular multiprecision implementations.

#include <iostream>
#include <string>
#include <utility>

#include <boost/multiprecision/mpfr.hpp>

int main()
{
    std::string s(100, '0');
    s.at(0) = '1';
    boost::multiprecision::mpfr_float_100 f(std::move(s));
    boost::multiprecision::mpfr_float_100 sqrt = boost::multiprecision::sqrt(f);
    std::cout << sqrt.str() << std::endl;

    return 0;
}

Definitely. One easy way would be to use the GNU multi-precision library's mpz_sqrt() function.

This question isnt really related to C++, but here is a list of methods you can use http://en.wikipedia.org/wiki/Methods_of_computing_square_roots

Depending on if its homework or not you might be able to use a premade lib to handle bignums

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM