Table of content

What is this?

The rules of the game are explained in my original post.

13th Challenge

Challenge

Euler Challange 1

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.

Euler Challange 2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Euler Challange 3

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

Solution

	fn main() {
	    euler1();
	    euler2();
	    euler3();
	}

	fn euler1(){
	    let x= (0..1000)
	        .filter(|x| (x%3 == 0 )| (x%5 == 0))
	        .sum::<u32>();

	    println!("sum of all multiples {:?}",x);
	}

	fn euler2(){

	    let mut sum = 2;
	    let mut sum_temp ;
	    let mut sum_prev = 1;
	    let mut sum_even = 0;

	    while sum < 4000000 {

	        if sum % 2 == 0 {
	            sum_even += sum;
	        }

	        sum_temp = sum;
	        sum = sum_prev + sum;
	        sum_prev = sum_temp;

	    }

	    println!("sum of all even fibonacci numbers up to {} is {:?}",40000000,sum_even);

	}

	fn euler3(){

	    const NUM:u64 = 600851475143;
	    let factors = trial_division(NUM);
	    println!("biggest primal factor:{:?}",factors.last().unwrap());
	}

	fn trial_division(n:u64) -> Vec<u64>{
	    let mut v = Vec::new();
	    let mut n = n;
	    let mut factor = 2;
	    while n > 1 {
	        if n % factor == 0 {
	            n = n / factor as u64;
	            v.push(factor);
	        } else {
	            factor += 1;
	        }
	    }
	    v
	}
	

See github and playground