Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Number Chain
- Description:
- Given a number, we can form a number chain by:
- 1. Arranging its digits in descending order.
- 2. Arranging its digits in ascending order.
- 3. Subtracting the number obtained in (2) from the number obtained in (1) to form a new number.
- 4. Repeating these steps until the new number matches the result from the previous calculation.
- For example given an original number of 1234, the process would be:
- 4321 - 1234 = 3087
- 8730 - 378 = 8352
- 8532 - 2358 = 6174
- 7641 - 1467 = 6174
- Chain length = 4
- The number of distinct numbers in the chain is the length of the chain. You are to write a program that reads numbers and displays the length of the chain for each number read. Some numbers may not result in a new number that matches the result from the previous calculation.
- For example, the number 84723 can go through more than 1000 iterations and still not reach the ending point.
- In cases like this, stop after 1000 iterations and indicate the chain length is greater than 1000 as shown in the sample output below.
- Test Data Input Output
- 1234
- original number was 1234 and chain length = 4 123456789 original number was 123456789 and chain length = 2
- 84723
- original number was 84723 and chain length > 1000 8080 original number was 8080 and chain length = 7
- 9235
- original number was 9235 and chain length = 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement