What is the sum of the first 500000 prime numbers?
I'll program it later tonight xD
The total of the first 500,000 prime numbers is 1,774,825,271,439 Calculated in 15.4 seconds on a mid-2010 Apple IMac using the following one line Mathematica program: Timing[ For[ n = p = t = 0, p < 500000, ++n, If[ PrimeQ[n], t += n; p += 1 ] ]; t ] Function, For [ start, test, incr, body ], excecutes start, then repeatedly evaluates body and incr until test fails to give True. Function, PrimeQ[n], returns True if n is a prime number. The program was tested and verified for the first 12 primes. Refer to the attachment.
How did we get such a different answer for 500000 primes, when even that C code I had gave me the same answer as you for the first 12 primes O.o http://ideone.com/djyVM
Mathematica is able to handle unlimited precision integer arithmetic on any machine. There may be a loss of significance with the larger prime numbers, however, this is just a guess. With a 32 bit machine the largest unsigned integer is 2^32-1 as I recall. This is a binary number equavalent to 4,294,967,295 decimal. If you know anyone who has Mathematica loaded up on their computer, then you should be able to copy and paste the program in and execute it.
@robtobey I think you're right, because I refactored the C program to use 64 bit signed ints and then it showed up as 1774825271439. (times out on the website though http://ideone.com/1HYoG
This is a good case for me to work on implementing that BigZ class in C.
Well good luck on your project.
Join our real-time social learning platform and learn together with your friends!