Created
November 24, 2012 05:32
-
-
Save edalorzo/4138557 to your computer and use it in GitHub Desktop.
Project Euler-Problem 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Given a maximum integer m and a factor n calculates the sum | |
* of all divisors of the given factor up to m. | |
* | |
* @param m maximum integer | |
* @param n given factor (i.e. 3 or 5) | |
* @return the sum of all divisors. | |
*/ | |
def sumDivisible(m: Int, n:Int): Int = { | |
val p = m / n | |
n * (p * (p + 1)) / 2 | |
} | |
def result = sumDivisible(999,3) + sumDivisible(999,5) - sumDivisible(999, 15) | |
def solve = "The sum of multiple of 3 and 5 up to 1000 is: " + result | |
//Naive solution based on filter | |
def alternative(j:Int, k: Int, m: Int) = 1 until m filter(n => n % j == 0 || n % k == 0) sum | |
println(solve) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment