Project Euler 7: The 10001st Prime Number
This mission can be solved by calculating lots of prime numbers. However, we do not want to put extra effort into our solutions, do we? After a bit of searching, I found a list of the 100000 first prime numbers (100008, actually). I saved this file. Unfortunately, it is not in an optimal format. I wrote a short Perl script that creates a new file, in which each prime number is on a separate line:
1 2 3 4 5 6 7 8 9 10 11 12 13 | # read the file from utm.edu open( PRIMES, '<', 'dat/100000.txt' ) or die $!; # this is the file that we will write to open( TO, '>', 'dat/primes-100000-first.txt' ) or die $!; while ( <PRIMES> ) { # for each line for ( split / +/ ) { # split, separating by spaces, and for each piece if ( m/^\d+$/ ) { # if it is a number print TO "$_\n"; # print the number to the file } } } close( TO ); close( PRIMES ); |
Of course, you need to replace the filenames.
Now, it is a piece of cake to find the 10001st prime number. Just do:
head -10001 dat/primes-100000-first.txt | tail -1
Which finds the 10001 first primes and outputs the last one of them.
Or you could open it in notepad and start counting rows.

[...] the post about Project Euler 7, I described how to obtain a list of primes. These are not enough: the last one is 1,299,827, and [...]
Pingback by Project Euler 10: Sum of First Primes -- Timblog — June 25, 2008 @ 2:36 pm