Skip to main content
added 406 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Better way forof calculating project euler's 2nd problem FibonacciProject Euler #2 (Fibonacci sequence)

Even Fibonacci numbers

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.

object Problem_2 extends App {
    def fibLoop():Long =
    {
        var x = 1L
        var y = 2L
        var sum = 0L
        var swap = 0L

        while (x < 4000000)
        {
            if (x % 2 ==0) sum +=x+= x
            swap = x
            x = y
            y = swap + x
        }
        sum
    }
    def fib:Int = {
        lazy val fs: Stream[Int] = 0 #:: 1 #:: fs.zip(fs.tail).map(p => p._1 + p._2)
        fs.view.takeWhile(_ <= 4000000).filter(_ % 2 == 0).sum
    }
 

    val t1 = System.nanoTime()
    val res = fibLoop
    val t2 = (System.nanoTime() - t1 )/1000 
    println(s"The result is: $res time taken $t2 ms ")
 
 }

Is there a bettermore functional way forof calculating the fibonaciFibonacci sequence and taking the sum of the the even values below 4million 4 million? (projecteuler.net -> problem 2)

TheIs the imperative method is 1000x faster  ?

Better way for calculating project euler's 2nd problem Fibonacci sequence)

object Problem_2 extends App {
def fibLoop():Long =
  {
  var x = 1L
  var y = 2L
  var sum = 0L
  var swap = 0L

  while(x < 4000000)
  {
    if(x % 2 ==0) sum +=x
    swap = x
    x = y
    y = swap + x
  }
  sum
 }
 def fib:Int = {
 lazy val fs: Stream[Int] = 0 #:: 1 #:: fs.zip(fs.tail).map(p => p._1 + p._2)
 fs.view.takeWhile(_ <= 4000000).filter(_ % 2 == 0).sum
}
 

  val t1 = System.nanoTime()
  val res = fibLoop
  val t2 = (System.nanoTime() - t1 )/1000 
  println(s"The result is: $res time taken $t2 ms ")
 
 }

Is there a better functional way for calculating the fibonaci sequence and taking the sum of the the even values below 4million ? (projecteuler.net -> problem 2)

The imperative method is 1000x faster  ?

Better way of calculating Project Euler #2 (Fibonacci sequence)

Even Fibonacci numbers

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.

object Problem_2 extends App {
    def fibLoop():Long =
    {
        var x = 1L
        var y = 2L
        var sum = 0L
        var swap = 0L

        while (x < 4000000)
        {
            if (x % 2 ==0) sum += x
            swap = x
            x = y
            y = swap + x
        }
        sum
    }
    def fib:Int = {
        lazy val fs: Stream[Int] = 0 #:: 1 #:: fs.zip(fs.tail).map(p => p._1 + p._2)
        fs.view.takeWhile(_ <= 4000000).filter(_ % 2 == 0).sum
    }

    val t1 = System.nanoTime()
    val res = fibLoop
    val t2 = (System.nanoTime() - t1 )/1000 
    println(s"The result is: $res time taken $t2 ms ")
}

Is there a more functional way of calculating the Fibonacci sequence and taking the sum of the even values below 4 million?

Is the imperative method 1000x faster?

edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Post Migrated Here from programmers.stackexchange.com (revisions)
Source Link
firephil
  • 213
  • 1
  • 9
Loading