This commit is contained in:
ldlda committed 2025-06-10 04:07:22 +07:00
commit b7f596588d
7 files changed
+515

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
//! where you think ts is from lmao
#![allow(unused)]
/// give me a fibonacci number NOW
pub fn fast2(n: u64) -> u64 {
let (mut a, mut b) = (0, 1);
let m = n;
let mut mask = (u64::MAX >> (m.leading_zeros() + 1)) + 1;
while mask > 0 {
let c = a * (2 * b - a);
let d = a * a + b * b;
if m & mask == 0 {
(a, b) = (c, d);
}
else {
(a, b) = (d, c + d);
}
mask >>= 1;
}
a
}
// 0, 1, 3, 7
// -> -1 /2 -1 /2 -1 /2
// ->
pub fn iterfib(n: u64) -> u64 {
let (mut a, mut b) = (0, 1);
for _ in 1..n {
(a, b) = (b, a + b);
}
b
}
/// ai code
pub fn fib(n: u64) -> u64 {
fn fib_pair(n: u64) -> (u64, u64) {
if (n) == 0 {
(0, 1)
} else {
let (a, b) = fib_pair(n / 2);
let c = a * (2 * b - a);
let d = a * a + b * b;
if n % 2 == 0 {
(c, d)
} else {
(d, c + d)
}
}
}
fib_pair(n).0
}
+19
View File
@@ -0,0 +1,19 @@
mod fib;
pub mod pyexport {
use pyo3::prelude::*;
/// this shit is the thing now in python
#[pyfunction]
fn fib(n: u64) -> u64 {
crate::fib::fast2(n)
// currently use u64 as a large number tupe, can use num_bigint
}
/// A Python module implemented in Rust.
#[pymodule(name = "lda_module")]
pub fn what_the_hell(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(fib, m)?)?;
Ok(())
}
}