How to resolve the algorithm Fairshare between two and more step by step in the D programming language
How to resolve the algorithm Fairshare between two and more step by step in the D 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 D programming language
Source code in the d programming language
import std.array;
import std.stdio;
int turn(int base, int n) {
int sum = 0;
while (n != 0) {
int re = n % base;
n /= base;
sum += re;
}
return sum % base;
}
void fairShare(int base, int count) {
writef("Base %2d:", base);
foreach (i; 0..count) {
auto t = turn(base, i);
writef(" %2d", t);
}
writeln;
}
void turnCount(int base, int count) {
auto cnt = uninitializedArray!(int[])(base);
cnt[] = 0;
foreach (i; 0..count) {
auto t = turn(base, i);
cnt[t]++;
}
auto minTurn = int.max;
auto maxTurn = int.min;
int portion = 0;
foreach (num; cnt) {
if (num > 0) {
portion++;
}
if (num < minTurn) {
minTurn = num;
}
if (maxTurn < num) {
maxTurn = num;
}
}
writef(" With %d people: ", base);
if (minTurn == 0) {
writefln("Only %d have a turn", portion);
} else if (minTurn == maxTurn) {
writeln(minTurn);
} else {
writeln(minTurn," or ", maxTurn);
}
}
void main() {
fairShare(2, 25);
fairShare(3, 25);
fairShare(5, 25);
fairShare(11, 25);
writeln("How many times does each get a turn in 50000 iterations?");
turnCount(191, 50000);
turnCount(1377, 50000);
turnCount(49999, 50000);
turnCount(50000, 50000);
turnCount(50001, 50000);
}
You may also check:How to resolve the algorithm List comprehensions step by step in the Nim programming language
You may also check:How to resolve the algorithm Grayscale image step by step in the R programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the D programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Polyspiral step by step in the C# programming language