diff --git a/src/main/scala/ProjectEuler/Euler1.scala b/src/main/scala/ProjectEuler/Euler1.scala new file mode 100644 index 0000000..a469668 --- /dev/null +++ b/src/main/scala/ProjectEuler/Euler1.scala @@ -0,0 +1,18 @@ +package ProjectEuler + +object Euler1 { + /** + *Problem Statement : If we list all the natural numbers + * below 10 that are multiples of 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. + + **/ + + def main (args : Array[String]) { + val sum = (1 until 1000).filter(n => n % 3 == 0 || n % 5 == 0).sum + println(sum) + } +} + diff --git a/src/main/scala/ProjectEuler/Euler2.scala b/src/main/scala/ProjectEuler/Euler2.scala new file mode 100644 index 0000000..15bb98e --- /dev/null +++ b/src/main/scala/ProjectEuler/Euler2.scala @@ -0,0 +1,27 @@ +package ProjectEuler + +object Euler2 { + /** + *Problem Statement : Each new term in the Fibonacci sequence + *is generated by adding the previous two terms. + *By starting with 1 and 2, the first 10 terms will be: + + *1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... + + *By considering the terms in the Fibonacci sequence whose *values do not exceed four million, + *find the sum of the even-valued terms. + + **/ + + def nextFib(x: Int, y: Int): Stream[Int] = { + x #:: nextFib(y, x+y) + } + + def fib = nextFib(1,1) + + def main (args : Array[String]) { + println(fib.takeWhile(_ <= 4000000).filter(_ % 2 == 0).sum) + } + +} + diff --git a/src/main/scala/ProjectEuler/Euler3.scala b/src/main/scala/ProjectEuler/Euler3.scala new file mode 100644 index 0000000..f4da397 --- /dev/null +++ b/src/main/scala/ProjectEuler/Euler3.scala @@ -0,0 +1,22 @@ +package ProjectEuler + +object Euler3 { + /** + *Problem Statement : The prime factors of 13195 are 5, 7, 13 and 29. + + *What is the largest prime factor of the number 600851475143 ? + + **/ + + def primeFactors(num: Long): List[Long] = { + val exists = (2L to math.sqrt(num).toLong).find(num % _ == 0) + exists match { + case None => List(num) + case Some(d) => d :: primeFactors(num/d) + } + } + + def main(args : Array[String]) { + println(primeFactors(600851475143L).last) + } +} diff --git a/src/main/scala/ProjectEuler/Euler4.scala b/src/main/scala/ProjectEuler/Euler4.scala new file mode 100644 index 0000000..7faace9 --- /dev/null +++ b/src/main/scala/ProjectEuler/Euler4.scala @@ -0,0 +1,21 @@ +package ProjectEuler + +object Euler4 { + /** + *Problem Statement : A palindromic number reads the same both ways. + *The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. + + *Find the largest palindrome made from the product of two 3-digit numbers. + + **/ + def isPalindrome(num: Int): Boolean = { + val numString = num.toString + numString == numString.reverse + } + + def main(args : Array[String]){ + val palindromes = for (x <- 100 to 999; y <- 100 to 999 if (isPalindrome(x * y))) yield x*y + println(palindromes.max) + } +} +