Skip to content

Instantly share code, notes, and snippets.

@NicMcPhee
Created August 19, 2012 02:22
Show Gist options
  • Save NicMcPhee/3391055 to your computer and use it in GitHub Desktop.
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)
; 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))))
# A one-line more "functional" solution.
puts (0...1000).select { |n| n % 3 == 0 or n % 5 == 0}.inject(:+)
# 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