What are Kaprekar numbers ?
Kaprekar numbers are special numbers that were discovered by India mathematician Dattaraya Ramchandra Kaprekar. A more detailed explanation of Kaprekar and his numbers can be found here
http://en.wikipedia.org/wiki/Kaprekar_number
http://en.wikipedia.org/wiki/D._R._Kaprekar
This is a humble effort from my side to pay tribute to this great mathematician. Here is a Ruby routine to generate "n" Kaprekar numbers...
The output might look something like this...
Kaprekar numbers are special numbers that were discovered by India mathematician Dattaraya Ramchandra Kaprekar. A more detailed explanation of Kaprekar and his numbers can be found here
http://en.wikipedia.org/wiki/Kaprekar_number
http://en.wikipedia.org/wiki/D._R._Kaprekar
This is a humble effort from my side to pay tribute to this great mathematician. Here is a Ruby routine to generate "n" Kaprekar 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
#!/usr/bin/ruby | |
=begin | |
A Ruby routine to generate Kaprekar's numbers | |
Don't confuse with Kaperkar's constant - 6174 | |
=end | |
require 'optparse' | |
require 'ostruct' | |
class C_args | |
def initialize (arguments) | |
@m_ostruct = OpenStruct.new | |
@m_optparser = OptionParser.new | |
@m_args = arguments | |
end | |
def parse | |
@m_optparser.banner = "Usage: ./generate_kaprekar.rb -n <integer number>" | |
@m_optparser.on("-n VAL", Integer, "Range for kaprekar numbers") do |val| | |
@m_ostruct.num = val | |
end | |
@m_optparser.parse(@m_args) | |
if @m_args.empty? | |
puts @m_optparser | |
exit | |
end | |
return @m_ostruct | |
end | |
end | |
class C_gen_kaprekar | |
def initialize (number) | |
@number = number | |
end | |
def gen_kaprekar() | |
puts "Number -> #{@number}" | |
for i in 2..@number | |
n_square = i**2 | |
s_square = n_square.to_s | |
s_size = n_square.to_s.size | |
if (s_size % 2 == 0) | |
j = (s_size / 2) | |
k = (s_size / 2) | |
else | |
j = (s_size + 1) / 2 | |
k = ((s_size + 1) / 2) - 1 | |
end | |
s_n1 = s_square[0,k] | |
s_n2 = s_square[k,j] | |
i_n2 = s_n2.to_i | |
i_n1 = s_n1.to_i | |
if (i == i_n1 + i_n2) | |
puts "Kaprekar -> #{i}" | |
end | |
end | |
end | |
end | |
h_c_args = C_args.new(ARGV) | |
m1_ostruct= h_c_args.parse | |
puts "number limit = #{m1_ostruct.num}" | |
h_c_gen_kaprekar = C_gen_kaprekar.new(m1_ostruct.num) | |
h_c_gen_kaprekar.gen_kaprekar |
The output might look something like this...