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.
Haskell:

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

Python:
  1. #!/usr/bin/python
  2.  
  3. def threeorfive(n):
  4. if ( n % 3 == 0 or n % 5 == 0):
  5. return True
  6. else:
  7. return False
  8.  
  9. def main():
  10. first_list = range(1,1000)
  11. second_list = filter(threeorfive, first_list)
  12. print "%s" % (sum(second_list))
  13.  
  14. if __name__ == "__main__":
  15. main()

Perl:
  1. #!/usr/bin/perl
  2.  
  3. my $count = 0;
  4. my $total = 0;
  5.  
  6. while ( $count < 1000)
  7. {
  8. if ( $count % 3 == 0 or $count % 5 == 0)
  9. {
  10. $total += $count;
  11. }
  12.  
  13. $count++;
  14. }
  15.  
  16. 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

Source: http://www.mycoding.net/2011/03/java-program-to-add-all-the-natural-numbers-below-one-thousand-that-are-multiples-of-3-or-5/

  1. public class Add
  2. {
  3. public static void main(String[] args)
  4. {
  5. int sum=0;
  6. int num = 0;
  7. System.out.println("This Java program will add all the natural numbers below one thousand that are multiples of 3 or 5.");
  8. while(num<999) //While loop keeps on running until num reaches 999
  9. {
  10. num++;
  11. 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
  12. {
  13. sum=sum+num;
  14. }
  15. }
  16. System.out.println("Sum is: "+ sum); //Printing the final output
  17. }
  18. }

roclafamilia

Helpful blog, bookmarked the website with hopes to read more!

Perl6

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

My Haskell Solution

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

Need double equals sign instead of single equals

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

Or...

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

or...

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

or...

  1. use List::Util qw(sum);
  2. 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.

Bookmark and Share