>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
>Find the sum of all the multiples of 3 or 5 below 1000.
>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
>Find the sum of all the multiples of 3 or 5 below 1000.
fn main() {
let mut list = vec![0];
for each in (0..999){
//if each %3 == 0 or each %5 ==0
// list.append(each)
// print sum(list)
println!("{}", each as i32 / 3 as i32);
}
}
fn main() {
let mut r = 0;
for i in 0..999 {
if i%3 == 0 || i%5 == 0 {
r += i;
}
}
println!("{}", r);
}
not perfect but should work:
fn main() {
let sum_multiples_of_x_up_to = |x, up_to| {
let mut y = x;
let mut res = 0;
while y < up_to {
res += y;
y += x;
}
res
};
print!("{}", sum_multiples_of_x_up_to(3, 1000) + sum_multiples_of_x_up_to(5, 1000) - sum_multiples_of_x_up_to(3*5, 1000));
}
This isn't a good programming puzzle since it can be instantly solved arithmetically.
actually, this also works without type problems, guess i didn't trust rust enough to do this without complaints:
fn main() {
let sum = |x, up_to| {
let n = (up_to - 1) / x;
x *n * (n+1) / 2
};
print!("{}", sum(3, 1000) + sum(5, 1000) - sum(3*5, 1000));
}
Holy shit Rust is verbose
fn main() {
println!("{}", (0..1000).filter(|x| x % 3 == 0 && x % 5 == 0)
.fold(0, |acc, x| acc + x));
}
Whoops, small mistake
fn main() {
println!("{}", (0..1000).filter(|x| x % 3 == 0 || x % 5 == 0)
.fold(0, |acc, x| acc + x));
}
>Using iteration to solve an O(1) problem
see
another
fn main() {
println!("{}", (0..999)
.filter(|i| i%3 == 0 || i%5 == 0)
.fold(0, |r, i| r + i));
}
it's really not though
>not doing a proper problem
this one's my favourite
The number of divisors of 120 is 16.
In fact 120 is the smallest number having 16 divisors.
Find the smallest number with 2500500 divisors.
Give your answer modulo 500500507.
instead of solving all problems Sup Forums will solve 1 single problem in 300 different ways.
that is, 2^500500 divisors
Yours is decent desu
see FizzBuzz
λ sum [x | x
and with nightly
might throw some simple error because my
#![feature(iter_arith)]
fn main() {
println!("{}", (0..999)
.filter(|i| i%3 == 0 || i%5 == 0)
.sum::());
}
>might throw some simple error because my
tested, pls ignore
>Rust
You guys realize that upper bound of the range operator is exclusive? You're checking only the numbers below 999
O(1) version
λ let sumDiv q n = q * n' * (n' + 1) `quot` 2 where n' = n `quot` q
λ sumDiv 3 999 + sumDiv 5 999 - sumDiv 15 999
233168
What is this beauty?
kek you're right
Generalized
λ let sumDiv q n = q * n' * (n' + 1) `quot` 2 where n' = (n-1) `quot` q
λ let sumDivOr xs n = sum [ (-1)^(length c - 1) * sumDiv (product c) n | c
typical illiterate memer
I'm just here to show how much more elegantly you can rewrite every Rust program in Haskell
Rustfags BTFO
>elegantly
'ok'
regardless, I prefer my languages < 900 MiB
languages don't have sizes
not when it doesn't matter, no
haskell really likes conciseness
[1..999] |> List.filter (fun i -> (i % 3)*(i % 5) = 0) |> List.sum
val it : int = 233168
Love me some ocaml.
List.fold (fun i acc -> if (i % 3 || i % 5) then i + acc else acc) 0 [1..999]
pub fn ex1() -> i32 {
(3..1000).filter(|x| x % 3 == 0 || x % 5 == 0).fold(0, |sum, x| sum + x)
}
1:
static void Main(string[] args)
{
int r = 0;
for (int i = 1; i < 1001; i++)
{
if ((i % 3 == 0) || (i % 5 == 0))
{
r += i;
}
}
Console.WriteLine(r);
Console.ReadLine();
}
2:
static void Main(string[] args)
{
ulong f = 1;
ulong s = 2;
ulong cont = 0;
ulong r = 0;
bool t = true;
while (t == true)
{
cont = f + s;
Console.WriteLine(cont);
if (cont % 2 == 0)
{
r += cont;
}
f = s;
s = cont;
if (cont >= 4000000)
{
t = false;
}
else
{
t = true;
}
}
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine(r);
Console.ReadLine();
}
>Inb4 >C#
>using Rust instead of D
import std.stdio;
import std.range;
import std.algorithm.iteration : filter, fold;
void main() {
writeln(iota(0,1000).filter!(i => i % 3 == 0 || i % 5 == 0).fold!((a,b) => a+b)(0));
}
non circle jerk answer
import std.stdio;
void main() {
uint sum;
for (int i=0; i
>40 replies for the first problem
bump
rust doesn't have a % operator, does it?
Can someone shop the R to a J?
do you think this is graphic design club?