Sum multiples of 3 and 5: Difference between revisions

Add TI SR-56 solution
(Add TI SR-56 solution)
Line 4,424:
2333333333333333333316666666666666666668
</pre>
 
=={{header|TI SR-56}}==
=== Iterative solution ===
{| class="wikitable"
|+ Texas Instruments SR-56 Program Listing for "Sum Multiples" (Iterative)
|-
! Display !! Key !! Display !! Key !! Display !! Key !! Display !! Key
|-
| 00 22 || GTO || 25 33 || STO || 50 || || 75 ||
|-
| 01 04 || 4 || 26 00 || 0 || 51 || || 76 ||
|-
| 02 04 || 4 || 27 57 || *subr || 52 || || 77 ||
|-
| 03 52 || ( || 28 00 || 0 || 53 || || 78 ||
|-
| 04 52 || ( || 29 03 || 3 || 54 || || 79 ||
|-
| 05 34 || RCL || 30 12 || INV || 55 || || 80 ||
|-
| 06 00 || 0 || 31 37 || *x=t || 56 || || 81 ||
|-
| 07 54 || / || 32 03 || 3 || 57 || || 82 ||
|-
| 08 05 || 5 || 33 08 || 8 || 58 || || 83 ||
|-
| 09 53 || ) || 34 34 || RCL || 59 || || 84 ||
|-
| 10 12 || INV || 35 00 || 0 || 60 || || 85 ||
|-
| 11 29 || *Int || 36 35 || SUM || 61 || || 86 ||
|-
| 12 64 || x || 37 01 || 1 || 62 || || 87 ||
|-
| 13 52 || ( || 38 27 || *dsz || 63 || || 88 ||
|-
| 14 34 || RCL || 39 02 || 2 || 64 || || 89 ||
|-
| 15 00 || 0 || 40 07 || 7 || 65 || || 90 ||
|-
| 16 54 || / || 41 34 || RCL || 66 || || 91 ||
|-
| 17 03 || 3 || 42 01 || 1 || 67 || || 92 ||
|-
| 18 53 || ) || 43 41 || R/S || 68 || || 93 ||
|-
| 19 12 || INV || 44 74 || - || 69 || || 94 ||
|-
| 20 29 || *Int || 45 01 || 1 || 70 || || 95 ||
|-
| 21 53 || ) || 46 94 || = || 71 || || 96 ||
|-
| 22 58 || *rtn || 47 22 || GTO || 72 || || 97 ||
|-
| 23 56 || *CP || 48 02 || 2 || 73 || || 98 ||
|-
| 24 38 || *CMs || 49 03 || 3 || 74 || || 99 ||
|}
 
Asterisk denotes 2nd function key.
 
{| class="wikitable"
|+ Register allocation
|-
| 0: Current Term || 1: Sum Of Terms || 2: Unused || 3: Unused || 4: Unused
|-
| 5: Unused || 6: Unused || 7: Unused || 8: Unused || 9: Unused
|}
 
Annotated listing:
<syntaxhighlight lang="text">
// Address 00: Entry point.
GTO 4 4
 
// Address 03: Subroutine
// If R0 is divisible by 3 and 5, return 0, else nonzero
( ( RCL 0 / 5 ) INV *Int x ( RCL 0 / 3 ) INV *Int ) *rtn
 
// Address 23: Main program
*CP *CMs // Zero all registers and T register.
STO 0 // R0 := Maximum number to consider.
*subr 0 3 // Check divisibility by 3 and 5.
INV *x=t 3 8 // Divisible?
RCL 0
SUM 1 // R1 += R0
*dsz 2 7 // R0--, repeat while nonzero.
RCL 1 // Retrieve answer.
R/S // End.
 
// Address 44: Input parsing
- 1 = // Consider all numbers *less than* N.
GTO 2 3
</syntaxhighlight>
 
'''Usage:'''
 
{{in}}
 
<pre>1000 RST R/S</pre>
 
{{out}}
 
<pre>233168</pre>
 
This took between 20 and 30 minutes to run.
 
=== Efficient closed-form solution ===
 
{| class="wikitable"
|+ Texas Instruments SR-56 Program Listing for "Sum Multiples" (Closed-form)
|-
! Display !! Key !! Display !! Key !! Display !! Key !! Display !! Key
|-
| 00 22 || GTO || 25 29 || *Int || 50 54 || / || 75 ||
|-
| 01 01 || 1 || 26 57 || *subr || 51 01 || 1 || 76 ||
|-
| 02 06 || 6 || 27 00 || 0 || 52 05 || 5 || 77 ||
|-
| 03 33 || STO || 28 03 || 3 || 53 94 || = || 78 ||
|-
| 04 01 || 1 || 29 64 || x || 54 29 || *Int || 79 ||
|-
| 05 54 || / || 30 05 || 5 || 55 57 || *subr || 80 ||
|-
| 06 02 || 2 || 31 94 || = || 56 00 || 0 || 81 ||
|-
| 07 64 || x || 32 35 || SUM || 57 03 || 3 || 82 ||
|-
| 08 52 || ( || 33 02 || 2 || 58 64 || x || 83 ||
|-
| 09 34 || RCL || 34 34 || RCL || 59 01 || 1 || 84 ||
|-
| 10 01 || 1 || 35 00 || 0 || 60 05 || 5 || 85 ||
|-
| 11 84 || + || 36 54 || / || 61 94 || = || 86 ||
|-
| 12 01 || 1 || 37 03 || 3 || 62 12 || INV || 87 ||
|-
| 13 53 || ) || 38 94 || = || 63 35 || SUM || 88 ||
|-
| 14 53 || ) || 39 29 || *Int || 64 02 || 2 || 89 ||
|-
| 15 58 || *rtn || 40 57 || *subr || 65 34 || RCL || 90 ||
|-
| 16 38 || *CMs || 41 00 || 0 || 66 02 || 2 || 91 ||
|-
| 17 74 || - || 42 03 || 3 || 67 41 || R/S || 92 ||
|-
| 18 01 || 1 || 43 64 || x || 68 || || 93 ||
|-
| 19 94 || = || 44 03 || 3 || 69 || || 94 ||
|-
| 20 33 || STO || 45 94 || = || 70 || || 95 ||
|-
| 21 00 || 0 || 46 35 || SUM || 71 || || 96 ||
|-
| 22 54 || / || 47 02 || 2 || 72 || || 97 ||
|-
| 23 05 || 5 || 48 34 || RCL || 73 || || 98 ||
|-
| 24 94 || = || 49 00 || 0 || 74 || || 99 ||
|}
 
Asterisk denotes 2nd function key.
 
{| class="wikitable"
|+ Register allocation
|-
| 0: Maximum Term || 1: Parameter || 2: Answer || 3: Unused || 4: Unused
|-
| 5: Unused || 6: Unused || 7: Unused || 8: Unused || 9: Unused
|}
 
Annotated listing:
<syntaxhighlight lang="text">
// Address 00: Entry point.
GTO 1 6
 
// Address 03: Subroutine
// Calculates sum of nums 1-N as (N/2)(N+1).
STO 1
/ 2 x ( RCL 1 + 1 ) ) *rtn
 
// Address 16: Main program
*CMs // Clear registers.
- 1 = STO 0 // R0 := N-1
/ 5 = *Int *subr 0 3 x 5 = SUM 2 // R2 += fives
RCL 0 / 3 = *Int *subr 0 3 x 3 = SUM 2 // R2 += threes
RCL 0 / 1 5 = *Int *subr 0 3 x 1 5 = INV SUM 2 // R2 -= fifteens
RCL 2 // Retrieve answer.
R/S // End.
</syntaxhighlight>
 
'''Usage:'''
 
{{in}}
 
<pre>1000 RST R/S</pre>
 
{{out}}
 
<pre>233168</pre>
 
Completed in 4 seconds.
 
{{in}}
 
<pre>1 EE 20 RST R/S</pre>
 
{{out}}
 
<pre>2.333333333 39</pre>
 
Completed in 4 seconds.
 
=={{header|UNIX Shell}}==
27

edits