Hi all,

I heard about rust as a cool language and trying to learn it now.

I write now nodejs and want a trick behind my sleeves when nodejs becomes to slow.

As i try to learn rust, i thought that i will try to solve project euler questions with rust.

I took the following problem https://projecteuler.net/problem=23

I have two files:

```
mod euler;
use euler::primes::sum_proper_divisors;
use std::collections::HashSet;
fn ex23() {
let mut res:u64 = 1;
let mut abundant: HashSet<u16> = HashSet::new();
for i in 2..28124{
{
let ab = &abundant;
let is_sum_of = ab.into_iter().filter(|&x| ab.contains(&(i -x))).next();
if is_sum_of == None {
res = res + (i as u64);
}
}
let sum_divisors = sum_proper_divisors(i as u32);
if sum_divisors > (i as u32) {
abundant.insert(i);
}
}
```

and euler/primes.rs

```
#[derive(Debug)]
struct PrimeDiv {
prime : u32,
count : u8
}
fn prime_factors(mut n:u32) -> Vec<PrimeDiv> {
let mut res: Vec<PrimeDiv> = vec![];
let root = (n as f64).sqrt().floor() as u32;
let mut i = 2;
while n>1 && i<=root{
if n%i ==0 {
let mut curr_prime = PrimeDiv{prime:i, count: 1};
n = n/i;
while n%i ==0 {
curr_prime.count = curr_prime.count +1;
n = n/i;
}
res.push(curr_prime);
}
i = i+1;
}
if n>1 && res.len()>0 {
res.push(PrimeDiv{prime:n, count: 1});
}
return res;
}
pub fn sum_proper_divisors(n:u32) -> u32 {
let mut sum = 1;
let prime_facts = prime_factors(n);
let mut helper_arr = vec![0; prime_facts.len()];
if prime_facts.len() ==0 {
return 0;
}
let last_index = prime_facts.len() - 1;
let mut divisor = 1;
loop {
let mut index =0;
while helper_arr[index] == prime_facts[index].count && index <last_index {
divisor = divisor / prime_facts[index].prime.pow(prime_facts[index].count as u32);
helper_arr[index] = 0;
index = index +1;
}
helper_arr[index] = helper_arr[index]+1;
divisor = divisor * prime_facts[index].prime;
if divisor ==n {
break;
}
//println!("divisor:{} index:{} helper: {} p:{}", divisor, index, helper_arr[index],prime_facts[index].count);
sum = sum + divisor;
}
return sum;
}
```

The program and the algorithms and very simple.

I was very impressed by the memory consumption.

It did not take more than 1.5 MB to run.

There were two topics which i am not sure i am doing the right way:

- The conversion stuff. For example here: let root = (n as f64).sqrt().floor() as u32; This is so ugly. I did not find a better way to do so because the functions are missing. But why isnâ€™t there a sqrt function on integers and why float does not return an integer. Is there a better way to do so?
- The running time was 4s. I can try to make the algorithm better and trade space for time, but for a system language 4s is an eternity. Do you see here any grave error?

Thanks,

David