From 96ffb129caca30f7a0b20f49bbc5921f0dd24cda Mon Sep 17 00:00:00 2001 From: Ignasi Marimon-Clos Date: Wed, 23 May 2012 22:14:27 +0200 Subject: [PATCH] Solve the problem number 1 --- src/main/scala/com/marimon/xmpp/Problem1.scala | 24 ++++++++++++++++++++++ src/test/scala/com/marimon/xmpp/Problem1Test.scala | 12 +++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/main/scala/com/marimon/xmpp/Problem1.scala diff --git a/src/main/scala/com/marimon/xmpp/Problem1.scala b/src/main/scala/com/marimon/xmpp/Problem1.scala new file mode 100644 index 0000000..2651fe4 --- /dev/null +++ b/src/main/scala/com/marimon/xmpp/Problem1.scala @@ -0,0 +1,24 @@ +package com.marimon.xmpp + +object Problem1 { + + def find(limit: Int): IndexedSeq[Int] = { + for ( + i <- 1 to (limit - 1) if i % 3 == 0 || i % 5 == 0 + ) yield i + } + + def sum(values: Seq[Int]): Int = { + values.foldLeft(0)((b, a) => b + a) + } + + def solve(input: Int): Int = { + { + for ( + i <- 1 to (input - 1) if i % 3 == 0 || i % 5 == 0 + ) yield i + }.foldLeft(0)((b, a) => b + a) + + } + +} \ No newline at end of file diff --git a/src/test/scala/com/marimon/xmpp/Problem1Test.scala b/src/test/scala/com/marimon/xmpp/Problem1Test.scala index 11d6a4b..2b26cbc 100644 --- a/src/test/scala/com/marimon/xmpp/Problem1Test.scala +++ b/src/test/scala/com/marimon/xmpp/Problem1Test.scala @@ -3,6 +3,7 @@ import org.scalatest.FlatSpec import org.scalatest.matchers.ShouldMatchers import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner +import scala.collection.immutable.IndexedSeq @RunWith(classOf[JUnitRunner]) class Problem1Test extends FlatSpec with ShouldMatchers { @@ -10,4 +11,15 @@ class Problem1Test extends FlatSpec with ShouldMatchers { //3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. //Find the sum of all the multiples of 3 or 5 below 1000. + "The problem 1" should "find all multiples of 3 or 5 under 10" in { + Problem1.find(10) should be(IndexedSeq(3, 5, 6, 9)) + } + + it should "sum the provided numbers" in { + Problem1.sum(IndexedSeq(1, 2, 3, 4)) should be(10) + } + + it should "solve the problem" in { + Problem1.solve(1000) should be(233168) + } } \ No newline at end of file -- 2.1.1