Task

The objective is to write a function that finds the sum of all positive multiples of 3 or 5 below n.

Show output for n = 1000.

This is is the same as Project Euler problem 1.

Extra credit: do this efficiently for n = 1e20 or higher.




*        Sum multiples of 3 and 5
SUM35    CSECT
         USING  SUM35,R13          base register
         B      72(R15)            skip savearea
         DC     17F'0'             savearea
         STM    R14,R12,12(R13)    save previous context
         ST     R13,4(R15)         link backward
         ST     R15,8(R13)         link forward
         LR     R13,R15            set addressability
         LA     R9,1               n=1
         LA     R7,7               do j=7 to 1 step -1
LOOPJ    MH     R9,=H'10'            n=n*10
         LR     R10,R9               n
         BCTR   R10,0                n-1
         ZAP    SUM,=PL8'0'          sum=0
         LA     R6,3                 i=3
       DO WHILE=(CR,R6,LE,R10)       do i=3 to n-1
         LR     R4,R6                  i
         SRDA   R4,32
         D      R4,=F'3'               i/3
         LTR    R4,R4                  if mod(i,3)=0
         BZ     CVD
         LR     R4,R6                  i
         SRDA   R4,32
         D      R4,=F'5'               i/5
         LTR    R4,R4                  if  mod(i,5)=0
         BNZ    ITERI
CVD      CVD    R6,IP                  ip=p
         AP     SUM,IP                 sum=sum+i
ITERI    LA     R6,1(R6)               i++
       ENDDO    ,                    enddo i
         XDECO  R9,PG                n
         MVC    PG+15(16),EM16       load mask
         ED     PG+15(16),SUM        packed dec (PL8) to char (CL16)
         XPRNT  PG,L'PG              print
         BCT    R7,LOOPJ           enddo j
         L      R13,4(0,R13)       restore previous savearea pointer
         LM     R14,R12,12(R13)    restore previous context
         XR     R15,R15            rc=0
         BR     R14                exit
SUM      DS     PL8
IP       DS     PL8    		 
EM16     DC     X'40202020202020202020202020202120'  mask CL16 15num
PG       DC     CL80'123456789012 : 1234567890123456'
         YREGS
         END    SUM35