Skip to content

Problem 10 [Language] #19

Description

@jamiephillips0000

Hi Guys
So I was looking on the internet for solutions to this that would return a value in the next millennium - because my attempts were not performing well at all! ;-)
So I found this somewhere on the net
lazy val ps: Stream[Int] = 2 #:: ps.map(i => Stream.from(i + 1).find(j => ps.takeWhile(k => k * k <= j).forall(j % _ > 0)).get)
ps.view.takeWhile(_ < 2000000).foldLeft(0L)(_ + _)
This works like a charm and returns within 2 seconds with the correct result
Questions

  1. Anyone got a recommendation on when to use Stream and View? Why does turning ps above into a view make a difference?
  2. Can someone explain what this does - k => k * k <= j
    I hope that all the gluhwein I had today has made my questions understandable
    Jamie

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions