Reputation: 7204
In the second line before the out statement the answer is -1, buy due to my print it should be the answer: 618970019642690137449562111, which is 2**89-1, instead i'm getting -1
I can't for the life of me figure out what the bug is, could someone enlighten me? Why am i getting -1 there instead of 618970019642690137449562111
Here is the code, the error with the "-1" is two lines right before the "[Out 778:]" statement ( you have to scroll right to see it). It should keep climbing the powers of 2 tree minus 1 but it gives the answer -1 which is incorrect:
import numpy as np
import gmpy2
mersenne=np.array([3, 5, 7, 13, 17, 19, 31, 61,
89, 107, 127, 521, 607, 1279, 2203,
2281, 3217, 4253, 4423, 9689, 9941,
11213, 19937, 21701, 23209, 44497, 86243,
110503, 132049, 216091, 756839, 859433,
1257787, 1398269, 2976221, 3021377, 6972593,
13466917, 20996011, 24036583, 25964951, 30402457,
32582657, 37156667, 42156667, 42643801,
43112609, 57885161, 74207281, 77232917, 82589933])
def XploderMODex(s):
s=gmpy2.mpz(s)
ss=gmpy2.mpz(2**s-1)
perfect=True
count=gmpy2.mpz(0)
for x in mersenne[mersenne<s]:
print(ss, x, 2**x-1)
if ss%(2**x-1) == 0:
print(ss%(2**x-1), ss, x, mersenne[count])
return False,count
count+=1
return True,count
In [778]: XploderMODex(1009)
5486124068793688683255936251187209270074392635932332070112001988456197381759672947165175699536362793613284725337872111744958183862744647903224103718245670299614498700710006264535590197791934024641512541262359795191593953928908168990292758500391456212260452596575509589842140073806143686060649302051520511 3 7
5486124068793688683255936251187209270074392635932332070112001988456197381759672947165175699536362793613284725337872111744958183862744647903224103718245670299614498700710006264535590197791934024641512541262359795191593953928908168990292758500391456212260452596575509589842140073806143686060649302051520511 5 31
5486124068793688683255936251187209270074392635932332070112001988456197381759672947165175699536362793613284725337872111744958183862744647903224103718245670299614498700710006264535590197791934024641512541262359795191593953928908168990292758500391456212260452596575509589842140073806143686060649302051520511 7 127
5486124068793688683255936251187209270074392635932332070112001988456197381759672947165175699536362793613284725337872111744958183862744647903224103718245670299614498700710006264535590197791934024641512541262359795191593953928908168990292758500391456212260452596575509589842140073806143686060649302051520511 13 8191
5486124068793688683255936251187209270074392635932332070112001988456197381759672947165175699536362793613284725337872111744958183862744647903224103718245670299614498700710006264535590197791934024641512541262359795191593953928908168990292758500391456212260452596575509589842140073806143686060649302051520511 17 131071
5486124068793688683255936251187209270074392635932332070112001988456197381759672947165175699536362793613284725337872111744958183862744647903224103718245670299614498700710006264535590197791934024641512541262359795191593953928908168990292758500391456212260452596575509589842140073806143686060649302051520511 19 524287
5486124068793688683255936251187209270074392635932332070112001988456197381759672947165175699536362793613284725337872111744958183862744647903224103718245670299614498700710006264535590197791934024641512541262359795191593953928908168990292758500391456212260452596575509589842140073806143686060649302051520511 31 2147483647
5486124068793688683255936251187209270074392635932332070112001988456197381759672947165175699536362793613284725337872111744958183862744647903224103718245670299614498700710006264535590197791934024641512541262359795191593953928908168990292758500391456212260452596575509589842140073806143686060649302051520511 61 2305843009213693951
5486124068793688683255936251187209270074392635932332070112001988456197381759672947165175699536362793613284725337872111744958183862744647903224103718245670299614498700710006264535590197791934024641512541262359795191593953928908168990292758500391456212260452596575509589842140073806143686060649302051520511 89 -1
0 5486124068793688683255936251187209270074392635932332070112001988456197381759672947165175699536362793613284725337872111744958183862744647903224103718245670299614498700710006264535590197791934024641512541262359795191593953928908168990292758500391456212260452596575509589842140073806143686060649302051520511 89 89
Out[778]: (False, mpz(8))
Here is a simpler example, same thing when i get to mersenne 89:
In [780]: XploderMODex(101)
2535301200456458802993406410751 3 7
2535301200456458802993406410751 5 31
2535301200456458802993406410751 7 127
2535301200456458802993406410751 13 8191
2535301200456458802993406410751 17 131071
2535301200456458802993406410751 19 524287
2535301200456458802993406410751 31 2147483647
2535301200456458802993406410751 61 2305843009213693951
2535301200456458802993406410751 89 -1
0 2535301200456458802993406410751 89 89
Upvotes: 0
Views: 43
Reputation: 649
It's an overflow error. You can use python's int
which doesn't overflow. You can add dtype
to your mersenne
array to solve it:
mersenne=np.array([3, 5, 7, 13, 17, 19, 31, 61,
89, 107, 127, 521, 607, 1279, 2203,
2281, 3217, 4253, 4423, 9689, 9941,
11213, 19937, 21701, 23209, 44497, 86243,
110503, 132049, 216091, 756839, 859433,
1257787, 1398269, 2976221, 3021377, 6972593,
13466917, 20996011, 24036583, 25964951, 30402457,
32582657, 37156667, 42156667, 42643801,
43112609, 57885161, 74207281, 77232917, 82589933], dtype='O')
Upvotes: 1