-
Code object FindMultiples: def findMultiples: Int => Int = n => (0 until n by 2).filter(i => (i%4)*(i%6) == 0).sum
Test Cases import collection.mutable.Stack import org.scalatest._ import flatspec._ import matchers._ import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import FindMultiples.findMultiples class ExampleSpec extends AnyFlatSpec with ScalaCheckPropertyChecks with should.Matchers: "findMultiples(n)" should "return 0 if n == 3" in { findMultiples(3) should be(0) } it should "return 0 if n == 4" in { findMultiples(4) should be(0) } it should "return 4 if n == 5" in { findMultiples(5) should be (4) } it should "return 4 if n == 6" in { findMultiples(6) should be(4) } it should "return 10 if n == 7" in { findMultiples(7) should be (10) } it should "return the correct result for any given n" in { val moreMultiples = Table( ("n", "multiple"), (20, 64), (35, 198), (120, 2340) ) forAll(moreMultiples) { case (n, multiple) => findMultiples(n) should be (multiple) } }
Output:
-
Code - object FindMultiples:
def findMultiples(n: Int): Int =Seq.range(0, n).filter(i => i % 4 == 0 || i % 6 == 0).sum- def findMultiples: Int => Int = n =>
- (0 until n by 2).filter(i => (i%4)*(i%6) == 0).sum
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}