[Ruby] I checked with Ruby whether there is a Caprecha number in an integer within a certain range

3 minute read

Background

Up to high school, I was good at math and loved it. However, when I entered a liberal arts university, I lived a life far from math. And it’s been 10 years since I became a member of society… I have forgotten both calculus and trigonometric functions, but I am a system engineer who has developed only the left brain, Recently, thanks to YouTube, I have had more and more opportunities to come into contact with math. It’s a good time to solve entrance exam questions and to feel the unknown world of university mathematics.

About the integer “** Capreca number **” with interesting properties introduced in the following video today I will verify using Ruby.

What is the number of capreka

Kaprekar number is an integer defined by one of the following.

  1. Squared and divided into the front part and the back part and the sum is equal to the original value.
  2. A value that is equal to the original value when the digit is rearranged and the difference between the maximum value and the minimum value is taken.

Source: Wikipedia

This time, we will look at definition 2.

For example, consider 495.

The maximum number that can be made by rearranging 495 is 954, and the minimum number is 459. 954-459 = 495, so it’s equal to the original number 495! It is interesting!

This number is called the Capreca number, There are only as many digits as we normally see.

Now let’s get into the main Ruby program.

Determine if any integer is a Capreca number

kaprekar.rb


num = 627

Store in # array
arr = num.to_s.split('').map!(&:to_i)
p arr # => [6, 2, 7]

# Rearrange to make maximum and minimum
max = arr.sort{ |a, b| a <=> b }.join('').to_i
min = arr.sort{ |a, b| b <=> a }.join('').to_i
p max # => 762
p min # => 267

# Max-Determine if min is the same as the original number
diff = max-min
p diff == num # => false

Now by rewriting num, you can judge only one arbitrary integer.

Determine if Capreca number is within a certain range

Let’s iterate from here to find out if there is a Capreka number within a specific range.

kaprekar.rb


result = []
num = 1
final = 1_000_000

while num <final do
  arr = num.to_s.split('').map!(&:to_i)
  min = arr.sort{ |a, b| a <=> b }.join('').to_i
  max = arr.sort{ |a, b| b <=> a }.join('').to_i
  diff = max-min

  if diff == num
    result.push("[#{num}] #{max}-#{min} = #{diff}")
  end

  num += 1
end

if result.empty?
  result.push("Not applicable")
end

puts result

(The ternary operator is not used for readability.)

terminal


[495] 954-459 = 495
[6174] 7641-1467 = 6174
[549945] 995544-445599 = 549945
[631764] 766431-134667 = 631764

In the above example, I tried up to a 6-digit integer and found 4 Capreka numbers. It’s also strange that it doesn’t exist in 5 digits!

Digression

Although it depends on the specifications of the PC, it will take quite a while if it is repeatedly processed 1 million times.

Looking at the number 495 introduced at the beginning, 495 = 99 × 5 came up immediately, you have a mathematical sense. Yes, all capreca numbers are multiples of 9.

So even if you don’t do it a million times, you can reduce the processing even more by verifying only a multiple of 9.

If there is a better way to write it, please let us know in the comments!

** (Added June 29, 2020) ** I received a comment immediately and posted the improved version in the comment section!