You are here:   ArielOrtiz.com > Programming Languages > Third Quarter Term Exam

Third Quarter Term Exam

This exam must be developed individually.

Problem Description

Problem 10 from Project Euler states:

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

Spoiler alert: The correct answer is 142,913,828,922.

Write a Clojure function that allows you to compute this solution in parallel, 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 exam3 (thus, your source file should be called exam3.clj). The function should take as an argument the number of threads to use during the computation.

Usage examples:

(exam3/prime-sum 1) ⇒ 142913828922
(exam3/prime-sum 2) ⇒ 142913828922 ;;; Result same as above, except computed faster!

;;; Using all available CPU cores.
(def n (.. Runtime getRuntime availableProcessors))
n ⇒ 4
(exam3/prime-sum n) ⇒ 142913828922 

;;; Use the 'time' function to measure the execution speed.
(time (exam3/prime-sum 1))
"Elapsed time: 8614.324209 msecs"
⇒ 142913828922
(time (exam3/prime-sum 2))
"Elapsed time: 5564.651921 msecs"
⇒ 142913828922
(time (exam3/prime-sum n))
"Elapsed time: 3835.156286 msecs"
⇒ 142913828922

You may define any number of additional auxiliary functions. Just make sure every single function contains a doc-string explaining its purpose.

Avoid using functions that create huge lists. These functions usually don't perform well when running in multiple threads. This means that when doing repetitions you might be better off using loop/recur.

Run your functions multiple times and measure how long they take to execute for different number of threads.

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:

Delivery and Assessment

The program source file must include at the top the author's personal information (name and student ID) within comments. For example:

;;;----------------------------------------------------
;;; ITESM CEM, April 20, 2012.
;;; Clojure Source File
;;; Activity: Third Quarter Term Exam (Sum of Primes)
;;; Author: 1166611  Anthony Stark
;;;----------------------------------------------------

    .
    . (The rest of the program goes here)
    .

Write a report of at least 200 words in which you describe how you solved the problem, the results of the time measurements and your conclusions. Include also at least one chart (bar chart, line chart, etc.) in which the speedup improvements can be clearly observed. The report must be presented as a PDF document called report.pdf.

Place both files (exam3.clj and report.pdf) in a tarball file called exam3.tgz. From a terminal, you can use the following command to create this file:

tar czf exam3.tgz exam3.clj report.pdf

Using the Online Assignment Delivery System (SETA), deliver the file exam3.tgz. No exams will be accepted through e-mail or any other means.

Due date: Friday, April 20.

Rubrics

35% The implementation gives the correct result.
35% The implementation produces an observable speedup (Sp = 1.3 or higher) when running the function with multiple threads in a multi-core system.
20% The written report contains all the requested elements.
5% Each function in the Clojure source file contains a short documentation string.
5% The source file contains within comments the authors' personal information.
DA In case of plagiarism or any kind of fraud.