2

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 :)

Ankesh
  • 4,847
  • 4
  • 38
  • 76
  • Getting the square root is the hard bit. Check [this thread](http://stackoverflow.com/questions/3432412/calculate-square-root-of-a-biginteger-system-numerics-biginteger) for a possible solution. – Matthew Watson Apr 13 '13 at 15:20
  • 1
    I think you shouldn't ask questions like this [during the competition](https://code.google.com/codejam/contest/2270488/dashboard#s=p2). – svick Apr 13 '13 at 16:04

3 Answers3

2

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.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • You could always use a variation of the babylonian method to converge on the square root of the number: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method – Patashu Apr 13 '13 at 15:25
  • @Patashu: There are lots of ways to compute the square root of an integer. See the linked duplicate answers for some examples. – Eric Lippert Apr 13 '13 at 15:29
0

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.

taocp
  • 23,276
  • 10
  • 49
  • 62
0

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.

Kninnug
  • 7,992
  • 1
  • 30
  • 42