The   Fibonacci Word   may be created in a manner analogous to the   Fibonacci Sequence   as described here:

    Define   F_Word1   as   1
    Define   F_Word2   as   0
    Form     F_Word3   as   F_Word2     concatenated with   F_Word1    i.e.:   01
    Form     F_Wordn   as   F_Wordn-1   concatenated with   F_wordn-2



Task

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:

   The number of characters in the word
   The word's Entropy


Related tasks
 
   Fibonacci word/fractal
   Entropy
   Entropy/Narcissist


import Foundation
 
struct Fib: Sequence, IteratorProtocol {
  private var cur: String
  private var nex: String
 
  init(cur: String, nex: String) {
    self.cur = cur
    self.nex = nex
  }
 
  mutating func next() -> String? {
    let ret = cur
 
    cur = nex
    nex = "\(ret)\(nex)"
 
    return ret
  }
}
 
func getEntropy(_ s: [Int]) -> Double {
  var entropy = 0.0
  var hist = Array(repeating: 0.0, count: 256)
 
  for i in 0..<s.count {
    hist[s[i]] += 1
  }
 
  for i in 0..<256 where hist[i] > 0 {
    let rat = hist[i] / Double(s.count)
    entropy -= rat * log2(rat)
  }
 
  return entropy
}
 
for (i, str) in Fib(cur: "1", nex: "0").prefix(37).enumerated() {
  let ent = getEntropy(str.map({ Int($0.asciiValue!) }))
 
  print("i: \(i) len: \(str.count) entropy: \(ent)")
}