Getting Started with Adobe After Effects - Part 6: Motion Blur
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.

TSQL Challenge 68 - Generate kaprekar kernel or series from numbers

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:

  1. 0 (a degenerate case),
  2. a constant number (Kaprekar Constant), or
  3. 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

  1. The output should be ordered by Num.
  2. The Num is BIGINT type and the number of digits in a Num column will not exceed 18.
  3. The data will be such that the result is never 0.
  4. There will be no duplicate numbers in the input data.
  5. 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

  1. The solution should be a single query that starts with a "SELECT" or “;WITH”

Notes

  1. Read the Submission Guidelines and make sure that your solution follows them.
  2. 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.

Leszek Gniadkowski
8 · 18% · 5729
Submit a Solution
Previous Challenge
Next Challenge
3
Liked
28
Comments
29
Solutions



Submit

TSQL Challenge 68 - Generate kaprekar kernel or series from numbers

Managed Windows Shared Hosting by OrcsWeb

Copyright © Rivera Informatic Private Ltd.