Browse Source

problem 25 cleanup

canon
Hazel Levine 1 year ago
parent
commit
c42a255478
  1. 8
      src/bin/prob25.rs

8
src/bin/prob25.rs

@ -10,15 +10,15 @@ use rug::{ops::Pow, Float, Integer};
const PHI: f64 = 1.618_033_988_749_895;
// https://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression
// n(F) = floor(log_phi(F * sqrt(5) + 1/2))
// uses change of base as GMP doesn't implement log of a certain base
fn fib_idx(n: Integer) -> Integer {
let phi = Float::with_val(64, PHI);
let to_ln: Float = n * Float::with_val(64, 5f64.sqrt()) + 0.5;
let ln_x: Float = to_ln.ln();
let ln_phi: Float = phi.ln();
// would lose type inference at this point
let x: Float = n * Float::with_val(64, 5f64.sqrt()) + 0.5;
(ln_x / ln_phi).floor().to_integer().unwrap() + 1
(x.ln() / phi.ln()).floor().to_integer().unwrap() + 1
}
fn compute(n: u32) -> Integer {

Loading…
Cancel
Save