views:

4403

answers:

12

As a Christmas gift I have written a small program in Java to calculate primes. My intention was to leave it on all night, calculating the next prime and writing it to a .txt file. In the morning I would kill the program and take the .txt file to my friend for Christmas.

Is there anything I should be worried about? Bear in mind that this is true beginner Ziggy you are talking to, not some smart error checking ASM guy.

EDIT More specifically, since I will be leaving this program on all night counting primes, is there any chance at all that I will encounter some kind of memory related error? Like, stacks crushing heaps or dogs and cats sleeping together?

EDIT even more specifically, is there a line of code I could put in to stop the printing of lines when the file's size is 4GB? Just to be safe?

EDIT: success: after leaving it on all night I got no more than 13 KB of primes, The highest I got was 22947217, which is like tens of thousands of primes. Success!

A: 

Only as to the size of the place you're storing the text file on disk.

And, if you're not writing it all right away, your memory + virtual memory.

p.campbell
+2  A: 

Technically, there is no limit except that which the file system places on you. However, Notepad is really cranky about opening obscenely large files.

toast
+11  A: 

I would recommend sending an SMS message for each prime you calculate. Your friend would like that much better than a bunch of paper. Plus he can be updated much more often.

You can send them free from here:.

Just have to get past the captcha.

John Sonmez
@Job B: at first....
Ólafur Waage
Yes, after the first several hundred million primes, it should start to get reasonable :)
Jon B
Also, SMS has a text limit of 160 characters. So any prime larger than that is not possible.
Ólafur Waage
Ah, but then you can just break them across several messages :)
Andrew Rollings
+1  A: 

If memory serves, FAT32 has a 4gig file limit size.

Stephen Wrighton
+2  A: 

You might consider tracking the number of bytes you write to each file and switching to a new one after some number of bytes. You might also provide a viewer for your files so your friend can see his gift more easily. :)

Jim Blizard
A: 

Ziggy--

I love this: "not some smart error checking ASM guy." You are describing all of us!

Have plenty of disk space and write away! As previously mentioned, be sure the editor used to open your file can open very large files.

Happy holidays, true beginner Ziggy.

rp
+5  A: 

There's plenty of limits, though none of them are intrinsic to .txt files:

  • Windows 9x Notepad won't open a file > 64KB.
  • Windows NT/2k/etc's Notepad has no limit, but tends to choke and lock up on multi-megabyte files. You also need to remember most text editors are dumb and try to read the entire file into RAM.
  • Lots of software is limited to 2GB or 4GB files depending on whether they use signed or unsigned ints - as someone already mentioned FAT32 is guilty of this.
Ant P.
A: 

How about saving some CPU cycles and just downloading a pre-computed list of primes? Or is it more "the thought that counts"? :)

Marc Novakowski
definitely the thought that counts: it's Christmas my friend!
Ziggy
I'm so glad I'm not on your Christmas list :)
Andrew Rollings
A: 

According to http://www.prime-numbers.org/ there are 455,042,511 prime numbers up to 10,000,000,000. So you could risk hitting a 4GB file limit, and should design your program to spawn multiple files.

To answer your edit - how to stop printing when the file size hits a certain point:

You could either count the number of lines you've printed or read the actual size of the file. In either case, I'd do this by wrapping your code in a while loop like while (lines++ < max) { ... }

Jon B
A: 

What about just creating one file for each prime number and then use the filename to display the number?

l3dx
you would hit the directory item limit pretty quickly then.
Ólafur Waage
That depends on the filesystem. But that would be a whole new question.
Marc
+1  A: 

More than likely you are using an algorithm that is slow. As the primes get larger your program will be taking longer and longer to calculate a single prime. If you let it run over night the text file is not going to be very large in the morning. I'd be impressed if it's over a couple of megs.

Cadoo
Nice! Actually I just timed the program to see if I could guess how big the text file would be by tomorrow. About half a minute it I was like, "oh wait, the increase in size is going to get smaller and smaller: I have little to worry about".
Ziggy
A: 

Somehow I doubt that when having your program run overnight, that the filesize will be a problem, considering that it will take longer to find primes as numbers get bigger. Just make sure you clean up or you might eat up all your RAM.

To answer your question: Theoretically, the filesystem restricts file size. However, a lot of text editors crash (vim does not) when loading big files (> 100 MB), because they try to fit it in one buffer.

To sum up, consider splitting up your files into chunks the weakest link (text editors) can handle.

Kafka