Showing posts with label Mathematics. Show all posts
Showing posts with label Mathematics. Show all posts

Monday, April 7, 2014

A Ruby routine to generate Kaprekar numbers

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...
#!/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...

Tuesday, December 17, 2013

On the mathematics of verification

Many of you would wonder why I am writing after more than 3 years. My last post was way back in 2010. Well I am still in the business of ASIC verification.  These years had been immensely profound and educating. When you get a glimpse of infinity, all our lifetime's learning appear to be a drop of water in the ocean.

But the quest for knowledge still goes on. I may not be able to ever understand the workings of this universe, but I still want to pursue the quest, even if it is for the mundane.

So in continuation to my last post "on the mathematics of assertions", I have got some really concrete insight into the problem from a much broader perspective. The problem of verification is far more complex than we perceive through dynamic simulations. There is a profound mathematical basis for the behavior of complex boolean systems. I have got a hold on the mathematics, but there is lot of work to be done before I can come to any publishable conclusion.