>> error fixed + test with a python impl

This commit is contained in:
lda committed 2025-06-10 06:16:42 +07:00
1 parent b7f596588d
commit 69a48bc711
5 files changed
+36 -5

No files matched your search

+1 -1
View File
@@ -4,7 +4,7 @@
# maturin generate-ci github
#
name: CI
# unexist rn
on:
push:
branches:
+20
View File
@@ -0,0 +1,20 @@
def fast2(n: int) -> int:
"port to python"
a, b = 0, 1
bit_len = n.bit_length()
mask = 0 if bit_len == 0 else 1 << (bit_len - 1)
while mask > 0:
c = a * (2 * b - a)
d = a * a + b * b
if n & mask == 0:
a, b = c, d
else:
a, b = d, c + d
mask >>= 1
return a
+11
View File
@@ -0,0 +1,11 @@
import timeit
import fib
import lda_module
import random
f = {"randint": random.randint}
g = "92"
# g = "randint(0, 92)"
print(g)
print(timeit.timeit(f"fast2({g})", globals=f | {"fast2": fib.fast2})) # 0.69
print(timeit.timeit(f"fib({g})", globals=f | {"fib": lda_module.fib})) # 0.31
+3 -4
View File
@@ -5,20 +5,19 @@
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;
let bit_len = u64::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);
let d = a * a + b * b;
if m & mask == 0 {
(a, b) = (c, d);
}
else {
} else {
(a, b) = (d, c + d);
}
mask >>= 1;
}
a
}
// 0, 1, 3, 7
// -> -1 /2 -1 /2 -1 /2
+1
View File
@@ -5,6 +5,7 @@ pub mod pyexport {
/// 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