Skip to content

Commit f57b575

Browse files
Optimize Fibonacci computation
1 parent 2d28204 commit f57b575

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

src/main.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ use anyhow::Result;
22
use clap::Parser;
33
use malachite::{
44
Integer,
5-
base::num::{
6-
arithmetic::traits::Square,
7-
basic::traits::{One, Two, Zero},
8-
},
5+
base::num::basic::traits::{One, Two, Zero},
96
};
107
use std::{io::{self, Write}, time::Instant};
118

@@ -50,8 +47,6 @@ fn parse_input(s: &str) -> Option<i32> {
5047
}
5148
}
5249

53-
const FOUR: Integer = Integer::const_from_unsigned(4);
54-
5550
fn fib(n: i32) -> Integer {
5651
if n == 0 {
5752
return Integer::ZERO;
@@ -68,7 +63,7 @@ fn fib(n: i32) -> Integer {
6863
let k = (n - 1) / 2;
6964
let a = fib(k);
7065
let b = fib(k - 1);
71-
return FOUR * a.square() - b.square() + Integer::from(-4 * (k % 2) + 2);
66+
return (Integer::TWO * &a + &b) * (Integer::TWO * &a - &b) + Integer::from(-4 * (k % 2) + 2);
7267
}
7368

7469
fn main() -> Result<()> {

0 commit comments

Comments
 (0)