I'm having a little problem with my simple prime number generator.
disclaimer: I'm working with absurdly large numbers - now over 2,000,000 digits which may be causing the instability in python, but I'm hoping this is a known issue that can be solved.
f = open('primes.txt', 'r') #this is a file containing all prime numbers less than n
#sequentially. I have another program that finds and adds
#more prime numbers to this file.
i = 1
for line in f: #Since the primes are separated by line breaks, I take the int of
#each line and multiply them together and stores the massive number
#in a variable called i
i = i*int(line)
f.close()
i = i+1 #since I know that every prime up to a certain point is a factor of
#i, I can add or subtract 1 to guarantee a number that has no prime
#factors (and therefore no factors)
print(i) #prints i to the terminal followed by 3 linebreaks
print("\n\n\n")
h = str(i) #I want to ultimately write i to a text file, but the write command
#doesn't take integer inputs
print(len(h)) #prints how large my new prime is
f = open('largestprime.txt', 'w') #opens a new file and overwrites what's currently inside
if int(h) == i: #I wrote this statement as a check when I started noticing something
#unusual was going on. It basically checks to make sure that the
#string h is the same thing as the integer i before writing to file.
f.write(h)
else:
f.write("Something Hinky's Going On")
input() #Waits for an enter before closing the terminal
It seems pretty straightforward to me, but the number it prints to the screen and the number it writes to the file are different every time. Here's the latest example:
-the terminal printed:
6690896934...(2,009,810 other digits)...0355781011 (total of 2,009,830 digits)
-while the file now contains:
5562116702...(2,007,021 other digits)...6916126637 (total of 2,007,041 digits)
The file is missing more than 2,000 digits! Am I making some mistake in writing to the file? Is there a known bug in python when writing absurdly long strings to file?
Any help would be appreciated. Thanks!