Skip to content

Instantly share code, notes, and snippets.

@mark
Created May 12, 2011 01:47
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 mark/967785 to your computer and use it in GitHub Desktop.
Save mark/967785 to your computer and use it in GitHub Desktop.
Digital Roots for 999
PEOPLE = [1, 2, 3, 4, 5, 6, 7, 8]
def subset(set)
if set.empty?
[[]]
else
first = set[0]
rest = set[1..-1]
subset(rest) + subset(rest).map { |x| x << first }
end
end
def sum(set)
set.inject(0) { |s, i| s + i }
end
def digital_root(set)
s = sum(set)
return s if s < 10
new_set = s.to_s.split('').map { |x| x.to_i }
digital_root new_set
end
all_groups = subset(PEOPLE).sort
valid_groups = all_groups.select { |p| p.length >= 3 && p.length <= 5 }
groups_without_junpei = valid_groups.reject { |g| g.include?(5) }
groups_with_snake = groups_without_junpei.select { |g| g.include?(2) }
groups_that_can_enter_3 = groups_with_snake.select { |g| digital_root(g) == 3 }
groups_that_can_enter_3.each { |g| puts g.inspect }
puts '*******************'
groups_without_snake = valid_groups.reject { |g| g.include?(2) }
groups_with_junpei = groups_without_snake.select { |g| g.include?(5) }
groups_with_june = groups_with_junpei.select { |g| g.include?(6) }
groups_that_can_enter_9 = groups_with_june.select { |g| digital_root(g) == 9 }
# groups_that_can_enter_9.each { |g| puts g.inspect }
puts '*******************'
base_groups = valid_groups.select { |g| digital_root(g) == 9 }
PEOPLE.each do |i|
containing = base_groups.select { |g| g.include?(i) }.length
puts "#{i}\t#{containing.to_f / base_groups.length}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment