Hello,
I have two different programs that calculate the Fibonacci sequence.
Code 1:
fn main() {
let mut number1=0;
let mut number2=1;
let mut number3=0;
for i in 2..10 {
number3=number1+number2;
println!("{}",number3);
number1=number2;
number2=number3;
}
}
Code 2:
fn main() {
let mut fib = [1; 10];
for i in 2..fib.len() {
fib[i] = fib[i - 2] + fib[i - 1];
}
for i in 0..fib.len() {
print!("{}, ", fib[i]);
}
}
How can I modify the following code to calculate the Fibonacci sequence without array?
fn main() {
let mut number=0;
for i in 2..10 {
number = (i-2) + (i-1);
println!("{}",number);
}
}
This is a math problem rather than a rust programming problem.
If you want to calculate fib(n) without calculate the full array, Here is one of such solution(description is in Chinese, but the code is useable.)
Further discussion could be found if you use google translate and read related answers carefully.
use rug::{Assign, Integer, integer::IntegerExt64};
fn fib(a:Integer)->Integer {
let mut idx=a.significant_bits_64();
let [mut coef_1,mut coef_5, mut c15, mut c1155, mut c55]=[Integer::from(2),Integer::from(0),Integer::from(0),Integer::from(0),Integer::from(0)];
while idx != u64::MAX {
std::thread::scope(|s|{
s.spawn(||c55=Into::<Integer>::into(&coef_5*&coef_5)*5);
s.spawn(||c15=(&coef_1*&coef_5).into());
s.spawn(||c1155=(&coef_1*&coef_1).into());
}); // multithreading is not necessary but it could speed up calculation.
c1155+=&c55;
if a.get_bit_64(idx as u64) {
coef_1.assign(&c15<<1);
coef_5.assign(&c1155+&coef_1);
coef_5>>=2;
coef_1+=&coef_5;
} else {
coef_1.assign(&c1155);
coef_1>>=1;
coef_5.assign(&c15);
}
idx-=1;
}
coef_5
}
fn main() {
for i in 0..100 {println!("{}",fib(Integer::from(i)))}
let now=std::time::Instant::now();
let x=fib(Integer::from(10_0000_0000)); // calculate it in several seconds.
println!("{:?}",now.elapsed());
println!("{}",x.mod_u64(1145141919));
}
I think he wants to calculate fib(n) without calculating the previous variable in the recursion/iteration.
Just like a math formula, as shown in @Neutron3529's answer.