4

'unsigned long long' can solve upto 15 digits.

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

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
user2278992
  • 93
  • 1
  • 2
  • 1
    I know this is probably not what you're looking for, but in case you don't need an exact result, you can just use `double`. – Detheroc Apr 14 '13 at 11:19
  • 2
    @Detheroc that would be a good first step to compute the real value with an algorithm like the newton method. – didierc Apr 14 '13 at 11:51

3 Answers3

4

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;
}
awesoon
  • 32,469
  • 11
  • 74
  • 99
3

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

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
3

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

krs
  • 4,096
  • 19
  • 22