fibonacci_sequence/
lib.rs1pub struct FibonacciValue {
2 pub index: u8,
3 pub fib_value: u128,
4}
5pub mod lib_fibonacci {
6 pub use super::*;
7 pub fn kernel(mut n: u8) -> FibonacciValue {
16 let mut value_before = FibonacciValue {
17 index: 0,
18 fib_value: 1,
19 };
20 let mut value_now = FibonacciValue {
21 index: 0,
22 fib_value: 1,
23 };
24 let mut exchange = FibonacciValue {
25 index: 0,
26 fib_value: 1,
27 };
28 let mut index: u8 = 0;
29 n = n - 1;
30 while n > index {
31 exchange = FibonacciValue {
32 index: index,
33 fib_value: value_before.fib_value + value_now.fib_value,
34 };
35 value_before = value_now;
36 value_now = exchange;
37 index += 1;
38 }
39 return value_now;
40 }
41 pub fn kernel_recursion(n: u8) -> FibonacciValue {
52 if n == 0 {
54 return FibonacciValue {
55 index: 0,
56 fib_value: 1,
57 };
58 }
59 if n == 1 {
60 return FibonacciValue {
61 index: 0,
62 fib_value: 1,
63 };
64 }
65
66 return FibonacciValue {
67 index: n,
68 fib_value: kernel_recursion(n - 1).fib_value + kernel_recursion(n - 2).fib_value,
69 };
70 }
71 fn abs(number: i128) -> i128 {
73 if number > 0 {
74 return number;
75 } else {
76 return -number;
77 }
78 }
79
80 fn involution(mut number: f64, mut times: i8) -> f64 {
81 let n: f64 = number;
82 while abs(times.into()) > 0 {
83 number *= n;
84 times -= 1
85 }
86 if times == 0 {
87 return match number {
88 0.0 => 0.0,
89 _ => 1.0,
90 };
91 }
92 return number;
93 }
94
95 pub fn kernel_linear_algebra(n: u8) -> f64 {
96 let result: f64 = (involution(1.618035, n.try_into().unwrap())
97 - involution(-0.61803399, n.try_into().unwrap()))
98 * 0.4472136;
99 return result;
100 }
101}
102
103pub trait Iterator {
104 fn next(&mut self) -> FibonacciValue;
105}
106
107impl Iterator for FibonacciValue {
108 fn next(&mut self) -> FibonacciValue {
109 return lib_fibonacci::kernel(self.index+1);
110 }
111}