How to resolve the algorithm Fibonacci word step by step in the Rust programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Fibonacci word step by step in the Rust programming language
Table of Contents
Problem Statement
The Fibonacci Word may be created in a manner analogous to the Fibonacci Sequence as described here:
Perform the above steps for n = 37. You may display the first few but not the larger values of n. {Doing so will get the task's author into trouble with them what be (again!).} Instead, create a table for F_Words 1 to 37 which shows:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fibonacci word step by step in the Rust programming language
Source code in the rust programming language
struct Fib<T> {
curr: T,
next: T,
}
impl<T> Fib<T> {
fn new(curr: T, next: T) -> Self {
Fib { curr: curr, next: next, }
}
}
impl Iterator for Fib<String> {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
let ret = self.curr.clone();
self.curr = self.next.clone();
self.next = format!("{}{}", ret, self.next);
Some(ret)
}
}
fn get_entropy(s: &[u8]) -> f64 {
let mut entropy = 0.0;
let mut histogram = [0.0; 256];
for i in 0..s.len() {
histogram.get_mut(s[i] as usize).map(|v| *v += 1.0);
}
for i in 0..256 {
if histogram[i] > 0.0 {
let ratio = histogram[i] / s.len() as f64;
entropy -= ratio * ratio.log2();
}
}
entropy
}
fn main() {
let f = Fib::new("1".to_string(), "0".to_string());
println!("{:10} {:10} {:10} {:60}", "N", "Length", "Entropy", "Word");
for (i, s) in f.take(37).enumerate() {
let word = if s.len() > 60 {"Too long"} else {&*s};
println!("{:10} {:10} {:.10} {:60}", i + 1, s.len(), get_entropy(&s.bytes().collect::<Vec<_>>()), word);
}
}
You may also check:How to resolve the algorithm Find the intersection of two lines step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Circular primes step by step in the Forth programming language
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the Ring programming language
You may also check:How to resolve the algorithm Determine if a string has all unique characters step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the Forth programming language