Skip to content

Instantly share code, notes, and snippets.

@nilium

nilium/nope.rb Secret

Last active August 29, 2015 13:57
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 nilium/d2fbec2e3705f5e38069 to your computer and use it in GitHub Desktop.
Save nilium/d2fbec2e3705f5e38069 to your computer and use it in GitHub Desktop.
# Attempts to compute all possible numbers in (0, 1000) that are:
# - even
# - only contain the digits { 0 1 3 4 6 7 9 }
# - both with repeating and non-repeating digits (all digits are unique)
[1,!1].map{|u|(?2..'996').step(2).select{|i|v=731;i.chars.all?{|k|k=1<<k.to_i;v&k!=0&&(!u||v&=~k)}}}
def numbers_containing_digits(range, digits, uniq = false)
digits = digits.chars if digits.kind_of? String
range.select do |i|
i.even? && i.to_s.chars.each_with_object(digits.dup).all? do |k, valids|
uniq ? valids.delete(k) : valids.include?(k)
end
end
end
POSSIBLE = "0134679"
NUM_RANGE = 1 ... 1000
numbers_containing_digits(NUM_RANGE, POSSIBLE, false)
numbers_containing_digits(NUM_RANGE, POSSIBLE, true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment