Post Reply 
(33s) OEIS A4186: Digits in Descending Order
09-24-2022, 07:41 AM (This post was last modified: 09-24-2022 12:46 PM by C.Ret.)
Post: #2
RE: (33s) OEIS A4186: Digits in Descending Order
Hello,

Since I still have no HP-35S, here is a version for an HP-41C without any extra RAM or ROM module:

Code:
001 LBL "4186                                               // real integer input N  in register X:
002   CLRG                                    
003   LBL 01                                                // LOOP1: strip integer N digit by digit    
004     RCL X  10  ST/ Z  MOD  X<>Y  ISG 00
010     LBL 02                                              // LOOP2: sort digits from register R10 to R01   
011       X<> IND L  X<=Y?  X<>Y  X<> IND L  DSE L  GTO 02  //   R10:highest digit  R01:smallest digits (or zero)
017   INT  X>0?  GTO 01                                     // loop until no more digit in N
020   10  X<>Y
022   LBL 03                                                // LOOP3: build-up result from digits in R10 down to R0#
023     10  *  RCl IND Y  +  DSE Y  DEG  DSE 00  GTO 03
031 END                                                     // Result in X:  N'= jih...dcba

[ ][RTN]
1234 [R/S] ... goose ... goose ... goose ... 4321.
10477 [R/S] ... goose ... goose ... goose ... 77410.

This is a simplified version from a code published as one of the numerous solutions of the code challenge Re: Misez p'tit, Optimisez - N°85 (L'Algorithme de Kaprekar) on the French Forum Silicium (20 août 2018).

A close related code may produce both decreasing and increasing digits sorted integers from the input:

Code:
X: N=hdebcijadf
001 LBL "4186B             011       X<> IND L        021   SIGN                 031     X<> IND L
002   CLRG                 012       X<=Y?            022   X<> Z                032     ST+ Y
003   LBL 01               013       X<>Y             023   LBL 03               033     RDN
004     RCL X              014       X<> IND L        024     10                 034     DSE L
005     10                 015     DSE L              025     ST* Z              035     DEG
006     ST/ Z              016     GTO 02             026     X<> T              036   DSE 00
007     MOD                017     INT                027     ST* T              037   GTO 03
008     X<>Y               018   X>0?                 028     X<> IND L          038 END
009     ISG 00             019   GTO 01               029     ST+ Z         
010     LBL 02             020   10                   030     ST* IND L       X: abcdefghij   Y: jihgfedcba

[ ][RTN]
1342 [R/S] ... goose ... goose ... goose ... 1234. [X<>Y] 4321.
10477 [R/S] ... goose ... goose ... goose ... 1477. [X<>Y] 77410.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (33s) OEIS A4186: Digits in Descending Order - C.Ret - 09-24-2022 07:41 AM



User(s) browsing this thread: 1 Guest(s)