Skip to content

Instantly share code, notes, and snippets.

@jonelf
Created June 29, 2011 15:32
Show Gist options
  • Save jonelf/1054098 to your computer and use it in GitHub Desktop.
Save jonelf/1054098 to your computer and use it in GitHub Desktop.
Self-descriptive numbers
# Searches for self-descriptive numbers
# http://en.wikipedia.org/wiki/Self-descriptive_number
module Enumerable
def all_with_index?
each_with_index{|e,i| return false unless yield(e,i)}
true
end
end
puts "Self-descriptive numbers"
(0..21212).each do |number|
number_array=number.to_s.split(//).map(&:to_i)
self_descriptive = number_array.all_with_index? do |v,i|
number_array.count(i)==v
end
puts number.to_s if self_descriptive
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment