solutions: 14
This commit is contained in:
parent
b7c61ccef8
commit
f9a42d16e7
1 changed files with 33 additions and 0 deletions
33
longest_collatz_sequence.rs
Normal file
33
longest_collatz_sequence.rs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
fn collatz_sequence(starting_num: u64) -> Vec<u64> {
|
||||||
|
let mut num = starting_num;
|
||||||
|
let mut result: Vec<u64> = vec![];
|
||||||
|
|
||||||
|
result.push(num);
|
||||||
|
|
||||||
|
while num != 1 {
|
||||||
|
if num % 2 == 0 {
|
||||||
|
num /= 2;
|
||||||
|
result.push(num);
|
||||||
|
} else {
|
||||||
|
num = (3*num)+1;
|
||||||
|
result.push(num);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut increment: u64 = 0;
|
||||||
|
let mut longest: u64 = 0;
|
||||||
|
|
||||||
|
for i in 1..=1000000 {
|
||||||
|
let length = collatz_sequence(i).len() as u64;
|
||||||
|
if length > longest {
|
||||||
|
longest = length;
|
||||||
|
increment = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{:?}", increment);
|
||||||
|
}
|
Loading…
Reference in a new issue