Skip to content
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

Problem 10 [Language] #19

Open
jamiephillips0000 opened this issue Dec 13, 2014 · 8 comments
Open

Problem 10 [Language] #19

jamiephillips0000 opened this issue Dec 13, 2014 · 8 comments
Labels

Comments

@jamiephillips0000
Copy link

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
@ghost
Copy link

ghost commented Dec 14, 2014

Jamie

about (2)

if k1 * k2 = j then either k1 * k1 <= j or k2 * k2 <= j

Luc

On Sat, Dec 13, 2014 at 10:30 PM, Jamie Phillips [email protected]
wrote:

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


Reply to this email directly or view it on GitHub
#19.

__~O
-\ <,
()/ ()

reality goes far beyond imagination

@ghost
Copy link

ghost commented Dec 14, 2014

by the way:

you can also "flatMap that shit"

val ps: Stream[Int] =
2 #:: ps.flatMap(i => Stream.from(i + 1).find(j => ps.takeWhile(k => k * k
<= j).forall(j % _ > 0)))

Luc

On Sun, Dec 14, 2014 at 10:30 PM, Luc Duponcheel [email protected]
wrote:

Jamie

about (2)

if k1 * k2 = j then either k1 * k1 <= j or k2 * k2 <= j

Luc

On Sat, Dec 13, 2014 at 10:30 PM, Jamie Phillips <[email protected]

wrote:

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


Reply to this email directly or view it on GitHub
#19.

__~O
-\ <,
()/ ()

reality goes far beyond imagination

__~O
-\ <,
()/ ()

reality goes far beyond imagination

@ghost
Copy link

ghost commented Dec 14, 2014

and, this one works as well (ans may be more intuitive)

val ps: Stream[Int] = 2 #:: ps.flatMap(i => Stream.from(i + 1).filter(j =>
ps.takeWhile(k => k * k <= j).forall(j % _ != 0)))

(flattening a stream of streams)

On Sun, Dec 14, 2014 at 10:35 PM, Luc Duponcheel [email protected]
wrote:

by the way:

you can also "flatMap that shit"

val ps: Stream[Int] =
2 #:: ps.flatMap(i => Stream.from(i + 1).find(j => ps.takeWhile(k => k *
k <= j).forall(j % _ > 0)))

Luc

On Sun, Dec 14, 2014 at 10:30 PM, Luc Duponcheel <[email protected]

wrote:

Jamie

about (2)

if k1 * k2 = j then either k1 * k1 <= j or k2 * k2 <= j

Luc

On Sat, Dec 13, 2014 at 10:30 PM, Jamie Phillips <
[email protected]> wrote:

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


Reply to this email directly or view it on GitHub
#19.

__~O
-\ <,
()/ ()

reality goes far beyond imagination

__~O
-\ <,
()/ ()

reality goes far beyond imagination

__~O
-\ <,
()/ ()

reality goes far beyond imagination

@ghost
Copy link

ghost commented Dec 14, 2014

and if you do not like flatMap, then this one works as well

val ps: Stream[Int] = 2 #:: Stream.from(3).filter(j => ps.takeWhile(k => k

  • k <= j).forall(j % _ != 0))

On Sun, Dec 14, 2014 at 10:52 PM, Luc Duponcheel [email protected]
wrote:

and, this one works as well (ans may be more intuitive)

val ps: Stream[Int] = 2 #:: ps.flatMap(i => Stream.from(i + 1).filter(j =>
ps.takeWhile(k => k * k <= j).forall(j % _ != 0)))

(flattening a stream of streams)

On Sun, Dec 14, 2014 at 10:35 PM, Luc Duponcheel <[email protected]

wrote:

by the way:

you can also "flatMap that shit"

val ps: Stream[Int] =
2 #:: ps.flatMap(i => Stream.from(i + 1).find(j => ps.takeWhile(k => k *
k <= j).forall(j % _ > 0)))

Luc

On Sun, Dec 14, 2014 at 10:30 PM, Luc Duponcheel <
[email protected]> wrote:

Jamie

about (2)

if k1 * k2 = j then either k1 * k1 <= j or k2 * k2 <= j

Luc

On Sat, Dec 13, 2014 at 10:30 PM, Jamie Phillips <
[email protected]> wrote:

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


Reply to this email directly or view it on GitHub
#19.

__~O
-\ <,
()/ ()

reality goes far beyond imagination

__~O
-\ <,
()/ ()

reality goes far beyond imagination

__~O
-\ <,
()/ ()

reality goes far beyond imagination

__~O
-\ <,
()/ ()

reality goes far beyond imagination

@eloots
Copy link
Contributor

eloots commented Dec 15, 2014

Hi Jamie,

Most, if not all of your questions regarding views are answered here:

http://docs.scala-lang.org/overviews/collections/views.html http://docs.scala-lang.org/overviews/collections/views.html

Funny that palindromes come up during the explanation.

More useful articles one level up:

http://docs.scala-lang.org/overviews/?_ga=1.26260989.1311091313.1410020137 http://docs.scala-lang.org/overviews/?_ga=1.26260989.1311091313.1410020137

Cheers, Eric

On 13-dec.-2014, at 22:30, Jamie Phillips [email protected] wrote:

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


Reply to this email directly or view it on GitHub #19.

@ghost
Copy link

ghost commented Dec 15, 2014

Hi Jamie and Eric,

note that
http://docs.scala-lang.org/overviews/collections/views.html
mentions

... Scala collections are by default strict in all their transformers,
except for Stream, which implements all its transformer methods lazily. ...

So, maybe, for Stream, using view does not matter to much?

On Mon, Dec 15, 2014 at 9:13 AM, Eric Loots [email protected]
wrote:

Hi Jamie,

Most, if not all of your questions regarding views are answered here:

http://docs.scala-lang.org/overviews/collections/views.html <
http://docs.scala-lang.org/overviews/collections/views.html>

Funny that palindromes come up during the explanation.

More useful articles one level up:

http://docs.scala-lang.org/overviews/?_ga=1.26260989.1311091313.1410020137
<
http://docs.scala-lang.org/overviews/?_ga=1.26260989.1311091313.1410020137>

Cheers, Eric

On 13-dec.-2014, at 22:30, Jamie Phillips [email protected]
wrote:

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


Reply to this email directly or view it on GitHub <
https://github.com/BeScala/project-euler/issues/19>.


Reply to this email directly or view it on GitHub
#19 (comment)
.

__~O
-\ <,
()/ ()

reality goes far beyond imagination

@samdebacker
Copy link
Member

I would agree with @LucDupAtGitHub that a view on a Stream is pretty useless. In general, if you don't need memoization use a lazy view of some collection rather than a Stream.

@mverbist
Copy link

mverbist commented Jan 9, 2015

or, using the given isPrime function

Stream.from(2).filter(isPrime).takeWhile(_ < 2000000).map(_.toLong).sum

The conversion to Long is necessary to avoid overflow

@eloots eloots added the Language label Jan 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants