init
This commit is contained in:
7
problems/p25/Cargo.toml
Normal file
7
problems/p25/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "p25"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
num-bigint = "0.4.6"
|
||||
48
problems/p25/src/main.rs
Normal file
48
problems/p25/src/main.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use num_bigint::BigUint;
|
||||
use std::collections::HashMap;
|
||||
|
||||
struct FibCal {
|
||||
cache: HashMap<u64, BigUint>,
|
||||
}
|
||||
|
||||
impl FibCal {
|
||||
fn new() -> Self {
|
||||
let mut cache = HashMap::new();
|
||||
cache.insert(0, BigUint::from(0u32));
|
||||
cache.insert(1, BigUint::from(1u32));
|
||||
Self { cache }
|
||||
}
|
||||
|
||||
fn fib(&mut self, n: u64) -> BigUint {
|
||||
if let Some(cached) = self.cache.get(&n) {
|
||||
return cached.clone();
|
||||
}
|
||||
let result = self.fib(n - 1) + self.fib(n - 2);
|
||||
self.cache.insert(n, result.clone());
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fib() {
|
||||
let mut fib_cal = FibCal::new();
|
||||
|
||||
assert_eq!(fib_cal.fib(0), BigUint::from(0u32)); // F(0) = 0
|
||||
assert_eq!(fib_cal.fib(1), BigUint::from(1u32)); // F(1) = 1
|
||||
assert_eq!(fib_cal.fib(2), BigUint::from(1u32)); // F(2) = 1
|
||||
assert_eq!(fib_cal.fib(3), BigUint::from(2u32)); // F(3) = 2
|
||||
assert_eq!(fib_cal.fib(4), BigUint::from(3u32)); // F(4) = 3
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut term = 1u64;
|
||||
let mut fib_cal = FibCal::new();
|
||||
loop {
|
||||
let result = fib_cal.fib(term);
|
||||
if result.to_string().len() >= 1000 {
|
||||
break;
|
||||
}
|
||||
term += 1;
|
||||
}
|
||||
println!("{term}");
|
||||
}
|
||||
Reference in New Issue
Block a user