Post Reply 
HHC 2019 will be in Reno, Nevada, Sept 21-22
09-25-2019, 07:26 PM
Post: #45
RE: HHC 2019 will be in Reno, Nevada, Sept 21-22
(09-24-2019 02:22 AM)dramsey Wrote:  Regarding this year's programming contest:

2. Also, I'd give less information on the solution. Several people "optimized" their programs by having them stop after four results were found, since I'd specifically said there were four numbers that satisfied the "three digit numbers equal to the sum of the cubes of their digits." This wasn't really what I was looking for but it was within the rules...

You also required that the four results be on the stack at the end, so after four were found it would be natural to stop. Pretending that I did not know there would be only four results, and therefore not knowing that they would fit on the stack so I had to come up with my own way to present the results, I came up with the program below, for 42S/DM42/Free42. It simply seqentially checks all 3 digit numbers, then stops and shows each successful result as it is found. The user must press R/S to continue, with "999" in the X register indicating all numbers have been checked and all successful results displayed.

Code:

001    LBL "HC19"    program label        
002    CF 29         suppress trailing decimal point In FIX 0        
003    FIX 0         fix for single digit display        
004    99.999        enter 99.999 for number generation and loop        
005    STO 00        store 99.999        
006    LBL 01        label for overall loop        
007    ISG 00        increment counter for 3-digit number to check        
008    GTO 04        not done, sum digits cubed and check        
009    STOP          done checking 100 to 999, X register shows 999 to indicate completion.        
010    LBL 04                
011    RCL 00        recall counter        
012    IP            take integer part to eliminate 999 after decimal        
013    CLA           clear alpha register        
014    ARCL ST X     store number whose digits to be cubed-summed in Alpha reg        
015    0             enter 0 to initialize sum of digits cubed        
016    LBL 03        label for branch to shift digits and Σ d^2        
017    ATOX          shift leftmost byte from ALPHA        
018    X=0?                  
019    GTO 00        if zero, done cubing and summing, go check if equal to original number        
020    48            not zero, so not done.  sum digit cubed.  first enter 48...
021    -            ...then subtract from extracted byte to convert to digit                
022    3             enter 3        
023    Y^X           cube digit        
024     +            add to running sum of cubed digits        
025    GTO 03        go back for next digit        
026    LBL 00        label to check if original number equal to sum of cubes        
027    X<>y          swap to get Σ d^3 in x register        
028    RCL 00        Recall orignal number        
029    IP            take integer part to eliminate 999 after decimal        
030    X!=Y?        check if not equal        
031    GTO 01        if not equal, go back for next number        
032    VIEW ST X     if equal, show X register         
033    STOP          stop to show result, user to press R/S to...
034    GTO 01        ...go back for next number        
035    END

(This program used the core of my program for Gene's "Happy Number" challenge of a few years ago which summed digits squared - so I had the code handy and it was easy to modify that to sum digits cubed. Also, yes, it could stop at 963, but that's not much of an optimization so I just go all the way to 999.)

Dave - My mind is going - I can feel it.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: HHC 2019 will be in Reno, Nevada, Sept 21-22 - Jeff O. - 09-25-2019 07:26 PM



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