From 46907f8752036b5b28928deb874792d0743db938 Mon Sep 17 00:00:00 2001 From: hal8174 Date: Sun, 6 Apr 2025 20:35:08 +0200 Subject: [PATCH] Fix bugs (switch naive and cyclic) and add timing output for series calculation --- src/bin/series.rs | 3 ++- src/cyclic.rs | 8 +------- src/naive.rs | 8 +++++++- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/bin/series.rs b/src/bin/series.rs index da7434b..a277a15 100644 --- a/src/bin/series.rs +++ b/src/bin/series.rs @@ -2,7 +2,8 @@ fn main() { for length in 2.. { let mut count = 0; let mut state = vec![0, 1]; + let start = std::time::Instant::now(); stamp_folding::cyclic::stamp_folding(&mut state, 1, length - 2, &mut count, &mut |_, _| {}); - println!("{length:02}: {count} {}", count * 2); + println!("{length:02}: {count} {} {:?}", count * 2, start.elapsed()); } } diff --git a/src/cyclic.rs b/src/cyclic.rs index 19f76e6..350c198 100644 --- a/src/cyclic.rs +++ b/src/cyclic.rs @@ -9,7 +9,7 @@ pub fn stamp_folding( // dbg!(¤t, depth, last_inserted_index); if remaining == 0 { callback(*count, current); - *count = count.checked_add(1).unwrap(); + *count = count.checked_add(depth as u128).unwrap(); return; } @@ -37,12 +37,6 @@ pub fn stamp_folding( } } - if last.is_none() { - current.insert(0, depth); - stamp_folding(current, 0, remaining - 1, count, callback); - current.remove(0); - } - let mut last = None; for i in (last_inserted_index + 1)..current.len() { if let Some(l) = last { diff --git a/src/naive.rs b/src/naive.rs index 350c198..19f76e6 100644 --- a/src/naive.rs +++ b/src/naive.rs @@ -9,7 +9,7 @@ pub fn stamp_folding( // dbg!(¤t, depth, last_inserted_index); if remaining == 0 { callback(*count, current); - *count = count.checked_add(depth as u128).unwrap(); + *count = count.checked_add(1).unwrap(); return; } @@ -37,6 +37,12 @@ pub fn stamp_folding( } } + if last.is_none() { + current.insert(0, depth); + stamp_folding(current, 0, remaining - 1, count, callback); + current.remove(0); + } + let mut last = None; for i in (last_inserted_index + 1)..current.len() { if let Some(l) = last {