# Kaprekar number

In mathematics, a Kaprekar number for a given base is a non_negative_integer, the representation of whose square in that base can be split into two parts that add up to the original number again. For instance, 297 is a Kaprekar number, because 97² = 88209 and 88+209 = 297. The Kaprekar numbers are named after D. R. Kaprekar.

How can you determine if a number is a Kaprekar number in Ruby?

```
def kaprekar?(num)
no_of_digits = num.to_s.size
square = (num ** 2).to_s

second_half = square[-no_of_digits..-1]
first_half = square.size.even? ? square[0..no_of_digits-1] : square[0..no_of_digits-2]

num == first_half.to_i + second_half.to_i
end

```

Okay, lets fire up IRB and step through this code….

```
.0.0-p247 :016 > num = 297
=> 297
2.0.0-p247 :018 > square = (num ** 2).to_s
=> "88209"
2.0.0-p247 :019 > second_half = square[-no_of_digits..-1]
=> "209"
2.0.0-p247 :020 > first_half = square.size.even? ? square[0..no_of_digits-1] : square[0..no_of_digits-2]
=> "88"
2.0.0-p247 :021 > num == first_half.to_i + second_half.to_i
=> true

```

Now, I think the people who discover these things have too much times on their hands! 