/**
* Largest palindrome product: https://projecteuler.net/problem=4
*
* TA 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.
*/
val allnum = 100 to 999
val allprod = for (x <- allnum; y <- allnum) yield {(x, y, x * y)}
val answer = allprod.
filter(x => (x._3 == x._3.toString.reverse.toInt)).
sortWith(_._3 > _._3).head
Project Euler
Thursday, March 12, 2015
Problem 3 in Scala
/**
* Largest prime factor: https://projecteuler.net/problem=3
*
* The prime factors of 13195 are 5, 7, 13 and 29.
*
* What is the largest prime factor of the number 600851475143?
*/
import scala.math._
def isPrime(n: Int) = (2 to math.sqrt(n).toInt).forall(n%_ != 0)
val n = 600851475143L
val answer = 1.to(sqrt(n).toInt).filter(isPrime).filter(n % _ == 0).sortWith(_ > _).head
* Largest prime factor: https://projecteuler.net/problem=3
*
* The prime factors of 13195 are 5, 7, 13 and 29.
*
* What is the largest prime factor of the number 600851475143?
*/
import scala.math._
def isPrime(n: Int) = (2 to math.sqrt(n).toInt).forall(n%_ != 0)
val n = 600851475143L
val answer = 1.to(sqrt(n).toInt).filter(isPrime).filter(n % _ == 0).sortWith(_ > _).head
Problem 2 in Scala
/**
* Even Fibonacci numbers: https://projecteuler.net/problem=2
*
* 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.
*/
import scala.collection.mutable.ListBuffer
val n = 4000000
var newElement = 0
val fibList = ListBuffer(1,2)
while (newElement <= n) {
val a = fibList.length
newElement = fibList(a - 1) + fibList(a - 2)
if (newElement <= n) fibList.append(newElement)
}
val answer = fibList.filter(_ % 2 == 0).sum
* Even Fibonacci numbers: https://projecteuler.net/problem=2
*
* 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.
*/
import scala.collection.mutable.ListBuffer
val n = 4000000
var newElement = 0
val fibList = ListBuffer(1,2)
while (newElement <= n) {
val a = fibList.length
newElement = fibList(a - 1) + fibList(a - 2)
if (newElement <= n) fibList.append(newElement)
}
val answer = fibList.filter(_ % 2 == 0).sum
Problem 1 in Scala
/**
* Multiples of 3 and 5: https://projecteuler.net/problem=1
*
* 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.
*/
val n = 1000
val answer = 1.until(n).filter(x => (x % 3 == 0 || x % 5 == 0)).sum
* Multiples of 3 and 5: https://projecteuler.net/problem=1
*
* 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.
*/
val n = 1000
val answer = 1.until(n).filter(x => (x % 3 == 0 || x % 5 == 0)).sum
Subscribe to:
Posts (Atom)