Bryce Verdier: Project Euler: Problem 5
Posted on | July 29, 2010 | No Comments
It's that time again. ;)
Question five reads:
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
And here are my solutions (which I hope are correct this time).
Haskell:
module Main where import Data.List then Nothing else Just(x,x - 1)) 20) -- solved this by the help on this URL: -- (http)basildoncoder.com/blog/2008/06/10/project-euler-problem-5/ -- by increaing the loop from 2 to 2520, problem solves in seconds
As a quick note, yes I know that I could do this much quicker and cleaner with a list comprehension. I decided to use unfoldr because I wanted the experience of working with it. If it wasn't for this little desire my answer would have looked a lot more like my Python answer.
Python:
#!/usr/bin/python def div_11_to_20(divided): return all([not bool(divided % x) for x in xrange(11,20+1)]) if __name__ == "__main__": count = 2520 while div_11_to_20(count) == False: count += 2520 print "%s" % count
And finally my Perl solution:
#!/usr/bin/perl sub divide_11_to_20 { my ( $divided ) = @_; foreach (11..20) { } } my $main_count = 2520; while ( !divide_11_to_20($main_count) ) { $main_count += 2520; }
Run Times:
Haskell: 0m1.302s
Python: 0m0.769s
Perl: 0m0.223s
Observations:
It doesn't surprise me that the Perl solution ends up being the fastest on these run times. I say this because per iteration the Perl solutions has less calculations. Both the Haskell and Python solutions perform 10 divisions per iteration, where as the Perl solution only performs 10 divisions when the correct number is is being divided. Its one of those things where the difference is very small, but will become larger as the number of iterations increases.
Comments
Leave a Reply
Warning: include(/home/remarkwit/enterpriselamp.org/wp-content/themes/Enterprise_LAMP/r_sidebar.php) [function.include]: failed to open stream: No such file or directory in /home/remarkwit/enterpriselamp.org/wp-content/themes/Enterprise_LAMP/index.php on line 36
Warning: include() [function.include]: Failed opening '/home/remarkwit/enterpriselamp.org/wp-content/themes/Enterprise_LAMP/r_sidebar.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/remarkwit/enterpriselamp.org/wp-content/themes/Enterprise_LAMP/index.php on line 36