>> error fixed + test with a python impl
This commit is contained in:
1 parent
b7f596588d
commit
69a48bc711
5 files changed
+36
-5
No files matched your search
+3
-4
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user