Quote:
Originally Posted by Beryllos
It wouldn't be difficult to modify the script to correctly test and print 1- and 2-digit numbers. (Although we know a posteriori that there are no 2-digit Armstrong numbers, for completeness we should test them and provide for their printing.) I'm not saying we must do it; perhaps this can be left as an exercise for the curious programmer.
|
I fixed it! It now correctly tests 1 and 2 digit numbers AND 4 digit numbers.
I also commented the code.
Code:
# A is the 1000s place, B is the 100s place, C is the 10s place and D is the 1s place
# am, bm, cm, dm are the multiplied version
VAR A
VAR am
VAR B
VAR bm
VAR C
VAR cm
VAR D
VAR AB
VAR ABC
VAR dm
VAR total
VAR sum
# sum is the sum of am + bm + cm + dm
# total tracks with the digit variables for easy comparison.
# This avoid needing to separate or combine digits
DO
# am = A ... etc. x ^ 1
ASSIGN am A
ASSIGN bm B
ASSIGN cm C
ASSIGN dm D
ADD AB A B
ADD ABC AB C
# We don't have an "or" operator so adding A+B+C and testing that is
# the same as if(A || B || C). If true, we know it's at least x^2 so square it
IF ABC
MULTIPLY bm bm
MULTIPLY cm cm
MULTIPLY dm dm
ENDIF
# if(A || B). We know it's either 3 or 4 digits so multiply again (x^3)
IF AB
MULTIPLY bm B
MULTIPLY cm C
MULTIPLY dm D
ENDIF
# if A (thousands place) is non-zero, it must be 4 digits so multiply again to
# make it x^4. Also we do all 4 multiplications on A here because it is our
# most significant digit so once it become non-zero, it will stay that way
IF A
MULTIPLY am am
MULTIPLY am A
MULTIPLY am A
MULTIPLY bm B
MULTIPLY cm C
MULTIPLY dm D
ENDIF
# Sum up the multiplied values
ASSIGN sum dm
ADD sum cm
ADD sum bm
ADD sum am
# If the sum of the digits raised to an exponent equal the original number
IF sum = total
# Don't print thousands place if it's zero (we don't want leading zeros)
IF A
# 48 is ASCII zero. Print only takes ASCII values and adding 48, printing
# and subtracting again is the most efficient way to do it
ADD A 48
# Print to the console
PRINT A
# Restore the original Value
SUBTRACT A 48
ENDIF
# If the hundreds place is zero, don't print it *unless* the thousands place is non-zero (i.e. for 1001)
IF AB
ADD B 48
PRINT B
SUBTRACT B 48
ENDIF
# Don't print the tens place if it's zero unless the hundreds or thousands place is non-zero
IF ABC
ADD C 48
PRINT C
SUBTRACT C 48
ENDIF
# Always print the ones place
ADD D 48
PRINT D
SUBTRACT D 48
# ASCII 10 is the newline character
PRINT 10
ENDIF
# Add 1 to the ones place
INC D
# Roll over when the ones place = 10
IF D > 9
# Zero out the ones place
CLEAR D
# Increment the tens place
INC C
ENDIF
# Rollover for the tens place
IF C > 9
CLEAR C
INC B
ENDIF
# And for the hundreds place
IF B > 9
CLEAR B
INC A
ENDIF
# Increment the total counter which will track with the split digit counter
INC total
# Loop back to the "DO" statement until we reach 9998
WHILE total != 9999
The changes produced a lot more BF code (about double):
Code:
>[-]+[>>>>>>>>>>>[-]<<<[-]>[<+>-]<[>+>>+<<<-]>>>>>>>[-]<<<[-]>[<+>-]<[>+>
>+<<<-]>>>>>>>>[-]<<<[-]>[<+>-]<[>+>>+<<<-]>>>>>>>>>>>>[-]<<<<<[-]<<[>>+<
<-]>>[<<+>>>>>>>+<<<<<-]>[-]<<<<<<<<<[-]<<<<<<<[>>>>>>>+<<<<<<<-]>>>>>>>[
<<<<<<<+>>>>>>>>>>>>>>>>+<<<<<<<<<-]<<<[>>>>+<<<<-]>>>>[<<<<+>>>>>>>>>>>>
+<<<<<<<<-]>>>>>>>>>>[-]<<<[-]>[<+>-]<[>+>>+<<<-]<<<<<<[>>>+<<<-]>>>[<<<+
>>>>>>>>>+<<<<<<-]<<[-]>>>>>[-]>>>[<<<+>>>-]<<<[>>>+<<<<<<<<+>>>>>-]<<<<<
[<<<<<[-]<<[-]>>>[<<<+>>>-]<<<[>>>+<+<<-][-]>>[<<<<[-]>>>>>[<<<<<+>>>>>-]
<<<<<[>>>>>+<<<+<<-]>>>>-]>[-]<<<[>>>+<<<-]>>>>>>>>>[-]<<<<[-]>>>[<<<+>>>
-]<<<[>>>+>+<<<<-]>>>>[<<<<<[-]>>>>[<<<<+>>>>-]<<<<[>>>>+<<<+<-]>>>>>-]<[
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<<<
<<<<+>>>>>>>>>>>>>>>>>>>>>>>>>>-]<<<<<<<<<<<<<<<<<<<<<<<<<<[>>>>>>>>>>>>>
>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+>>>>>-]<<<<[-]+++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++[<---------------------------------------------------------
------------------------------------------>-]<---------------------------
------------------------------------------------------------------------]
Needless to say it isn't too quick. There is a lot of room for optimization.
Mostly by organizing variables automatically to minimize the amount of times the pointer has to be shifted < >,
not clearing cells that are known to be zero (this is already the case in some places but not everywhere) and using secondard loops to compress the long strings of --- and +++.
And the output:
Code:
0
1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474