solutions: 1-3
This commit is contained in:
commit
e250be74f0
16
even_fibonacci_numbers.rs
Normal file
16
even_fibonacci_numbers.rs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
fn main() {
|
||||||
|
let mut sequence: Vec<i32> = vec![1, 2];
|
||||||
|
while sequence[sequence.len()-1] <= 4000000 {
|
||||||
|
let mut last = sequence[sequence.len()-1];
|
||||||
|
let mut secondlast = sequence[sequence.len()-2];
|
||||||
|
(secondlast, last) = (last, secondlast+last);
|
||||||
|
sequence.push(last);
|
||||||
|
}
|
||||||
|
for i in sequence.clone() {
|
||||||
|
if i % 2 != 0 || i > 4000000 {
|
||||||
|
sequence.remove(sequence.iter().position(|x| *x == i).expect("Item not found"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let sum: i32 = sequence.iter().sum();
|
||||||
|
println!("{}", sum);
|
||||||
|
}
|
18
largest_prime_factor.rs
Normal file
18
largest_prime_factor.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
fn main() {
|
||||||
|
let mut num: u64 = 600851475143;
|
||||||
|
let mut factors: Vec<u64> = vec![];
|
||||||
|
let mut i: u64 = 2;
|
||||||
|
while i.pow(2) <= num {
|
||||||
|
if num % i == 0 {
|
||||||
|
factors.push(i);
|
||||||
|
num = (num as f64 / i as f64).floor() as u64;
|
||||||
|
} else {
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if num != 1 {
|
||||||
|
factors.push(num);
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{:?}", factors);
|
||||||
|
}
|
12
multiples_of_3_or_5.rs
Normal file
12
multiples_of_3_or_5.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
fn main() {
|
||||||
|
let mut multiples: Vec<i32> = vec![];
|
||||||
|
for num in 0..1000 {
|
||||||
|
if num % 3 == 0 {
|
||||||
|
multiples.push(num);
|
||||||
|
} else if num % 5 == 0 {
|
||||||
|
multiples.push(num);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let sum: i32 = multiples.iter().sum();
|
||||||
|
println!("{}", sum);
|
||||||
|
}
|
Loading…
Reference in a new issue