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

File renamed without changes.
Generated
+62 -3
View File
@@ -59,6 +59,46 @@ dependencies = [
"autocfg",
]
[[package]]
name = "num-bigint"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
dependencies = [
"num-integer",
"num-traits",
"rand 0.8.5",
]
[[package]]
name = "num-integer"
version = "0.1.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
dependencies = [
"num-traits",
]
[[package]]
name = "num-iter"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.21.3"
@@ -98,6 +138,7 @@ dependencies = [
"indoc",
"libc",
"memoffset",
"num-bigint",
"once_cell",
"portable-atomic",
"pyo3-build-config",
@@ -166,6 +207,15 @@ version = "5.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"rand_core 0.6.4",
]
[[package]]
name = "rand"
version = "0.9.1"
@@ -173,7 +223,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97"
dependencies = [
"rand_chacha",
"rand_core",
"rand_core 0.9.3",
]
[[package]]
@@ -183,9 +233,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core",
"rand_core 0.9.3",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
[[package]]
name = "rand_core"
version = "0.9.3"
@@ -199,8 +255,11 @@ dependencies = [
name = "rpy"
version = "0.1.0"
dependencies = [
"num-bigint",
"num-iter",
"num-traits",
"pyo3",
"rand",
"rand 0.9.1",
]
[[package]]
+4 -1
View File
@@ -9,5 +9,8 @@ name = "lda_module"
crate-type = ["cdylib"]
[dependencies]
pyo3 = "0.25.0"
num-bigint = { version = "0.4.6", features = ["rand"] }
num-iter = "0.1.45"
num-traits = "0.2.19"
pyo3 = { version = "0.25.0", features = ["num-bigint"] }
rand = "0.9.1"
+29 -6
View File
@@ -1,4 +1,5 @@
import timeit
import math
# import timeit
import fib
import lda_module
@@ -17,8 +18,30 @@ print(n)
# its so good
# powered by --release
print(timeit.timeit(f"fib({g})", number=n, globals=f | {"fib": fib.fast2}))
print(timeit.timeit(f"fib({g})", number=n, globals=f | {"fib": lda_module.fib.fib}))
print("other fibs")
print(timeit.timeit(f"fib({g})", number=n, globals=f | {"fib": lda_module.fib.fibr}))
print(timeit.timeit(f"fib({g})", number=n, globals=f | {"fib": lda_module.fib.iterfib}))
# print(timeit.timeit(f"fib({g})", number=n, globals=f | {"fib": fib.fast2}))
# print(timeit.timeit(f"fib({g})", number=n, globals=f | {"fib": lda_module.fib.fib}))
# print("other fibs")
# print(timeit.timeit(f"fib({g})", number=n, globals=f | {"fib": lda_module.fib.fibr}))
# print(timeit.timeit(f"fib({g})", number=n, globals=f | {"fib": lda_module.fib.iterfib}))
input_number = 8_000_000
import time
start = time.perf_counter_ns()
r = fib.fast2(input_number)
end = time.perf_counter_ns()
print(f"did {input_number} in {(end - start) / 1e3} us") # 956078.1
print("number has", math.ceil(math.log10(r)), "digits")
start = time.perf_counter_ns()
r = lda_module.fib_big.fast2(input_number) # 10 million times slower # 3332277.8
end = time.perf_counter_ns()
print(f"did {input_number} in {(end - start) / 1e3} us")
print("number has", math.ceil(math.log10(r)), "digits")
start = time.perf_counter_ns()
r = lda_module.fib_big.f_rec(input_number) # 10 million times slower # 3332277.8
end = time.perf_counter_ns()
print(f"did {input_number} in {(end - start) / 1e3} us")
print("number has", math.ceil(math.log10(r)), "digits")
+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)
}
}
}
}