Skip to content

Instantly share code, notes, and snippets.

Created Jun 11, 2013
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"
puts "#{number} is NOT a Kaprecar number"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment