user8866053
user8866053

Reputation:

How Can I Fix An Overflow Error From the Calculation In Python 3?

I created a program to calculate primes with mill's constant, but anyway, it results in huge computations being done. For instance, 1.306... ** 5661

It throws an overflow error. How can I fix this? I tried xrange, and it failed to help me because that no longer exists in python 3. I don't know how to reduce it.

Can anyone give me some help? Thanks a lot!

Edit: here's the code:

theta = 1.3063778838630806904686144926026

bottom = int(input("lower bound: "))
top = int(input("high bound: "))

for i in range(0,top + 1):
    print(round(theta ** (3**i))) # causes the error

Upvotes: 0

Views: 1040

Answers (2)

PM 2Ring
PM 2Ring

Reputation: 55469

Here's how to calculate Mill's primes using integers. First you need to write Mill's constant as a fraction. I used one of the values from the Wikipedia article.

num, den = 1551795687, 1187861266
print(num / den)

for i in range(1, 8):
    e = 3 ** i
    n = num ** e
    d = den ** e
    print(i, n // d)

output

1.3063778838630806
1 2
2 11
3 1361
4 2521008887
5 16022236204009819034551083884
6 4113101149215105495247660946168530631843333312378291569324941703732418013747202413154
7 69583804376962776892757521964751417769589800913915250464742380681561387050414758147961918413247296927859465626141517084928624751186191429632740787663513270579366994745400890812584434492059975056388739246886951607326825627525396066637918379217513934013930

To perform more accurate calculations you'll need to use a better starting fraction, but that will cause n and d to grow even more rapidly.

Upvotes: 1

user8866053
user8866053

Reputation:

Thank you, @PM 2Ring and @Blurp! You helped me a lot by pointing out the decimal module, which was exactly what I needed! It turns out that 559397567061773305900... is prime!

Upvotes: 0

Related Questions