Skip to content

Instantly share code, notes, and snippets.

@caius
Last active July 29, 2020 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caius/1489f7b45a7fe18e5fed052070f7f161 to your computer and use it in GitHub Desktop.
Save caius/1489f7b45a7fe18e5fed052070f7f161 to your computer and use it in GitHub Desktop.
# five digit number
# two of the digits are the same
# four of the digits are odd
# number is higher than 40010
# number is higher than 45000
# digit sum is 16
# five digit, upper/lower bounds
candidates = (40011..44999).select { |i|
# two digits are the same
digits = i.to_s.split("")
digits.uniq.size == 4
}.select { |i|
# digit sum
i.to_s.split("").map(&:to_i).inject(:+) == 16
}.select { |i|
# four digits are odd
digits = i.to_s.split("").map(&:to_i)
digits.count(&:odd?) == 4
}
candidates
# => [41137,
# 41173,
# 41317,
# 41335,
# 41353,
# 41371,
# 41533,
# 41713,
# 41731,
# 43117,
# 43135,
# 43153,
# 43171,
# 43315,
# 43351,
# 43513,
# 43531,
# 43711]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment