CS-A1120 Programming 2
Department of Computer Science
Aalto University
By the time you’ve sorted out a complicated idea into little steps that even a stupid machine can deal with, you’ve certainly learned something about it yourself.
—Douglas Adams, Dirk Gently’s Holistic Detective Agency
From gates to functional and parallel programming
After finishing this course, you
… and more!
What does it compute?
def test(m: Long) = var i = 1L var s = 0L while i <= m do s = s + i i = i + 1 end while s end test
Discuss with person next to you (a couple of minutes)…
while-sum in a consoledef test(m: Long) = var i = 1L var s = 0L while i <= m do s = s + i i = i + 1 end while s end test val NANOS_PER_SEC = 1e9 val test_start_time = System.nanoTime val x = test(4000000000L) val test_end_time = System.nanoTime val test_duration = test_end_time - test_start_time println("the test took %.2f seconds".format(test_duration/NANOS_PER_SEC))
Adds up the first four billion positive integers
the test took 1.06 seconds
(Intel Core i5-1135G7 @ 2.40GHz × 8 processor and 16 GiB of main memory)
test? Better?(https://presemo.aalto.fi/prog2)
def test(m: Long) = var i = 1L var s = 0L while i <= m do s = s + i i = i + 1 end while s end test
test?(https://presemo.aalto.fi/prog2)
One suggestion:
def test2(m: Long) = m * ((m + 1)/2.0)
Another:
def test3(m: Long) = (0L to m).sum
Yet another:
def test4(m: Long): Long = if m > 0 then m+test4(m-1) else 0
Or why not?
def test5(m: Long): Long = (0L to m).reduce(_ + _)
Are these "better"? Any drawbacks?
to and until to create a Range in Scala
to is inclusive (the last element is included)until is exclusive (the last element is excluded)scala> (0 until 10).last val res: Int = 9 scala> (0 to 10).last val res: Int = 10
Range inherits the SeqOps trait, meaning that we can call methods such as map, fold, zip, and so on. For example, to square some valuesscala> (1 to 3).map(x => x * x) val res: IndexedSeq[Int] = Vector(1, 4, 9) scala> Seq(1,2,3).map(x => x * x) val res: Seq[Int] = List(1, 4, 9)
Which is why we can define the sum as
def test3(m: Long) = (0L to m).sum
(But why the 0L and not 0?)
def test4(m: Long): Long = if m > 0 then m+test4(m-1) // Recursive step else 0 // Base case
Programming is a way to harness computing for a given purpose
Independent of programming language and hardware architecture
Programming skills and understanding of computing are integral parts of scientific education
Computer science studies the phenomena of computing and the use of computing machines
scala> def test(m: Long) = m * ((m + 1)/2.0) def test(m: Long): Double scala> test(30) val res: Double = 465 scala>test(4000000000) val res: Double = 8.000000002E18
Why Double?
scala> def test(m: Long) = m * ((m + 1)/2) def test(m: Long): Long scala> test(30) val res: Long = 450
450??
scala> def test(m: Long) = (m * (m + 1))/2 def test(m: Long): Long scala> test(4000000000) val res: Long = -1223372034854775808
-1223372034854775808???
Double cannot handle arbitrary large numbers either; There is a type called BigInt, that you may want to try for huge numbers.)
The Status of the P Versus NP Problem by Lance Fortnow (2009)
Is the number 2026 in the following list of integers?
\( 89, 117, 153, 265, 395, 477, 516, 665, 670, 795 \)
What would you have to do to answer this question?
Can the number 2026 be formed as a sum of the following integers?
\( 89, 117, 153, 265, 395, 477, 516, 665, 670, 795 \)
given that you can use each at maximum one time?
What would you have to do to answer this question?
It is possible to write programs to search for an answer, but, there is no known method for solving this problem efficiently in its general form (i.e. arbitrarily many numbers of arbitrary size)
Can the number 2026 be formed as a sum of the following integers?
\( 89, 117, 153, 265, 395, 477, 516, 665, 670, 795 \)
Assume someone gave you the answer "Yes, the numbers are \(89, 117, 153, 395, 477, 795\)."
What would you have to do to answer this question?
You must submit only solutions produced by yourself. You do not have permission to enter course materials into AI tools or to otherwise distribute the materials to external parties.
We follow the Aalto code of conduct and we maintain academic integrity!
Make sure to read and reflect on these documents.
These slides are an overview of information on MyCourses as of the start of the course (February 23, 2026).
Up-to-date information available in MyCourses.
[ ] Signed up in SiSU[ ] Read course information on MyCourses[ ] Have access token/app for the computer labs[ ] Read the instructions
[ ] Able to start an IDE (in labs or on own computer)[ ] Know how to work with the assignments[ ] Know when/where the exercise sessions are heldPlease note: Course staff will not be able to answer questions about exercises over email or direct messages. Please come to the exercise sessions if you need help!
This course is based on previous material developed by