List comprehension in Haskell, Python and Ruby

前端 未结 5 979
天命终不由人
天命终不由人 2021-01-01 21:26

I have started looking at the project Euler site as a way to learn Haskell, and improve my Python and Ruby. I think the Haskell and Python versions are ok, but I\'m sure the

5条回答
  •  悲&欢浪女
    2021-01-01 21:45

    Try something like this:

    (1...1000).inject(0) do |sum, i|
      if (i % 3 == 0) or (i % 5 == 0)
        sum + i
      else
        sum
      end
    

提交回复
热议问题