Skip to content

Commit

Permalink
[#5] Exactly the same logic, but with an implicit class that adds int…
Browse files Browse the repository at this point in the history
…eger extensions.

This is a simple 'implicit class' showcase.
  • Loading branch information
oleastre committed Nov 28, 2014
1 parent 3e2af0d commit 8d807c8
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/test/scala/org/bescala/projecteuler/problems/Problem001.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,24 @@ class Problem001 extends EulerSuite {
( count15 * (count15 + 1) * 15 )) / 2
}

euler(problem(1), "implicit class showcase") {

/* Let's add integer extensions as an implicit class show case
*/
implicit class IntegerExtensions(value: Int) {

/* Returns the sum of integers from 1 to value using mathematical properties.
*/
def sumSuite: Int = value * (value + 1) / 2

/* Returns the number of multiples of value below a given value
*/
def numberMultiplesBelow(other: Int) = (other-1) / value
}


(3.numberMultiplesBelow(1000)).sumSuite * 3 +
(5.numberMultiplesBelow(1000)).sumSuite * 5 -
(15.numberMultiplesBelow(1000)).sumSuite * 15
}
}

0 comments on commit 8d807c8

Please sign in to comment.