The Project Euler Challenge - Exercise Your Scala Skills

Showing 1-9 of 9 messages
The Project Euler Challenge - Exercise Your Scala Skills Mitch Wong Ho 9/28/11 6:45 AM
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!
unk...@googlegroups.com 9/28/11 6:47 AM <This message has been deleted.>
Re: The Project Euler Challenge - Exercise Your Scala Skills Mitch Wong Ho 9/28/11 6:56 AM
#1 Add all the natural numbers below one thousand that are multiples
of 3 or 5.
Re: The Project Euler Challenge - Exercise Your Scala Skills Gary Pamparà 9/28/11 10:09 AM
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

On Wed, Sep 28, 2011 at 3:56 PM, Mitch Wong Ho <oreo...@gmail.com> wrote:
> #1 Add all the natural numbers below one thousand that are multiples
> of 3 or 5.

Re: The Project Euler Challenge - Exercise Your Scala Skills Mitch Wong Ho 9/28/11 12:47 PM
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
>
Re: The Project Euler Challenge - Exercise Your Scala Skills Gary Pamparà 9/28/11 1:23 PM
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

Re: The Project Euler Challenge - Exercise Your Scala Skills Pavel Tcholakov 9/28/11 10:01 PM
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.

Re: The Project Euler Challenge - Exercise Your Scala Skills Mitch Wong Ho 9/29/11 4:33 AM
Thanks for the extra challenges, Pavel.

Good tip, Gary.
#2 By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. Mitch Wong Ho 10/3/11 12:33 AM
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.