0

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!

Ryan Zaugg
  • 39
  • 2
  • Can you do `f.close()` at the end? (If you used the appropriate `with` construct, this problem wouldn't happen.) – alani Sep 12 '20 at 21:14
  • Use long instead of int https://stackoverflow.com/a/538583/806876 – pygeek Sep 12 '20 at 21:15
  • Does this answer your question? [Handling very large numbers in Python](https://stackoverflow.com/questions/538551/handling-very-large-numbers-in-python) – pygeek Sep 12 '20 at 21:17
  • And just as a side note. h is not necessarily prime. It's just not divisible by any of the primes in the file. It could be divisible by a larger prime. – Frank Yellin Sep 12 '20 at 21:27
  • @pygeek, yes, we totally agree. I was asking for your confirmation of my understanding, and was never questioning anything you had said. But in fact, the OP is dealing with such extremely large values, that we're well past any issue with ability to represent a large enough number or not. – CryptoFool Sep 12 '20 at 21:31
  • Sorry to get off track...the crux of the problem is that `print(i)` and `f.write(str(i))` are outputting different sequences of digits, right? In both cases, we're getting over 2 million digits, but we're getting both a different value and a different number of digits in each case. Do I summarize the problem correctly here? If so, I have absolutely no idea what's going on, but it sure sounds like some sort of capability boundary is being crossed here. How long does it take to run this program? How many prime values are in the input file? – CryptoFool Sep 12 '20 at 21:35

0 Answers0