Skip to content

Instantly share code, notes, and snippets.

@inkel
Created July 27, 2012 14:06
Show Gist options
  • Save inkel/3188211 to your computer and use it in GitHub Desktop.
Save inkel/3188211 to your computer and use it in GitHub Desktop.
Números cachipulis
#!/usr/bin/env ruby
param = ARGV.first
class Fixnum
def is_cachipulis?
length = self.to_s.length
digits = self.to_s.split('').map(&:to_i)
return false if digits.inject(&:+) != length
digits.each_with_index do |d, i|
unless digits.count(i) == d
return false
end
end
true
end
def self.cachipulis
min, max = 1000, 2000
while min < 1_000_000_000
n = min.to_s.split('').map(&:to_i)
unless n.inject(&:+) > n.length
(min..max).each do |i|
yield i if i.is_cachipulis?
end
end
min, max = max + 1, max + 1000
end
end
end
if param == "lista"
Fixnum.cachipulis do |number|
puts number
end
else
if param.to_i.is_cachipulis?
puts 'Si'
else
puts 'No'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment