Skip to content

Commit

Permalink
m2
Browse files Browse the repository at this point in the history
  • Loading branch information
growingspaghetti committed Jul 21, 2020
1 parent 108ed5d commit a773f66
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/m2.rs
Original file line number Diff line number Diff line change
@@ -1 +1,88 @@
/// 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.
///
/// ```rust
/// use self::project_euler::m2::sum_of_even_fibonacci_sequence_less_than_4000000;
/// assert_eq!(sum_of_even_fibonacci_sequence_less_than_4000000(), 4613732);
/// ```
pub fn sum_of_even_fibonacci_sequence_less_than_4000000() -> i64 {
let fib = |prepre: i64, pre: i64| -> (i64, i64) {
match (prepre, pre) {
(0, 0) => (0, 1),
(0, 1) => (1, 2),
// (1, 2) => (2, 3),
// (2, 3) => (3, 5),
// (3, 5) => (5, 8),
_ => (pre, prepre + pre),
}
};

let mut sum = 0;
let mut prepre = 0i64;
let mut pre = 0i64;
loop {
{
let tuple = fib(prepre, pre);
prepre = tuple.0;
pre = tuple.1;
}
println!("num {}", pre);
if pre > 4_000_000 {
break;
}
if pre % 2 == 0 {
sum += pre;
}
}
sum
}

/// 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:
///
/// (0, 1,)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.
///
/// ```rust
/// use self::project_euler::m2::sum_of_even_fibonacci_sequence_less_than_4000000_011_235_8;
/// assert_eq!(sum_of_even_fibonacci_sequence_less_than_4000000_011_235_8(), 4613732);
/// ```
pub fn sum_of_even_fibonacci_sequence_less_than_4000000_011_235_8() -> i64 {
let fib = |preprepre: i64, prepre: i64, pre: i64| -> (i64, i64, i64) {
match (preprepre, prepre, pre) {
(0, 1, 1) => (2, 3, 5),
// (2, 3, 5) => ( 8, 13, 21),
// (8, 13, 21) => (34, 55, 89),
_ => {
let post = prepre + pre;
let postpost = pre + post;
let postpostpost = post + postpost;
(post, postpost, postpostpost)
}
}
};

let mut sum = 0;
let mut preprepre = 0i64;
let mut prepre = 1i64;
let mut pre = 1i64;
loop {
{
let tuple = fib(preprepre, prepre, pre);
preprepre = tuple.0;
prepre = tuple.1;
pre = tuple.2
}
println!("num {}", preprepre);
if preprepre > 4_000_000 {
break;
}
if preprepre % 2 == 0 {
sum += preprepre;
}
}
sum
}

1 comment on commit a773f66

@growingspaghetti
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rust-lang/rfcs#372 Destructuring assignment #372

Please sign in to comment.