|
| 1 | +/// 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: |
| 2 | +/// |
| 3 | +/// 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... |
| 4 | +/// |
| 5 | +/// By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. |
| 6 | +/// |
| 7 | +/// ```rust |
| 8 | +/// use self::project_euler::m2::sum_of_even_fibonacci_sequence_less_than_4000000; |
| 9 | +/// assert_eq!(sum_of_even_fibonacci_sequence_less_than_4000000(), 4613732); |
| 10 | +/// ``` |
| 11 | +pub fn sum_of_even_fibonacci_sequence_less_than_4000000() -> i64 { |
| 12 | + let fib = |prepre: i64, pre: i64| -> (i64, i64) { |
| 13 | + match (prepre, pre) { |
| 14 | + (0, 0) => (0, 1), |
| 15 | + (0, 1) => (1, 2), |
| 16 | + // (1, 2) => (2, 3), |
| 17 | + // (2, 3) => (3, 5), |
| 18 | + // (3, 5) => (5, 8), |
| 19 | + _ => (pre, prepre + pre), |
| 20 | + } |
| 21 | + }; |
1 | 22 |
|
| 23 | + let mut sum = 0; |
| 24 | + let mut prepre = 0i64; |
| 25 | + let mut pre = 0i64; |
| 26 | + loop { |
| 27 | + { |
| 28 | + let tuple = fib(prepre, pre); |
| 29 | + prepre = tuple.0; |
| 30 | + pre = tuple.1; |
| 31 | + } |
| 32 | + println!("num {}", pre); |
| 33 | + if pre > 4_000_000 { |
| 34 | + break; |
| 35 | + } |
| 36 | + if pre % 2 == 0 { |
| 37 | + sum += pre; |
| 38 | + } |
| 39 | + } |
| 40 | + sum |
| 41 | +} |
| 42 | + |
| 43 | +/// 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: |
| 44 | +/// |
| 45 | +/// (0, 1,)1,| 2, 3, 5,| 8, 13, 21,| 34, 55, 89, ... |
| 46 | +/// |
| 47 | +/// By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. |
| 48 | +/// |
| 49 | +/// ```rust |
| 50 | +/// use self::project_euler::m2::sum_of_even_fibonacci_sequence_less_than_4000000_011_235_8; |
| 51 | +/// assert_eq!(sum_of_even_fibonacci_sequence_less_than_4000000_011_235_8(), 4613732); |
| 52 | +/// ``` |
| 53 | +pub fn sum_of_even_fibonacci_sequence_less_than_4000000_011_235_8() -> i64 { |
| 54 | + let fib = |preprepre: i64, prepre: i64, pre: i64| -> (i64, i64, i64) { |
| 55 | + match (preprepre, prepre, pre) { |
| 56 | + (0, 1, 1) => (2, 3, 5), |
| 57 | + // (2, 3, 5) => ( 8, 13, 21), |
| 58 | + // (8, 13, 21) => (34, 55, 89), |
| 59 | + _ => { |
| 60 | + let post = prepre + pre; |
| 61 | + let postpost = pre + post; |
| 62 | + let postpostpost = post + postpost; |
| 63 | + (post, postpost, postpostpost) |
| 64 | + } |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + let mut sum = 0; |
| 69 | + let mut preprepre = 0i64; |
| 70 | + let mut prepre = 1i64; |
| 71 | + let mut pre = 1i64; |
| 72 | + loop { |
| 73 | + { |
| 74 | + let tuple = fib(preprepre, prepre, pre); |
| 75 | + preprepre = tuple.0; |
| 76 | + prepre = tuple.1; |
| 77 | + pre = tuple.2 |
| 78 | + } |
| 79 | + println!("num {}", preprepre); |
| 80 | + if preprepre > 4_000_000 { |
| 81 | + break; |
| 82 | + } |
| 83 | + if preprepre % 2 == 0 { |
| 84 | + sum += preprepre; |
| 85 | + } |
| 86 | + } |
| 87 | + sum |
| 88 | +} |
0 commit comments