Skip to content

Instantly share code, notes, and snippets.

@gerep
Created Jun 11, 2013
Embed
What would you like to do?
[Blogger] Check if a given number is a kaprekar one
number = ARGV[0].to_i
size = number.to_s.size
square = (number**2).to_s
first_half = square.size.even? ? square[0..size-1] : square[0..size-2]
second_half = square[-size..-1]
if number == first_half.to_i + second_half.to_i || square.size == 1 && number == square.to_i
puts "#{number} is a Kaprecar number"
else
puts "#{number} is NOT a Kaprecar number"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment