This challenge invites you to Generate kaprekar kernel or series from numbers.
What is this Kaprekar Series?
Kaprekar Series and Kaprekar Constants are numbers generated by applying the Kaprekar
Routine on a number!
Take any non-negative number. Now create two numbers, one with all the digits in ascending order and one with all the digits in descending order. Subtract the smaller number from the large number. Keep repeating these steps. You now have applied the Kaprekar routine on the original number!
On iterating, you'll reach:
- 0 (a degenerate case),
- a constant number (Kaprekar Constant), or
- a cycle of numbers (Kaprekar Series).
Read more on Kaprekar Series...
Database Source Control in just 5 minutes
It takes just 5 minutes to connect your SQL databases to source control. Got 5 minutes to spare?
Bring your database development process forward by 5 years.
Get started now.
Sample Data
Num
-----------------
2456
12343
445599995544
34534526262625263
Expected Results
Num Kernel Series
----------------- ----------------- --------------------------
2456 6174 NULL
12343 NULL 63954, 61974, 82962, 75933
445599995544 555499994445 NULL
34534526262625263 98765420987543211 NULL
Rules
- The output should be ordered by Num.
- The Num is BIGINT type and the number of digits in a Num column will not exceed 18.
- The data will be such that the result is never 0.
- There will be no duplicate numbers in the input data.
- Each series value will have to include, if necessary, leading zeroes to make it equal in length to the number of digits in the original number.
Sample Script
Use the TSQL Script given below to generate the source table and fill them with sample data.
IF OBJECT_ID('TC68','U') IS NOT NULL BEGIN
DROP TABLE TC68
END
GO
CREATE TABLE TC68(Num BIGINT)
GO
INSERT INTO TC68(Num)
SELECT 2456 UNION ALL
SELECT 12343 UNION ALL
SELECT 445599995544 UNION ALL
SELECT 34534526262625263
SELECT * FROM TC68
Restrictions
- The solution should be a single query that starts with a "SELECT" or “;WITH”
Notes
- Read the Submission Guidelines and make sure that your solution follows them.
- If you would like to use a Tally Table, you can use the script given here. Your solution should not include the script to create and populate the tally table. You can assume that the tally table will be available in the database where the evaluation team will run your Code.
Database Source Control in just 5 minutes
It takes just 5 minutes to connect your SQL databases to source control. Got 5 minutes to spare?
Bring your database development process forward by 5 years.
Get started now.