Sum multiples of 3 and 5: Difference between revisions

m (syntax highlighting fixup automation)
(24 intermediate revisions by 12 users not shown)
Line 242:
100000000000000000000000000000 : 2333333333333333333333333333316666666666666666666666666668
1000000000000000000000000000000 : 233333333333333333333333333333166666666666666666666666666668</pre>
 
=={{header|ALGOL 60}}==
{{works with|A60}}
<syntaxhighlight lang="ALGOL">
begin
 
comment - return n mod m;
integer procedure mod(n,m);
value n, m; integer n, m;
begin
mod := n - m * entier(n / m);
end;
 
integer i, limit;
real sum;
 
limit := 1000;
sum := 0;
for i := 1 step 1 until (limit - 1) do
if mod(i, 3) = 0 or mod(i, 5) = 0 then
sum := sum + i;
outreal(1,sum);
 
end
</syntaxhighlight>
{{out}}
<pre>
233168
</pre>
 
=={{header|ALGOL 68}}==
Line 469 ⟶ 498:
{{out}}
<pre>233168</pre>
 
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="gwbasic"> 10 INPUT N
20 LET SUM = 0
30 FOR I = 3 TO N - 1
40 IF I / 3 = INT (I / 3) OR I / 5 = INT (I / 5) THEN SUM = SUM + I
50 NEXT I
60 PRINT SUM</syntaxhighlight>
 
==={{header|BASIC256}}===
Line 482 ⟶ 519:
print multSum35(999)
end</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">10 cls
20 print multsum35(1000)
30 end
40 function multsum35(n)
50 suma = 0
60 for i = 1 to n-1
70 if (i mod 3 = 0) or (i mod 5 = 0) then suma = suma+i
80 next i
90 multsum35 = suma
100 end function</syntaxhighlight>
 
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Public Sub Main()
Print "Sum of positive integers below 1000 divisible by 3 or 5 is : "; multSum35(999)
End
 
Function multSum35(n As Integer) As Integer
 
If n = 0 Then Return 0
Dim suma As Integer = 0
For i As Integer = 1 To n
If (i Mod 3 = 0) Or (i Mod 5 = 0) Then suma += i
Next
Return suma
 
End Function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|GW-BASIC}}===
{{works with|Applesoft BASIC}}
{{works with|Chipmunk Basic}}
{{works with|MSX BASIC|any}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">10 CLS : REM 10 HOME for Applesoft BASIC
20 LET N = 1000
30 GOSUB 60
40 PRINT S
50 END
60 REM multsum35
70 LET S = 0
80 FOR I = 1 TO N-1
90 IF I/3 = INT(I/3) OR I/5 = INT(I/5) THEN LET S = S+I : REM for Applesoft BASIC & Quite BASIC
90 IF (I MOD 3 = 0) OR (I MOD 5 = 0) THEN LET S = S+I : REM for MSX Basic & Chipmunk Basic
100 NEXT I
110 RETURN</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
<syntaxhighlight lang="qbasic">10 INPUT N
20 LET S = 0
30 FOR I = 3 TO N - 1
40 IF I / 3 = INT (I / 3) THEN 70
50 IF I / 5 = INT (I / 5) THEN 70
60 GOTO 80
70 LET S = S + I
80 NEXT I
90 PRINT S
100 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
See [[#GW-BASIC|GW-BASIC]].
 
==={{header|QBasic}}===
Line 507 ⟶ 611:
PRINT multSum35(999)
END</syntaxhighlight>
 
==={{header|Quite BASIC}}===
See [[#GW-BASIC|GW-BASIC]].
 
==={{header|Yabasic}}===
Line 1,305 ⟶ 1,412:
{{out}}
<pre>233168</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func msum35 n .
for i = 1 to n
if i mod 3 = 0 or i mod 5 = 0
sum += i
.
.
return sum
.
print msum35 999
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,721 ⟶ 1,841:
The sum of all the multiples of 3 or 5 below 1000 is 233168
The sum of all multiples less than 1e20 is 2333333333333333333316666666666666666668
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
_n = 1000
 
void local fn DoIt
long i, sum = 0
for i = 0 to _n - 1
if ( i mod 3 == 0 or i mod 5 == 0 )
sum += i
end if
next
NSLog(@"%ld",sum)
end fn
 
fn Doit
 
HandleEvents
</syntaxhighlight>
 
{{out}}
<pre>
233168
</pre>
 
Line 2,271 ⟶ 2,418:
100000000 -> 2333333316666668</pre>
 
=={{header|Joy}}==
<syntaxhighlight lang="jq">
DEFINE divisor == rem 0 = ;
mul3or5 == [3 divisor] [5 divisor] cleave or ;
when == swap [] ifte .
 
"The sum of the multiples of 3 or 5 below 1000 is " putchars
 
0 999 [0 =] [pop]
[
[dup rollup + swap] [mul3or5] when
pred
] tailrec .
</syntaxhighlight>
{{Out}}
<pre>
The sum of the multiples of 3 or 5 below 1000 is 233168
</pre>
=={{header|jq}}==
<syntaxhighlight lang="jq">
Line 2,286 ⟶ 2,451:
 
10e20 | task(3;5) # => 2.333333333333333e+41</syntaxhighlight>
 
 
=={{header|Julia}}==
Line 2,881 ⟶ 3,047:
</pre>
 
=={{header|OCamlOdin}}==
Note: 1e19 is the largest sum that can be calculated with 128 bit integers.
<syntaxhighlight lang="ocaml">let sum_mults n =
<syntaxhighlight lang="odin">
let sum = ref 0 in
package main
for i = 3 to (n - 1) do
if (i mod 3) = 0 || (i mod 5) = 0 then
sum := !sum + i;
done;
!sum;;
 
import "core:fmt"
print_endline (string_of_int (sum_mults 1000));;
 
sumdiv :: proc(n, d: i128) -> i128 {
m := n / d
return (m % 2 == 0)? \
m/2 * (m + 1) * d : \
(m + 1)/2 * m * d
}
 
sum3or5 :: proc(n: i128) -> i128 {
return sumdiv(n, 3) + sumdiv(n, 5) - sumdiv(n, 15)
}
 
main :: proc() {
sum := 0
for n in 1..=999 {
if n % 3 == 0 || n % 5 == 0 {
sum += n
}
}
fmt.println("The sum of all multiples of 3 and 5 < 1000 is", sum)
fmt.println("The sum of all multiples of 3 and 5 < 1e19 is", sum3or5(1e19 - 1))
}
</syntaxhighlight>
{{Out}}
<pre>
The sum of all multiples of 3 and 5 < 1000 is 233168
The sum of all multiples of 3 and 5 < 1e19 is 23333333333333333331666666666666666668
</pre>
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let sum_m3m5 n =
let termial x = (x * x + x) lsr 1 in
3 * (termial (n / 3) - 5 * termial (n / 15)) + 5 * termial (n / 5)
 
let () =
let pow10 x = truncate (10. ** (float x)) in
for i = 1 to 9 do
let u = pred (pow10 i) in
Printf.printf "Summing multiples of 3 or 5 in 1..%u: %u\n" u (sum_m3m5 u)
done</syntaxhighlight>
{{out}}
<pre>233168</pre>
Summing multiples of 3 or 5 in 1..9: 23
=== Functional programming version (more idiomatic) ===
Summing multiples of 3 or 5 in 1..99: 2318
Summing multiples of 3 or 5 in 1..999: 233168
Summing multiples of 3 or 5 in 1..9999: 23331668
Summing multiples of 3 or 5 in 1..99999: 2333316668
Summing multiples of 3 or 5 in 1..999999: 233333166668
Summing multiples of 3 or 5 in 1..9999999: 23333331666668
Summing multiples of 3 or 5 in 1..99999999: 2333333316666668
Summing multiples of 3 or 5 in 1..999999999: 233333333166666668
</pre>
=== With wheel increments (slower) ===
<syntaxhighlight lang="ocaml">
open Printf;;
Line 3,290 ⟶ 3,500:
The number of multiples of 3 or 5 below 10000000 is 23333331666668
The number of multiples of 3 or 5 below 100000000 is 2333333316666668</pre>
 
==={{header|PL/I-80}}===
Although a brute-force approach gets the job done with a minimum
of fuss, and would generally be the preferred first choice, the use of Gauss's
summation formula will significantly speed up running time if
summing to higher limits than required by the problem. The solution
here demonstrates both approaches
<syntaxhighlight lang = "PL/I">
sum35_demo: proc options (main);
dcl limit fixed bin;
limit = 1000;
put skip list ('Sum of all multiples of 3 and 5 below', limit);
put skip edit ('Sum = ', sum35(limit)) ((a),(f(6)));
put skip edit ('Also: ', sum35alt(limit)) ((a),(f(6)));
 
stop;
 
sum35:
proc(limit) returns (float bin);
dcl
(limit, i) fixed bin,
sum float bin;
sum = 0;
do i=1 to (limit-1);
if mod(i,3) = 0 | mod(i,5) = 0 then
sum = sum + i;
end;
return (sum);
end sum35;
 
sum35alt:
proc(limit) returns (float bin);
dcl
limit fixed bin,
sum float bin;
sum = sum_of_multiples(3, limit) +
sum_of_multiples(5, limit) -
sum_of_multiples(15, limit);
return (sum);
end sum35alt;
 
sum_of_multiples:
proc(n, limit) returns (float bin);
dcl
(n, limit, i) fixed bin,
m float bin;
m = (limit - 1) / n;
return (n * m * (m + 1) / 2);
end sum_of_multiples;
 
end sum35_demo;
</syntaxhighlight>
{{out}}
<pre>
Sum of all multiples of 3 and 5 below 1000
Sum = 233168
Also: 233168
</pre>
 
=={{header|PowerShell}}==
Line 3,920 ⟶ 4,188:
return n * (n + 1) / 2
</syntaxhighlight>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
===Simple solution===
Just counting...
≪ 0 1 DO
1 +
IF DUP 3 MOD NOT OVER 5 MOD NOT OR THEN SWAP OVER + SWAP END
UNTIL DUP 4 PICK == END
ROT DROP2
===Efficient solution===
This is a fast approach to calculate the sum for higher values of n, taking into account that:
# the sum of multiples of 3 and 5 is the sum of multiples of 3 plus the sum of multiples of 5, minus the sum of multiples of 15 to remove double counting
# the sum of multiples of m being < n is equal to m*(1+2+ ... k) with k =[n/m]
# 1+2+...k = k*(k+1)/2
Unfortunately, RPL can only handle double precision numbers, which prevents from precisely calculating the sum for n > 1E+15
≪ → n
≪ 0 1 3 FOR j
{ 3 5 -15 } j GET
n OVER / ABS IP
DUP 1 + * 2 / * +
NEXT
≫ ≫
‘SUM35’ STO
 
999 SUM35
{{out}}
<pre>
1: 233168
</pre>
 
=={{header|Ruby}}==
Line 4,048 ⟶ 4,347:
 
</pre>
 
=={{header|S-BASIC}}==
<syntaxhighlight lang="basic">
rem - return n mod m
function mod(n, m = integer) = integer
end = n - m * (n / m)
 
var i, limit = integer
var sum = real
 
limit = 1000
sum = 0
for i = 1 to limit-1
if (mod(i,3) = 0) or (mod(i, 5) = 0) then
sum = sum + i
next i
print using "Sum = #######"; sum
 
end
</syntaxhighlight>
{{out}}
<pre>
Sum = 233168
</pre>
 
 
=={{header|Scala}}==
Line 4,348 ⟶ 4,672:
23333331666668,23333331666668
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|Uiua}}==
<syntaxhighlight>
End ← 1000
 
MultOfthree ← ⊚=0◿3 # indices where divisible by 3
Indicesthree ← MultOfthree ↘1⇡End
MultOffive ← ⊚=0◿5 # indices where divisible by 5
IndicesFive ← MultOffive ↘1⇡End
 
/+ ⊏⊂ Indicesthree IndicesFive ↘1⇡End # join, select and sum
</syntaxhighlight>
{{out}}
233168
</pre>
 
Line 4,479 ⟶ 5,033:
endmodule</syntaxhighlight>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="go">fn s35(n int) int {
Line 4,513 ⟶ 5,067:
=={{header|Wren}}==
===Simple version===
<syntaxhighlight lang="ecmascriptwren">var sum35 = Fn.new { |n|
n = n - 1
var s3 = (n/3).floor
Line 4,535 ⟶ 5,089:
{{libheader|Wren-gmp}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./gmp" for Mpz
import "./fmt" for Fmt
 
Line 4,643 ⟶ 5,197:
 
=={{header|Zig}}==
Note that solving for 1e20 requires numbers > 128 bits. However, Zig supports fixed size integers up to 65,556 bits, and with Zig, it's possible to figure out at compile-time what the maximum width of an integer should be at run-time.
Inclusion/Exclusion mapping i64 -> i128 (largest integers supported in Zig natively)
<syntaxhighlight lang="zig">
const std = @import("std");
const stdout = std.io.getStdOut().writer();
 
fn sumdivDoubleWide(comptime n: i64, d: i64anytype) i128type {
const Signedness = std.builtin.Signedness;
var m: i128 = @divFloor(n, d);
switch (@typeInfo(@TypeOf(n))) {
.Int => |t|
return std.meta.Int(t.signedness, t.bits * 2),
.ComptimeInt => {
const sz = @as(u16, @intFromFloat(@log2(@as(f64, @floatFromInt(n))))) + 1;
return std.meta.Int(Signedness.signed, sz * 2);
},
else =>
@compileError("must have integral type for DoubleWide")
}
}
 
fn sumdiv(n: anytype, d: anytype) DoubleWide(n) {
var m: DoubleWide(n) = @divFloor(n, d);
return @divExact(m * (m + 1), 2) * d;
}
 
fn sum3or5(n: i64anytype) i128DoubleWide(n) {
return sumdiv(n, 3) + sumdiv(n, 5) - sumdiv(n, 15);
}
 
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
try stdout.print("The sum of the multiples of 3 and 5 below 1000 is {}\n", .{sum3or5(999)});
 
try stdout.print("The sum of the multiples of 3 and 5 below 1e18 is {}\n", .{sum3or5(999_999_999_999_999_999)});
var s: usize = 0;
for (1..1000) |n| {
if (n % 3 == 0 or n % 5 == 0)
s += n;
}
try stdout.print("The sum of the multiples of 3 and 5 below 1000 is {}\n", .{s});
try stdout.print("The sum of the multiples of 3 and 5 below 1e20 is {}\n", .{sum3or5(99_999_999_999_999_999_999)});
}
 
</syntaxhighlight>
{{Out}}
<pre>
The sum of the multiples of 3 and 5 below 1000 is 233168
The sum of the multiples of 3 and 5 below 1e181e20 is 2333333333333333331666666666666666682333333333333333333316666666666666666668
 
</pre>
=={{header|zkl}}==
2

edits