2

I need to get the ceiling of a square root, such as 10**10001 (10^10001). If I do:

from math import sqrt
print(int(sqrt(10**10001))+1)

I look, and it gives me an OverflowError. More precisely, a

Traceback (most recent call last)
  File <stdin>, line 1, in <module>
OverflowError: int too large to convert to float.

I need a integer for an answer. The OverflowError is coming from the math.sqrt(x) function. If anybody can help, it would be appreciated.

Lucas Urban
  • 627
  • 4
  • 15
  • That's 1 with 10 thousand zeros. Why do you need a number that large? – Mike67 Oct 31 '20 at 00:52
  • I need to calculate extremely large numbers to see if they are prime. – Lucas Urban Oct 31 '20 at 01:18
  • Does this answer your question? [Using the sqrt function of math module for long numbers in python](https://stackoverflow.com/questions/28150824/using-the-sqrt-function-of-math-module-for-long-numbers-in-python) – GordonAitchJay Oct 31 '20 at 03:59
  • 2
    This seems to be a duplicate of https://stackoverflow.com/questions/28150824/using-the-sqrt-function-of-math-module-for-long-numbers-in-python. Try `import decimal` `print(int(decimal.Decimal(10**10001).sqrt())+1)` – GordonAitchJay Oct 31 '20 at 04:00
  • 1. The question does not answer it. I am using regular integers. – Lucas Urban Oct 31 '20 at 12:40
  • 2. Your method doesn't work! It gives me the round logarithm of the answer! (317) – Lucas Urban Oct 31 '20 at 12:41
  • 1
    The question says ``print(int(sqrt(10*10001))+1)``, which is very much computable and prints ``317``. Is that a typo? – MisterMiyagi Nov 06 '20 at 14:12
  • `math.sqrt` works with `float`. Given an `int`, it will convert some `int` to a `float` at some point. Python's `float` gets as large as 1.797e308, so 1e10001 is way too big for it (just as the error message says). You will need a different data type and a sqrt function that will work with that data type. I'm a bit confused why you insist on outputting an integer because sqrt(1e10001)=sqrt(10*1e10000)=sqrt(10)*1e5000 doesn't seem like an integer. – BatWannaBe Nov 06 '20 at 14:18

1 Answers1

-2

the math module has a ceil and floor function

try print(math.ceil(math.sqrt(NUMBER)))