LinuxQuestions.org Printing Armstrong numbers from 1 to 500. (C Programming)
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 07-20-2017, 10:03 AM #136 Beryllos Member   Registered: Apr 2013 Location: Massachusetts Distribution: Debian Posts: 529 Rep: 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.
07-22-2017, 01:35 AM   #137
smeezekitty
Senior Member

Registered: Sep 2009
Location: Washington U.S.
Distribution: M\$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep:
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

# 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
# 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
# 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
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
PRINT C
SUBTRACT C 48
ENDIF
# Always print the ones place
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```

1 members found this post helpful.
 07-22-2017, 10:57 PM #138 Beryllos Member   Registered: Apr 2013 Location: Massachusetts Distribution: Debian Posts: 529 Rep: Well done! Edit: Oh no! When I run the BF code, it outputs only 0 through 9. I tried a few different BF interpreters but the results are the same. Any ideas? Last edited by Beryllos; 07-22-2017 at 11:24 PM. Reason: troubleshooting
07-23-2017, 01:49 AM   #139
smeezekitty
Senior Member

Registered: Sep 2009
Location: Washington U.S.
Distribution: M\$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep:
Quote:
 Originally Posted by Beryllos Well done! Edit: Oh no! When I run the BF code, it outputs only 0 through 9. I tried a few different BF interpreters but the results are the same. Any ideas?
You need a BF interpreter that uses 16 bit (or 32 bit cells). Because otherwise the 8 bit cells will overflow when you raise it to the third.(it doesn't support longs -- yet).

Try this one.
Make sure you select 16 bit cells in the sidebar.

1 members found this post helpful.
07-23-2017, 08:14 AM   #140
Beryllos
Member

Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep:
Quote:
 Originally Posted by smeezekitty You need a BF interpreter that uses 16 bit (or 32 bit cells). Because otherwise the 8 bit cells will overflow when you raise it to the third.(it doesn't support longs -- yet)...
Yes, that was it. It works fine.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post bauer172uw Programming 3 04-13-2006 11:10 PM Gigantor Programming 8 12-05-2005 10:32 PM mrobertson Programming 1 06-28-2005 08:19 AM vijeesh_ep Programming 18 09-08-2004 11:59 AM fisheromen1031 Programming 1 07-27-2004 02:19 PM

LinuxQuestions.org

All times are GMT -5. The time now is 02:40 PM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -