I wish to calculate the square root of a number which had more than 15 digits if possible for 100 digits it would be great

number like 100000000000000

Currently i am using C with integer as data type

int IsPerfectSquare(int number)
{

   if (number< 0)

   return 0;

   int root = (round(sqrt(number)));

    if(number == (root * root))

    {        
       return 1;    
    }    
    else
    {        
      return 0;    
    }
}

But does double or long double can hold 15 digits or more

Language is no bar. I can code in C# as well

Thanks :)

有帮助吗?

解决方案

In C# you can use decimal to get up to 29 digits and BigInteger to get arbitrarily many digits.

However, you'll need a different technique to tell if a 100 digit BigInteger is a perfect square, as there isn't an off-the-shelf "root" function that I'm aware of.

其他提示

The size of an integer in C is platform-dependant, so there is no guarantee of how many digits it can hold. You can look into the GNU Multiple Precision math library for dealing with big numbers in C.

In C++, You can check how many bigs long double can hold by using the following function:

 std::cout << std::numeric_limits<long double>::digits10 << std::endl;

If you really want to deal with numbers with 100 digits, you need to code your own BigInteger class.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top