[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
ITT We all do Project Euler in Rust
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 42
Thread images: 3
File: Rust General.jpg (35 KB, 1680x1260) Image search: [Google]
Rust General.jpg
35 KB, 1680x1260
>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);

}
}
>>
>>54748278

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));
}

>>
>>54748278
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));
}
>>
>>54748301
>>54749743
>>54749844
>>54750193
Holy shit Rust is verbose
>>
>>54748278
fn main() {
println!("{}", (0..1000).filter(|x| x % 3 == 0 && x % 5 == 0)
.fold(0, |acc, x| acc + x));
}
>>
>>54750217
Whoops, small mistake

fn main() {
println!("{}", (0..1000).filter(|x| x % 3 == 0 || x % 5 == 0)
.fold(0, |acc, x| acc + x));
}
>>
>>54748301
>>54749743
>>54749844
>>54750217
>>54750234
>Using iteration to solve an O(1) problem

see
>>54750193
>>
>>54748278
>>54749743
another

fn main() {
println!("{}", (0..999)
.filter(|i| i%3 == 0 || i%5 == 0)
.fold(0, |r, i| r + i));
}


>>54750206
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 /g/ will solve 1 single problem in 300 different ways.
>>
>>54750283
that is, 2^500500 divisors
>>
>>54750261
Yours is decent desu
>>
>>54750294
see FizzBuzz
>>
>>54748278
λ sum [x | x <- [1..999], (x `rem` 3) * (x `rem` 5) == 0]
233168
>>
>>54750261
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::<usize>());
}
>>
>>54750337
>might throw some simple error because my
tested, pls ignore
>>
>Rust
>>
>>54748301
>>54749743
>>54750261
>>54750337

You guys realize that upper bound of the range operator is exclusive? You're checking only the numbers below 999
>>
>>54750322
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
>>
>>54750322
What is this beauty?
>>
>>54750371
kek you're right
>>
>>54750372
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 <- subsequences xs, not (null c) ]
λ sumDivOr [3,5] 1000
233168
λ sumDivOr [3,5,7] 1000
271066
>>
>>54750322
>>54750372
>>54750400
typical illiterate memer
>>
>>54750416
I'm just here to show how much more elegantly you can rewrite every Rust program in Haskell
>>
>>54750322
>>54750372
>>54750400
Rustfags BTFO
>>
>>54750451
>elegantly
'ok'

regardless, I prefer my languages < 900 MiB
>>
>>54750521
languages don't have sizes
>>
File: 1435825139253.jpg (128 KB, 1024x682) Image search: [Google]
1435825139253.jpg
128 KB, 1024x682
>>54750619
not when it doesn't matter, no
>>
>>54750322
haskell really likes conciseness
[1..999] |> List.filter (fun i -> (i % 3)*(i % 5) = 0) |> List.sum
>>
>>54751107
val it : int = 233168
>>
>>54751107
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 <= 1000; ++i)
sum += (i % 3 == 0 || i % 5 == 0) ? i : 0;
writeln(sum);
}
>>
>>54748278
>40 replies for the first problem
>>
>>54749743
rust doesn't have a % operator, does it?
>>
Can someone shop the R to a J?
>>
>>54758521
do you think this is graphic design club?
Thread replies: 42
Thread images: 3

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.