Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. Number Chain
  2. Description:
  3. Given a number, we can form a number chain by:
  4. 1. Arranging its digits in descending order.
  5. 2. Arranging its digits in ascending order.
  6. 3. Subtracting the number obtained in (2) from the number obtained in (1) to form a new number.
  7. 4. Repeating these steps until the new number matches the result from the previous calculation.
  8.  
  9. For example given an original number of 1234, the process would be:
  10. 4321 - 1234 = 3087
  11. 8730 - 378 = 8352
  12. 8532 - 2358 = 6174
  13. 7641 - 1467 = 6174
  14. Chain length = 4
  15.  
  16. 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.
  17.  
  18. For example, the number 84723 can go through more than 1000 iterations and still not reach the ending point.
  19. In cases like this, stop after 1000 iterations and indicate the chain length is greater than 1000 as shown in the sample output below.
  20.  
  21. Test Data Input Output
  22. 1234
  23.  
  24. original number was 1234 and chain length = 4 123456789 original number was 123456789 and chain length = 2
  25. 84723
  26.  
  27. original number was 84723 and chain length > 1000 8080 original number was 8080 and chain length = 7
  28. 9235
  29.  
  30. original number was 9235 and chain length = 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement