using the superior declarative module + add more to test

+ const for no reason (some functions cant even be const
This commit is contained in:
lda committed 2025-06-10 17:02:42 +07:00
1 parent 69a48bc711
commit c690410c8c
7 files changed
+188 -33

No files matched your search

+20 -7
View File
@@ -2,10 +2,10 @@
#![allow(unused)]
/// give me a fibonacci number NOW
pub fn fast2(n: u64) -> u64 {
pub const fn fast2(n: u32) -> u64 {
let (mut a, mut b) = (0, 1);
let m = n;
let bit_len = u64::BITS - m.leading_zeros(); // copilot
let bit_len = u32::BITS - m.leading_zeros(); // copilot
let mut mask = if bit_len == 0 { 0 } else { 1 << (bit_len - 1) };
while mask > 0 {
let c = a * (2 * b - a);
@@ -22,16 +22,17 @@ pub fn fast2(n: u64) -> u64 {
// 0, 1, 3, 7
// -> -1 /2 -1 /2 -1 /2
// ->
pub fn iterfib(n: u64) -> u64 {
pub const fn iterfib(mut n: u32) -> u64 {
let (mut a, mut b) = (0, 1);
for _ in 1..n {
while n > 0 {
(a, b) = (b, a + b);
n -= 1; // do this to be const
}
b
a
}
/// ai code
pub fn fib(n: u64) -> u64 {
fn fib_pair(n: u64) -> (u64, u64) {
pub const fn fib(n: u32) -> u64 {
const fn fib_pair(n: u32) -> (u64, u64) {
if (n) == 0 {
(0, 1)
} else {
@@ -49,3 +50,15 @@ pub fn fib(n: u64) -> u64 {
fib_pair(n).0
}
#[test]
fn it_works() {
for _ in 0..100 {
let n = crate::rand_lda::randfib();
let a = fib(n);
let b = iterfib(n);
let c = fast2(n);
assert_eq!(a, b, "a is not equal b; n = {n}");
assert_eq!(c, b, "c is not equal b; n = {n}")
}
}
+34 -13
View File
@@ -1,20 +1,41 @@
mod fib;
mod rand_lda;
pub mod pyexport {
mod pyexport {
use pyo3::prelude::*;
#[pymodule]
mod lda_module {
use super::*;
#[pymodule]
mod fib {
use super::*;
/// this shit is the thing now in python
#[pyfunction]
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
fn fib(n: u64) -> u64 {
crate::fib::fast2(n)
// currently use u64 as a large number tupe, can use num_bigint
}
#[pyfunction]
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
const fn fib(n: u32) -> u64 {
crate::fib::fast2(n)
}
/// 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(())
#[pyfunction]
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
const fn iterfib(n: u32) -> u64 {
crate::fib::iterfib(n)
}
#[pyfunction]
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
const fn fibr(n: u32) -> u64 {
crate::fib::fib(n)
}
}
#[pymodule]
mod rand_lda {
use super::*;
#[pyfunction]
#[pyo3(text_signature = "() -> int")]
fn randfib() -> u32 {
crate::rand_lda::randfib()
}
}
}
}
+6
View File
@@ -0,0 +1,6 @@
//! rand specific impl
/// the number is 92. number is 0 to 92.
pub fn randfib() -> u32 {
rand::random_range(0..93)
}