Sum multiples of 3 and 5: Difference between revisions

Add a Limbo version
(→‎{{header|D}}: Add Déjà Vu example)
(Add a Limbo version)
Line 612:
The sum of multiples of 3 or 5 between 1 and 9999 is: 23331668
The sum of multiples of 3 or 5 between 1 and 99999 is: 2333316668</pre>
 
=={{header|Limbo}}==
 
Uses the IPints library when the result will be very large.
 
<lang Limbo>implement Sum3and5;
 
include "sys.m"; sys: Sys;
include "draw.m";
include "ipints.m"; ipints: IPints;
IPint: import ipints;
 
Sum3and5: module {
init: fn(nil: ref Draw->Context, args: list of string);
};
 
ints: array of ref IPint;
 
init(nil: ref Draw->Context, args: list of string)
{
sys = load Sys Sys->PATH;
ipints = load IPints IPints->PATH;
 
# We use 1, 2, 3, 5, and 15:
ints = array[16] of ref IPint;
for(i := 0; i < len ints; i++)
ints[i] = IPint.inttoip(i);
 
args = tl args;
while(args != nil) {
h := hd args;
args = tl args;
# If it's big enough that the result might not
# fit inside a big, we use the IPint version.
if(len h > 9) {
sys->print("%s\n", isum3to5(IPint.strtoip(h, 10)).iptostr(10));
} else {
sys->print("%bd\n", sum3to5(big h));
}
}
}
 
triangle(n: big): big
{
return((n * (n + big 1)) / big 2);
}
 
sum_multiples(n: big, limit: big): big
{
return(n * triangle((limit - big 1) / n));
}
 
sum3to5(limit: big): big
{
return(
sum_multiples(big 3, limit) +
sum_multiples(big 5, limit) -
sum_multiples(big 15, limit));
}
 
itriangle(n: ref IPint): ref IPint
{
return n.mul(n.add(ints[1])).div(ints[2]).t0;
}
 
isum_multiples(n: ref IPint, limit: ref IPint): ref IPint
{
return n.mul(itriangle(limit.sub(ints[1]).div(n).t0));
}
 
isum3to5(limit: ref IPint): ref IPint
{
return(
isum_multiples(ints[3], limit).
add(isum_multiples(ints[5], limit)).
sub(isum_multiples(ints[15], limit)));
}
</lang>
 
{{out}}
<pre>% sum3and5 1000 100000000000000000000
233168
2333333333333333333316666666666666666668</pre>
 
 
=={{header|Mathematica}}==
32

edits