The Project Euler Challenge - Exercise Your Scala Skills

123 views
Skip to first unread message

Mitch Wong Ho

unread,
Sep 28, 2011, 9:45:01 AM9/28/11
to South Africa Scala User Group
As a fun way to learn Scala and the challenge your self, I've created
an account on The Euler Project (http://projecteuler.net).

If you are not aware of Project Euler, Project Euler is a website that
"Project Euler exists to encourage, challenge, and develop the skills
and enjoyment of anyone with an interest in the fascinating world of
mathematics."

The idea is to post a problem from Project Euler and you can then work
through it and post the solution code here.

Have fun!
Message has been deleted

Mitch Wong Ho

unread,
Sep 28, 2011, 9:56:46 AM9/28/11
to South Africa Scala User Group
#1 Add all the natural numbers below one thousand that are multiples
of 3 or 5.

Gary Pampara

unread,
Sep 28, 2011, 1:09:44 PM9/28/11
to south-africa-s...@googlegroups.com
Just remember that the solutions are supposed to be a secret :) It's
an achievement to get them.

There are numerous ways to solve this first problem, but two that
quickly spring to mind are:

1. filter:

val x = (1 until 1000) filter { x => x % 3 == 0 || x % 5 == 0 }

2. flatMap (using a for comprehension):

val y = for { i <- (1 until 1000) if (i %3 == 0 || i % 5 == 0) } yield i

Regards,
Gary

Mitch Wong Ho

unread,
Sep 28, 2011, 3:47:52 PM9/28/11
to South Africa Scala User Group
In the name of fun and learning, I thought this would be a good way to
learn and apply Scala.

Thank you Gary for your two solutions. If you run either of Gary's
statements in the interpreter, you get a List of values that multiples
of 3 or 5.

Just to finish off the solution and arrive at the answer, here is a
possible next-step after Gary's functions:

y.foldLeft(0)((m: Int, n: Int) => m+n) //returns 233168

Checkout: http://oldfashionedsoftware.com/2009/07/10/scala-code-review-foldleft-and-foldright/
for a good explanation on the foldLeft() function.

Therefore this problem can be solved using Scala Collections.


On Sep 28, 7:09 pm, Gary Pampara <gpamp...@gmail.com> wrote:
> Just remember that the solutions are supposed to be a secret :) It's
> an achievement to get them.
>
> There are numerous ways to solve this first problem, but two that
> quickly spring to mind are:
>
> 1. filter:
>
> val x = (1 until 1000) filter { x => x % 3 == 0 || x % 5 == 0 }
>
> 2. flatMap (using a for comprehension):
>
> val y = for { i <- (1 until 1000) if (i %3 == 0 || i % 5 == 0) } yield i
>
> Regards,
> Gary
>

Gary Pampara

unread,
Sep 28, 2011, 4:23:48 PM9/28/11
to south-africa-s...@googlegroups.com
No arguments at all. Learning is the most important thing :)

Euler problems are really, really entertaining - albeit that the later
ones can be extremely frustrating until that "aha" moment.

For numeric types, such as Int in this case, the sum function is also
available on the collections.

Eg (using the defined y value): y.sum == 233168

Regards,
Gary

Pavel Tcholakov

unread,
Sep 29, 2011, 1:01:19 AM9/29/11
to south-africa-s...@googlegroups.com
Hey guys, new here! Good to have a local Scala forum at last!

As well as Project Euler, this is a great resource for learning Scala:

http://aperiodic.net/phil/scala/s-99/

Adapted from the similar "99 Prolog problems" and "99 Lisp problems"
tutorials. Unlike project Euler, it tends to have less of an
algorithmic/mathematical "aha" focus and more aimed at teaching the
functional side of programming. Plus you can peek at the solutions
once you've had your try ;-)

P.

Mitch Wong Ho

unread,
Sep 29, 2011, 7:33:46 AM9/29/11
to south-africa-s...@googlegroups.com
Thanks for the extra challenges, Pavel.

Good tip, Gary.

Mitch Wong Ho

unread,
Oct 3, 2011, 3:33:48 AM10/3/11
to South Africa Scala User Group
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.
Reply all
Reply to author
Forward
0 new messages