How to resolve the algorithm Fairshare between two and more step by step in the Nim programming language
How to resolve the algorithm Fairshare between two and more step by step in the Nim programming language
Table of Contents
Problem Statement
The Thue-Morse sequence is a sequence of ones and zeros that if two people take turns in the given order, the first persons turn for every '0' in the sequence, the second for every '1'; then this is shown to give a fairer, more equitable sharing of resources. (Football penalty shoot-outs for example, might not favour the team that goes first as much if the penalty takers take turns according to the Thue-Morse sequence and took 2^n penalties) The Thue-Morse sequence of ones-and-zeroes can be generated by:
Use this method:
Counting from zero; using a function/method/routine to express an integer count in base b, sum the digits modulo b to produce the next member of the Thue-Morse fairshare series for b people.
Show the first 25 terms of the fairshare sequence:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fairshare between two and more step by step in the Nim programming language
Source code in the nim programming language
import math, strutils
#---------------------------------------------------------------------------------------------------
iterator countInBase(b: Positive): seq[Natural] =
## Yield the successive integers in base "b" as a sequence of digits.
var value = @[Natural 0]
yield value
while true:
# Add one to current value.
var c = 1
for i in countdown(value.high, 0):
let val = value[i] + c
if val < b:
value[i] = val
c = 0
else:
value[i] = val - b
c = 1
if c == 1:
# Add a new digit at the beginning.
# In this case, for better performances, we could have add it at the end.
value.insert(c, 0)
yield value
#---------------------------------------------------------------------------------------------------
func thueMorse(b: Positive; count: Natural): seq[Natural] =
## Return the "count" first elements of Thue-Morse sequence for base "b".
var count = count
for n in countInBase(b):
result.add(sum(n) mod b)
dec count
if count == 0: break
#———————————————————————————————————————————————————————————————————————————————————————————————————
for base in [2, 3, 5, 11]:
echo "Base ", ($base & ": ").alignLeft(4), thueMorse(base, 25).join(" ")
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the zkl programming language
You may also check:How to resolve the algorithm Pythagoras tree step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Stern-Brocot sequence step by step in the Phix programming language
You may also check:How to resolve the algorithm 100 doors step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Spiral matrix step by step in the MATLAB programming language