034 Digit factorials.sf 381 B

1234567891011121314151617181920
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # https://github.com/trizen
  5. # Find the sum of all numbers which are equal to the sum of the factorial of their digits.
  6. # https://projecteuler.net/problem=34
  7. # Runtime: 1.636s (previously: 2.673s)
  8. var f = (0..9 -> map {|d| d! })
  9. var sum = (3..1e5 `by` 2 -> grep { |n|
  10. n.digits.sum_by { f[_] } == n
  11. }.sum)
  12. say sum