Created
June 29, 2011 15:32
-
-
Save jonelf/1054098 to your computer and use it in GitHub Desktop.
Self-descriptive numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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