def isPrime(num):
s=0
for j in range (1, num):
if(num%j==0):
s=s+1
if(s>1):
break
return(s)
a = 10**20;
b = 10**400;
for i in xrange(a, b):
if(isPrime(i)==1 and isPrime(sum(int(x) for x in str(i)))==1):
print('Sum of all digits of', i, 'is', sum(int(x) for x in str(i)))
My objective is to output all the numbers within 10^20 and 10^400 whose digits add up to a prime number in order to answer the question, "How many integers in the range [10^20, 10^400] exist such that the sum of their digits is a prime number?"
While googling around, I read that range would overflow and that xrange would be more efficient. When xrange was used, the error "OverflowError: Python int too large to convert to C long;" occurs.
How can I output the answer without error?
A question was asked similar to this for python 2.x it seems: Handling big numbers in code this may help for smaller numbers...
In general, this answer via Quora is quite informative: https://www.quora.com/How-large-can-Python-handle-big-number like others have said, it will depend on how much memory you have available.
From the Quora article:
The real limit depends on the amount of memory Python has access to. If it had, say, 1GB at its disposal, that would amount to approximately 80000000008000000000 bits. That would, therefore, let you have numbers all the way up to 2800000000028000000000. If somehow we could use the Titan supercomputer’s 600+ TiB of CPU memory specifically for making numbers, we could go all the way up to about 22522252. That's a lot.