0

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
  • Post your code along with the question and it will help others see whats the problem with your question – Ram Nov 27 '17 at 03:41
  • I just posted the code –  Nov 27 '17 at 03:42
  • 1
    See here: https://stackoverflow.com/questions/20201706 –  Nov 27 '17 at 03:46
  • Surely you mean [Mill's constant](https://en.wikipedia.org/wiki/Mills%27_constant)? Miller is a different prime number researcher. – PM 2Ring Nov 27 '17 at 03:46
  • Blurp I'm sorry to be stupid, but how does this help me? I don't fully understand the answer given. –  Nov 27 '17 at 03:48
  • PM 2Ring I do, thanks. –  Nov 27 '17 at 03:50
  • 3
    `range` in Python 3 behaves like Python 2's `xrange`. But that's not your problem. The numbers produced by Mill's formula rapidly become enormous, far too big to fit in a float. You could use the `decimal` module instead, to get a few more values before you run out of RAM. Another option is to do the calculation using rational numbers: express Mill's constant as a fraction, and use `//` floor division to divide the numerator by the denominator, which will give you integer answers. – PM 2Ring Nov 27 '17 at 03:52
  • @PM2Ring 2Ring could you be slightly more specific? When using floor division, I get all 1s. –  Nov 27 '17 at 03:55

2 Answers2

1

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.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
0

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!