Created
August 19, 2012 02:22
-
-
Save NicMcPhee/3391055 to your computer and use it in GitHub Desktop.
Various solutions to Problem 1 in Project Euler (http://projecteuler.net/problem=1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; A Racket solution similar to the one-line Ruby solution. | |
(require racket/base) ; Needed for in-range | |
(require racket/sequence) ; Needed for sequence->list | |
(foldl + 0 | |
(filter (lambda (n) (or (= (remainder n 3) 0) | |
(= (remainder n 5) 0))) | |
(sequence->list (in-range 0 1000)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A one-line more "functional" solution. | |
puts (0...1000).select { |n| n % 3 == 0 or n % 5 == 0}.inject(:+) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# An imperative, loop-based solution. | |
result = 0 | |
1000.times do |n| | |
if n % 3 == 0 or n % 5 == 0 | |
result += n | |
end | |
end | |
puts result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment