Skip to content

Instantly share code, notes, and snippets.

@edalorzo
Created November 24, 2012 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edalorzo/4138557 to your computer and use it in GitHub Desktop.
Save edalorzo/4138557 to your computer and use it in GitHub Desktop.
Project Euler-Problem 1
/**
* 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