Riddle 10: Summation of primes

Search for a command to run...

No comments yet. Be the first to comment.
In this loose series I plan to solve the math riddles from the Euler project one by one.
The Task A Pythagorean triplet is a set of three natural numbers, a < b < c, for which a^2 + b^2 = c^2. For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. Solutio...
In our previous post, we introduced how to utilize Discrete Event Simulation (DES) in PicoLisp. Now, let's explore another application: a railroad model simulation that includes tracks and trains. The visualization is ASCII-based, so feel free to unl...

The Dining Philosophers

In this post, we will introduce the concept of co-routines and how they are handled in PicoLisp. This will be the foundation for the subsequent posts on Discrete Event Simulation and finally a couple of examples, including an ASCII model railway simu...

One of the many nice things of PicoLisp is that it comes with a built-in database. This makes it easy to collect and store our own data, for example via a mobile app. But not all data is best represented in tables and lists: sometimes a colorful litt...

In the last two posts, we have built a little app that shows the current location and displays the nearest ice cream shops: https://picolisp-explored.com/an-openstreetmap-app-written-in-picolisp-pt-1 https://picolisp-explored.com/an-openstreetmap-ap...

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
This task is so easy that it is almost not worth its own post. The reason is because it is identical to Task 7, "10001st prime" except that we need to add one single line.
What we need to do is:
For 1., we can re-use the code from Task 7 without modification. The only challenge is that instead of 10.000 prime numbers, we need to calculate around 150.000 prime numbers. Then we sum all list items up with apply:
(load "./Task_7_10001_Prime_final.l")
(prinl (apply + (sieve 2000000)))
Let's compare the computation time for Task 7 and Task 10.
: (bench (apply + (sieve 110000)))
0.114 sec
-> 544815056
: (bench (apply + (sieve 2000000)))
5.135 sec
-> 142913828922
The computation time has increased from 0.1 s to 5.1 s, but is still much faster than the 60 seconds that the task description allows.
You can find the "code" for the finished task here.