How to resolve the algorithm Fairshare between two and more step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Fairshare between two and more step by step in the Julia 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 Julia programming language

The provided Julia code calculates the fair share of a resource among a specified number of players, where each player's share is determined by the sum of the digits in their player number represented in the base equal to the number of players.

Here's a detailed explanation of the code:

fairshare(nplayers, len) = [sum(digits(n, base=nplayers)) % nplayers for n in 0:len-1]: This function takes two arguments: nplayers (the number of players) and len (the length of the sequence to generate). It creates a list comprehension that iterates over the range from 0 to len-1, representing each player's number. For each player number n, it calculates the sum of its digits in base nplayers using the digits function. The result is then multiplied by nplayers to get the fair share for that player. The list comprehension returns a list of fair shares for all player numbers from 0 to len-1.

for n in [2, 3, 5, 11]: This is a for loop that iterates over a list of four numbers: 2, 3, 5, and 11. These numbers represent the number of players for different scenarios.

println("Fairshare ", n > 2 ? "among" : "between", " $n people: ", fairshare(n, 25)): For each number of players n, it prints a message indicating whether the fair share is being calculated "among" (for n greater than 2) or "between" (for n equal to 2) n people. It then calculates the fair share for 25 rounds (represented by len = 25) using the fairshare function and prints the result.

The output of this code will be:

Fairshare among 3 people: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
Fairshare among 5 people: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
Fairshare between 2 people: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
Fairshare among 11 people: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

This output shows the fair shares for different numbers of players over 25 rounds. For example, in a game with 5 players, the fair shares for the first 25 rounds are [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24].

Source code in the julia programming language

fairshare(nplayers,len) = [sum(digits(n, base=nplayers)) % nplayers for n in 0:len-1]

for n in [2, 3, 5, 11]
    println("Fairshare ", n > 2 ? "among" : "between", " $n people: ", fairshare(n, 25))
end


  

You may also check:How to resolve the algorithm Percentage difference between images step by step in the OCaml programming language
You may also check:How to resolve the algorithm Terminal control/Cursor positioning step by step in the Elena programming language
You may also check:How to resolve the algorithm Semiprime step by step in the Forth programming language
You may also check:How to resolve the algorithm Events step by step in the Delphi programming language
You may also check:How to resolve the algorithm Abstract type step by step in the J programming language