Many but not all positive integers can be written as a sum of distinct primes. The number 12, for example, can be written as either 5 + 7 or 2 + 3 + 7. The numbers 4 and 6, on the other hand, have no partition into unique primes, and the number 9 has only one: 2 + 7. This, the first graded assignment of the semester, asks you to enumerate all the ways of partitioning a given number n into a sum of two or more distinct primes, a + b + ... + m and to do so in five different programming languages: Java, C++, Ada 95, C#, and Python.
When run, your programs should read an integer n from
standard
input, and then write the appropriate sums to standard output, in
arbitrary order.
Given the input 20, for example, your programs might print
2 + 7 + 11
3 + 17
7 + 13
You should print each partition only once. Toward this end, you will
probably find it easiest to write your programs in such a way that they
print the addends in sorted order.
The prime partition problem is
naturally recursive. The easiest and most elegant
solutions employ iterators, which are abstractions used to drive
a for loop. We will study iterators in Section 6.5.3; you
may want to read ahead. In the terminology of that section,
you’ll find that Python and C# have true iterators, Java
and C++ have iterator objects, and Ada has no special iterator
support. If you already
knew the languages, you’d probably find your task easiest in Python and
C#, and hardest in Ada, with Java and C++ somewhere in the middle. Of
course you probably don’t know all five languages already, so that will
be something of a hurdle.
A hint: If you program with iterators, you may find it useful to define
primes(a, b) that generates all prime numbers
between a and b, inclusive.
prime_partitions(n, k) that generates all
prime partitions of n using primes greater than
k.
prime_partitions(n, 1).
As in all assignments this semester, you may work alone or in teams of two. If you choose to work in pairs, one obvious division of labor is to split up the languages among you. That’s ok, but I strongly encourage you to read each others’ solutions, to make sure you understand them.
In addition to the usual requirements, your write-up must compare and contrast the programming experience in the different languages. (This will of course be easier if you really understand what happened in all five.) What was easy? What was hard? Are there noticable differences in speed? What do you like/dislike? Did you find iterators to be helpful?
Be sure to follow all the rules on the Grading page.
As with all assignments, use the turn-in script:
~cs254/bin/TURN_IN. Put your
write-up in a README.txt or README.pdf file
in the directory in which you run the script.
Be sure to describe any features of your code that the TA
might not immediately notice.
We will be using the following language implementations:
javac (Sun’s Java 5 compiler)
and run with the java interpreter. Both are located in
/usr/staff/bin on the CSUG machines.
g++ (the GNU C++ translator).
It’s in /usr/bin. It produces native executables.
gnatmake (a wrapper for the
GNU Ada translator). It’s in /u/cs254/bin, which you
should add to your PATH environment variable (ask a
friend or one of the TA if you
don’t know how). It produces native executables.
mcs (the Mono project C#
compiler) and run with the mono JIT/run-time system.
Both are located in /u/cs254/bin.
python interpreter. It’s
in /usr/bin.
You are welcome to do development using other language implementations and/or platforms, but you must ensure that your final versions compile and run correctly using the implementations listed above. We will be testing using only these.
I won’t be devoting lecture time to how to use these languages. You’ll need to find on-line tutorials or other resources, and teach yourself. Here are some decent starting points:
Before the beginning of class on Tuesday, September 11, send e-mail
to cs254 containing answers to the following
questions:
"hello,
world" to standard output in C#? In Ada?
