During this activity, students should be able to:
This activity helps the student develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.
Individually, solve the following problem. This problem is an adaptation of problem 10 Summation of primes from Project Euler.
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below five million.
Spoiler alert: The correct answer is 838,596,693,108.
Write a Clojure function that allows you to compute this solution in parallel (with the help of Clojure’s pmap
function), using a computer system with a dual-core CPU or better. The name of the function must be prime-sum
and it must be contained in a namespace called parallel
(thus, your source file should be called parallel.clj
). The function should take as an argument the number of threads to use during the computation.
Usage examples:
user=> (parallel/prime-sum 1) 838596693108 user=> ;;; Same result as above, except computed faster! user=> (parallel/prime-sum 2) 838596693108 user=> ;;; Using all available CPU cores. user=> (def n (.. Runtime getRuntime availableProcessors)) #'user/n user=> n 8 user=> (parallel/prime-sum n) 838596693108 user=> ;;; Use the 'time' function to measure the execution speed. user=> (time (parallel/prime-sum 1)) "Elapsed time: 17164.104407 msecs" 838596693108 user=> (time (parallel/prime-sum 2)) "Elapsed time: 10979.473318 msecs" 838596693108 user=> (time (parallel/prime-sum n)) "Elapsed time: 4989.7304 msecs" 838596693108
NOTE: Elapsed times in your system may vary considerably from the ones presented here.
Please consider the following when programming your solution:
loop/recur
.
When running the function with the number of threads equal to the number of available CPU cores, make sure that all CPU cores get used at near 100% their capacity during a certain moment, as shown in the following image of the system monitor:
Deliver a single file called ring.clj
containing your solution. Please provide the following information:
IMPORTANT: The program source file must include at the top the author's personal information (name and student id) within comments. For example:
;;; ITESM CEM, November 23, 2014. ;;; Clojure Source File ;;; Activity: Using Parallel Map ;;; Author: Steve Rogers, A01166611 . . (The rest of the program goes here) .
Due date is Sunday, November 23.
This activity will be evaluated using the following criteria:
-10 | The program doesn't contain within comments the author's personal information. |
---|---|
10 | The program contains syntax errors. |
DA | The program was plagiarized. |
10-100 | Depending on the quality of the solution. |