kolakoski_algorithms

Crates.iokolakoski_algorithms
lib.rskolakoski_algorithms
version0.1.0
created_at2025-01-04 17:36:58.2539+00
updated_at2025-01-04 17:36:58.2539+00
descriptionEfficient algorithms for the Kolakoski sequence
homepage
repositoryhttps://github.com/colt-browning/kolakoski
max_upload_size
id1504109
size19,886
Andrei Z. (colt-browning)

documentation

README

This crate implements an iterator returning the tuples (kn, dn), where kn is the nth term of the Kolakoski sequence and dn = ∑i in 1..=n (−1)ki is the “Kolakoski discrepancy function”.

use kolakoski_algorithms::Kolakoski;

println!("{:?}", Kolakoski::default().take(20).map(|(k, _)| k).collect::<Vec<_>>());
// [1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1]
# assert_eq!(&[1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1][..], &Kolakoski::default().take(20).map(|(k, _)| k).collect::<Vec<_>>());

If you are interested in analysing the behaviour of the sequence for certain large values of the argument, you can avoid generating all preceding terms:

# use kolakoski_algorithms::Kolakoski;
println!("{:?}", Kolakoski::new(1_000_000_000).take(100).collect::<Vec<_>>());
Commit count: 1

cargo fmt