Skip to content

Instantly share code, notes, and snippets.

@iwan
Last active March 1, 2016 16:05
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 iwan/d4d94396cc72f0bf15bc to your computer and use it in GitHub Desktop.
Save iwan/d4d94396cc72f0bf15bc to your computer and use it in GitHub Desktop.

Self-referential number (10 digit)

Which ten-digit number has the n-th digit representing how many occurrences of the digit (n−1) there are in the number, for all n≤10 ?

class Fixnum
def autoref?
a = to_s.rjust(10, "0").split("").map(&:to_i) # a = [2,3,1,1,3,4,5,5,1,9]
h = Hash[(0..9).map{|e| [e, 0]}]
a.each{|e| h[e]+=1}
a.each_with_index do |n, i|
return false if n!=h[i]
end
true
end
end
0.upto(10**10-1) do |n|
puts n if n%20_000==0
if n.autoref?
puts ">>>>#{n}<<<<<"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment