## Project Euler: Problem 1

So I'll admit it, I'm more than acceptably late to this party. OJ started working on these problems years ago, while I was still coding away on other stuff at school. Now that I'm not in school and needing a challenge I'm doing the best I can to tackle the problems at Project Euler. Since I'm also trying to pick up Haskell, this makes for a good way to learn the language. This isn't my first time using a Functional Programming language. But the last time I tried, it was forced upon me at school with inadequate assistance and the whole experience left a bad taste in my mouth. I'm glad that getting over the bad experience and learning something new at the same time.

I've started keeping a github repo with my solutions for the various problems. If anyone is interested in commenting on my code please do so. I welcome all constructive criticism.

To start this thing off; problem one reads,
“If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.”

Being this one was simple. I coded it up in Haskell, Python, and Perl.

`problem1 = sum \$ filter (\x -> mod x 3 == 0 || mod x 5 == 0) [1..1000]problem1' = sum \$ filter (\x -> mod x 3 == 0 || mod x 5 == 0) [1..999]`

Python:
`#!/usr/bin/python def threeorfive(n):  if ( n % 3 == 0 or n % 5 == 0):    return True  else:    return False def main():  first_list = range(1,1000)  second_list = filter(threeorfive, first_list)  print "%s" % (sum(second_list)) if __name__ == "__main__":  main()`

Perl:
`#!/usr/bin/perl my \$count = 0;my \$total = 0; while ( \$count < 1000){  if ( \$count % 3 == 0 or \$count % 5 == 0)  {    \$total += \$count;  }   \$count++;} print \$total;`

One thing that is nice about doing this in different languages, is that you get to become aware of the differences between some of those languages. For instance in Python the range function works a little differently than I expected. I was expecting an inclusive range function, one in which the 10 is included in the list. But that is now how Python's range works. It gives me 10 numbers, starting with 1, the end result is a list ending in 9. It's not a big deal and easily fixable, but just not something I was expecting. That is why the Haskell code has to functions in it. Just to verify that the Python code was correct.

I'll be posting more answers as I complete them. So expect to see some random posts with Euler solutions in them.

### In java

`public class Add{	public static void main(String[] args){		int sum=0;		int num = 0;		System.out.println("This Java program will add all the natural numbers below one thousand that are multiples of 3 or 5.");		while(num<999) //While loop keeps on running until num reaches 999		{			num++;			if(num%3 == 0 || num%5 == 0) //If the number is a multiple of 3 or 5, it will be added to the current sum			{				sum=sum+num;			}		}		System.out.println("Sum is: "+ sum); //Printing the final output	}}`

### Perl6

say [+] (1..999).grep { \$^n !% 3 || \$^n !% 5 }

Good to hear you're playing with Haskell mate. It's a fab language to learn.

I personally prefer filtering during list generation using list comprehension rather than filtering post creation. Behind the scenes I'd say that they function the same thanks to Haskell's laziness though :)

`solution = sum [ x | x <- [1..999],mod x 3 = 0 || mod x 5 = 0]`

Keep solving and posting mate :)

OJ

solution = sum [ x | x <- [1..999],mod x 3 == 0 || mod x 5 == 0]

### Or...

`my \$total;for my \$n (1..999) {  \$total += \$n if \$n % 3 == 0 or \$n % 5 == 0}`

or...

`use List::Util qw(sum);my \$total = sum grep { \$_ % 3 == 0 or \$_ % 5 == 0 } 1..999;`

or...

`use List::Util qw(sum);my \$total = sum grep \$_ % 15 ~~ [0,3,5,6,9,10,12], 1..999;`

### I didn't know that Perl could

I didn't know that Perl could do the 1..999 thing. Also, I'm going to have to look into that List::Util a bit as well. Thanks.

### or oneliner... perl -E 'for

or oneliner...

perl -E 'for (1..9) { \$s += \$_ unless \$_ % 5 && \$_ % 3 }; say \$s'

### That one liner is amazing!

That one liner is amazing! I'm going to have to remember that one. Thanks for sharing.

If you made it this far down into the article, hopefully you liked it enough to share it with your friends. Thanks if you do, I appreciate it.