Sum multiples of 3 and 5: Difference between revisions
Content added Content deleted
(Add TI SR-56 solution) |
(Odin version) |
||
Line 2,915: | Line 2,915: | ||
</pre> |
</pre> |
||
=={{header|Odin}}== |
|||
Note: 1e19 is the largest sum that can be calculated with 128 bit integers. |
|||
<syntaxhighlight lang="odin"> |
|||
package main |
|||
import "core:fmt" |
|||
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}}== |
=={{header|OCaml}}== |
||
<syntaxhighlight lang="ocaml">let sum_m3m5 n = |
<syntaxhighlight lang="ocaml">let sum_m3m5 n = |