I checked with Ruby whether there is a Kaprekar number in an integer within a certain range

background

Until high school, I was good at math and loved it. However, when I entered a liberal arts university, I lived a life far from mathematics. And it's been 10 years since I became a member of society ... I'm a system engineer who has developed only the left brain, who has forgotten both calculus and trigonometric functions. Recently, thanks to YouTube, I have gradually increased my chances to come into contact with mathematics. It is a good time to solve entrance exam questions and feel free to touch the unknown world of university mathematics.

Today, about the integer "** Kaprekar number **" with interesting properties introduced in the following video We will verify using Ruby.

What is Kaprekar number?

The Kaprekar Number is an integer defined by one of the following:

  1. When squared and the sum is divided into the front part and the back part, it is equal to the original value.
  2. The value equal to the original value when the difference between the maximum and minimum digits is taken by rearranging the digits.

Source: Wikipedia

This time, we will look at Definition 2.

Take ** 495 ** as an example.

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

Such numbers are called Kaprekar numbers. The number of digits we usually see is only countable.

Now let's get into the main Ruby program.

Determine if any integer is Kaprekar

kaprekar.rb


num = 627

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

#Sort 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

#maximum-Determining if the minimum 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 the Kaprekar number is within a certain range

From here, let's use iterative processing to find out if there is a Kaprekar number within ** a particular 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

(For readability, I dare not use the ternary operator)

terminal


【495】 954 - 459 = 495
【6174】 7641 - 1467 = 6174
【549945】 995544 - 445599 = 549945
【631764】 766431 - 134667 = 631764

In the above example, when I tried using up to 6 digit integers, I found 4 Kaprekar numbers. It is also strange that it does not exist in 5 digits!

Digression

It depends on the specifications of the PC, but it will take a long time to process it repeatedly 1 million times.

Look at the number 495 introduced at the beginning You have a mathematical sense when you immediately come up with 495 = 99 × 5. Yes, all Kaprekar numbers are ** multiples ** of 9.

So even if you don't do it 1 million times, you can reduce the processing even more by verifying only multiples of 9.

If you have a better way to write it, let us know in the comments!

** (Added on 2020/06/29) ** I received a comment immediately and posted the improved version in the comment section!

Recommended Posts

I checked with Ruby whether there is a Kaprekar number in an integer within a certain range
I checked the number of taxis with Ruby
I made an interpreter (compiler?) With about 80 lines in Ruby.
How to change a string in an array to a number in Ruby
I searched for a web framework with Gem in Ruby
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
[Ruby] I want to put an array in a variable. I want to convert to an array
Is there no type in Ruby?
A program that determines whether the entered integer is close to an integer
I want to ForEach an array with a Lambda expression in Java
I tried a calendar problem in Ruby
I made a risky die with Ruby
How to check if an instance variable is defined in a Ruby class
How to start a subscript from an arbitrary number in Ruby iterative processing