Commits

Ignasi Marimon-Clos committed 96ffb12

Solve the problem number 1

  • Participants
  • Parent commits 0c795db

Comments (0)

Files changed (2)

src/main/scala/com/marimon/xmpp/Problem1.scala

+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)
+
+  }
+
+}

src/test/scala/com/marimon/xmpp/Problem1Test.scala

 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 {
   //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)
+  }
 }