Square root of a 100 digit number in C++

家住魔仙堡 提交于 2019-12-01 18:52:18

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!