This commit is contained in:
lda committed 2025-06-29 17:15:46 +07:00
1 parent 462b63109b
commit 18f5766c67
7 files changed
+261 -22

No files matched your search

+119
View File
@@ -0,0 +1,119 @@
use num_bigint::BigUint;
use num_traits::One;
/// num bigint making it awkward
pub fn fast2_big(n: BigUint) -> BigUint {
let (mut a, mut b) = (BigUint::ZERO, BigUint::one());
let m = n;
let mut bit_len = m.bits();
loop {
let c = &a * (&b * 2u8 - &a);
let d = &a * &a + &b * &b;
if !m.bit(bit_len) {
(a, b) = (c, d);
} else {
b = c + &d;
a = d;
}
if bit_len == 0 {
break;
} else {
bit_len -= 1;
}
}
a
}
/// DO NOT USE for it is slow af
pub fn iter_bigfib(n: BigUint) -> BigUint {
let (mut a, mut b) = (BigUint::ZERO, BigUint::one());
for _ in num_iter::range(BigUint::ZERO, n) {
(b, a) = (a + &b, b);
}
a
}
// again not having copy + new upon ops makes this so ASSSS
pub fn fib_rec(n: BigUint) -> BigUint {
fn fib_pair(n: BigUint) -> (BigUint, BigUint) {
if BigUint::ZERO == n {
(BigUint::ZERO, BigUint::one())
} else {
let (a, b) = fib_pair(&n / 2u8);
let c = &a * (&b * 2u8 - &a);
let d = &a * &a + &b * &b;
if n % 2u8 == BigUint::ZERO {
(c, d)
} else {
let temp = c + &d;
(d, temp)
}
}
}
fib_pair(n).0
}
#[cfg(test)]
mod tests {
use crate::{fib, rand_lda::randfib};
use super::*;
#[test]
fn i_understand_the_lib() {
let number: u64 = rand::random();
let big = BigUint::from(number);
let Some(mut real) = (u64::BITS - number.trailing_zeros()).checked_sub(1) else {
// case number is 0 i dont want to check
return i_understand_the_lib(); // huh
};
while real > 0 {
let r = number & (1 << real);
if r != 0 {
assert!(big.bit(real as _));
} else {
assert!(!big.bit(real as _))
}
real -= 1;
}
}
#[test]
fn should_be_the_same_fast() {
let n0_92: u8 = randfib() as _;
assert_eq!(
BigUint::from(fib::fast2(n0_92 as _)),
fast2_big(n0_92.into()),
"shit. number is {n0_92}"
)
}
#[test]
fn should_be_the_same_iter() {
let n0_92: u8 = randfib() as _;
assert_eq!(
BigUint::from(fib::iterfib(n0_92 as _)),
iter_bigfib(n0_92.into()),
"shit. number is {n0_92}"
)
}
#[test]
fn should_be_the_same_rec() {
let n0_92: u8 = randfib() as _;
assert_eq!(
BigUint::from(fib::fib(n0_92 as _)),
fib_rec(n0_92.into()),
"shit. number is {n0_92}"
)
}
#[test]
fn it_works() {
// dropping test of iter here because it is so slow
let number: u64 = rand::random::<u64>() % 8_000_000;
println!("{number}");
let a = fib_rec(number.into());
let b = fast2_big(number.into());
assert_eq!(a, b, "a does not equal b; n = {number}");
}
}
+18 -9
View File
@@ -51,14 +51,23 @@ pub const fn fib(n: u32) -> 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}")
#[cfg(test)]
mod tests {
use super::fast2;
use super::iterfib;
use super::fib;
#[test]
pub(crate) 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}")
}
}
}
+29 -3
View File
@@ -1,5 +1,6 @@
mod fib;
mod rand_lda;
mod big;
mod pyexport {
use pyo3::prelude::*;
@@ -9,13 +10,13 @@ mod pyexport {
#[pymodule]
mod fib {
use super::*;
#[pyfunction]
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
const fn fib(n: u32) -> u64 {
crate::fib::fast2(n)
}
#[pyfunction]
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
const fn iterfib(n: u32) -> u64 {
@@ -30,12 +31,37 @@ mod pyexport {
#[pymodule]
mod rand_lda {
use super::*;
#[pyfunction]
#[pyo3(text_signature = "() -> int")]
fn randfib() -> u32 {
crate::rand_lda::randfib()
}
}
#[pymodule]
mod fib_big {
use super::*;
use num_bigint::BigUint;
///sybau
#[pyfunction]
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
fn fast2(n: BigUint) -> BigUint {
crate::big::fast2_big(n)
}
///iter. DO NOT USE
#[pyfunction]
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
fn iter(n: BigUint) -> BigUint {
crate::big::iter_bigfib(n)
}
///recursion.
#[pyfunction]
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
fn f_rec(n: BigUint) -> BigUint {
crate::big::fib_rec(n)
}
}
}
}