Skip to content

adding Project Euler problems in the folder Project Euler #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/scala/ProjectEuler/Euler1.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}

27 changes: 27 additions & 0 deletions src/main/scala/ProjectEuler/Euler2.scala
Original file line number Diff line number Diff line change
@@ -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)
}

}

22 changes: 22 additions & 0 deletions src/main/scala/ProjectEuler/Euler3.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
21 changes: 21 additions & 0 deletions src/main/scala/ProjectEuler/Euler4.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}