Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7070723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:36:04+00:00 2026-05-28T05:36:04+00:00

This question relates to euler project sum-of-primes, and Stream.view , but there is a

  • 0

This question relates to euler project sum-of-primes, and Stream.view, but there is a bit of a twist. I want to calculate the sum of all the primes below two million. I create a generator of prime numbers defined as:

lazy val primes: Stream[Int] = 2 #:: Stream.from(3).filter(i =>
  primes.takeWhile(j => j * j <= i).forall(i % _ > 0))

I wrote two tests, one using Stream[Int]#foldLeft and one using Stream[Int]#sum:

 @Test
 def testEuler010a {
   primes.view.takeWhile(_ < 2000000).foldLeft(0L)(_ + _) mustEqual 142913828922L
 }

 @Test
 def testEuler010b {
   primes.view.takeWhile(_ < 2000000).sum mustEqual 142913828922L
 }

testEuler010a gives me the right answer while testEuler010b does not with an answer of 1179908154. I would expect that Stream[Int]#foldLeft(0L)(_ + _) would be identical to Stream[Int].sum, but it is not. Even if I materialize the Stream with a toList(), I get he same discrepancy. Is it an incorrect assumption that those methods should give the same result?

I’m using Scala 2.9.1.final.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:36:04+00:00Added an answer on May 28, 2026 at 5:36 am

    The problem seems to be overflow. These give the same results for me:

    (primes map (_.longValue) takeWhile (_ < 2000000)).sum
    (primes map (_.longValue) takeWhile (_ < 2000000)).foldLeft(0L)(_ + _)
    

    I suppose the difference between sum and foldLeft is that the result type of sum is the same as the type of the elements being summed over (as required by the Numeric trait), but foldLeft can have a different result type, which you achieve by writing 0L.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question specifically relates to my experience with PHP, but there's no reason why
This is kind of a two part question. But it both relates to the
This question relates to cleaning up the view and giving the controller more of
This question relates to Java collections - specifically Hashtable and Vector - but may
This question relates specifically to JavaScript, but I imagine would apply to any language.
This question relates to my other post . Ok so after a bit more
This question relates to this question which I asked earlier this week. The answer
This question relates to those parts of the KenKen Latin Square puzzles which ask
James here. This question relates (sort of) to the one I posted a little
This relates to my other question on accessing a REST service that uses forms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.