Yeah, you were caught skipping class. Ignoring that it was to go to a lecture by a Nobel laureate (What? Learn something?), your wonderful administration has decided to sentence you to hard labor in a chain gang. This isn't just any chain gang, though; this one is run by the math department.You show up to find that you are to repeatedly calculate number chains, which are the repeating loops formed by subtracting the number with its digits sorted two different ways. Each worker has been given a set of numbers; any mistakes result in being given twice as many numbers on which to work. Naturally, you think, this kind of mindless work is perfect for a computer. No one notices when you slip out your laptop...
Start with the given positive integer. This is the first number in the chain. Sort the digits (of the base 10 number) into ascending order, and also in descending order. Subtract the ascending number from the descending number. This is the next number in the chain. Continue until you find a cycle.
The output should show each step in finding the chain along with both the length until the chain is identified and the length of the chain.
Example 2:
Enter an integer: 893
983 - 389 = 594
954 - 459 = 495
954 - 459 = 495
Length until repeat: 3
Length of chain: 1
Example 4:
Enter an integer: 932
932 - 239 = 693
963 - 369 = 594
954 - 459 = 495
954 - 459 = 495
Length until repeat: 4
Length of chain: 1
Example 5:
Enter an integer: 123456
654321 - 123456 = 530865
865530 - 35568 = 829962
998622 - 226899 = 771723
777321 - 123777 = 653544
655443 - 344556 = 310887
887310 - 13788 = 873522
875322 - 223578 = 651744
765441 - 144567 = 620874
876420 - 24678 = 851742
875421 - 124578 = 750843
875430 - 34578 = 840852
885420 - 24588 = 860832
886320 - 23688 = 862632
866322 - 223668 = 642654
665442 - 244566 = 420876
876420 - 24678 = 851742
Length until repeat: 16
Length of chain: 7
Example 6:
Enter an integer: 444
444 - 444 = 0
0 - 0 = 0
Length until repeat: 2
Length of chain: 1
Example 7:
Enter an integer: 69954
99654 - 45699 = 53955
95553 - 35559 = 59994
99954 - 45999 = 53955
Length until repeat: 3
Length of chain: 2