Sum multiples of 3 and 5: Difference between revisions

Go solution. Another one sitting around on my hard disk.
(→‎{{header|MATLAB}} / {{header|Octave}}: There is no arbitrary precision support in MATLAB and Octave)
(Go solution. Another one sitting around on my hard disk.)
Line 680:
1000000000000000000 233333333333333333166666666666666668 ok
</lang>
 
=={{header|Go}}==
<lang go>package main
 
import "fmt"
 
func main() {
fmt.Println(s35(1000))
}
 
func s35(i int) int {
i--
sum2 := func(d int) int {
n := i / d
return d * n * (n + 1)
}
return (sum2(3) + sum2(5) - sum2(15)) / 2
}</lang>
{{out}}
<pre>
233168
</pre>
Extra credit:
<lang go>package main
 
import (
"fmt"
"math/big"
)
 
var (
b1 = big.NewInt(1)
b3 = big.NewInt(3)
b5 = big.NewInt(5)
b10 = big.NewInt(10)
b15 = big.NewInt(15)
b20 = big.NewInt(20)
)
 
func main() {
fmt.Println(s35(new(big.Int).Exp(b10, b3, nil)))
fmt.Println(s35(new(big.Int).Exp(b10, b20, nil)))
}
 
func s35(i *big.Int) *big.Int {
j := new(big.Int).Sub(i, b1)
sum2 := func(d *big.Int) *big.Int {
n := new(big.Int).Quo(j, d)
p := new(big.Int).Add(n, b1)
return p.Mul(d, p.Mul(p, n))
}
s := sum2(b3)
return s.Rsh(s.Sub(s.Add(s, sum2(b5)), sum2(b15)), 1)
}</lang>
{{out}}
<pre>
233168
2333333333333333333316666666666666666668
</pre>
 
=={{header|Groovy}}==
1,707

edits