Fairshare between two and more: Difference between revisions

Add C# implementation
(Added Arturo implementation)
(Add C# implementation)
 
(18 intermediate revisions by 12 users not shown)
Line 41:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F _basechange_int(=num, b)
Return list of ints representing positive num in base b
Line 62:
 
L(b) (2, 3, 5, 11)
print(‘#2’.format(b)‘: ’String(fairshare(b, 25))[1 .< (len)-1])</langsyntaxhighlight>
 
{{out}}
Line 71:
11: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 2, 3, 4
</pre>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
BEGIN # Use the generalised Thue-Morse sequence to generate Fairshare #
# sequences for various numbers of people #
# returns the digit sum of n in the specified base #
PROC digit sum = ( INT n, base )INT:
IF n = 0 THEN 0
ELSE
INT result := 0;
INT v := ABS n;
WHILE v > 0 DO
result +:= v MOD base;
v OVERAB base
OD;
result
FI # digit sum # ;
# show the first n terms of the fairshare sequence in the specified base #
PROC show fairshare = ( INT n, base )VOID:
BEGIN
print( ( whole( base, -2 ), ":" ) );
FOR i FROM 0 TO n - 1 DO
print( ( " ", whole( digit sum( i, base ) MOD base, -2 ) ) )
OD;
print( ( newline ) )
END # show fairshare # ;
show fairshare( 25, 2 );
show fairshare( 25, 3 );
show fairshare( 25, 5 );
show fairshare( 25, 11 )
END
</syntaxhighlight>
{{out}}
<pre>
2: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
3: 0 1 2 1 2 0 2 0 1 1 2 0 2 0 1 0 1 2 2 0 1 0 1 2 1
5: 0 1 2 3 4 1 2 3 4 0 2 3 4 0 1 3 4 0 1 2 4 0 1 2 3
11: 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 2 3 4
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">fairshare ← ⊣|(+/⊥⍣¯1)
2 3 5 11 ∘.fairshare 0,⍳24</syntaxhighlight>
{{out}}
<pre>0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
0 1 2 1 2 0 2 0 1 1 2 0 2 0 1 0 1 2 2 0 1 0 1 2 1
0 1 2 3 4 1 2 3 4 0 2 3 4 0 1 3 4 0 1 2 4 0 1 2 3
0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 2 3 4</pre>
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">-- thueMorse :: Int -> [Int]
on thueMorse(base)
-- Non-finite sequence of Thue-Morse terms for a given base.
Line 348 ⟶ 398:
set my text item delimiters to dlm
s
end unlines</langsyntaxhighlight>
{{Out}}
<pre>First 25 fairshare terms for N players:
Line 358 ⟶ 408:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">thueMorse: function [base, howmany][
i: 0
result: new []
Line 373 ⟶ 423:
(pad.right "Base "++(to :string b) 7)++" =>"
join.with:" " map to [:string] thueMorse b 25 'x -> pad x 2
]</langsyntaxhighlight>
 
{{out}}
Line 381 ⟶ 431:
Base 5 => 0 1 2 3 4 1 2 3 4 0 2 3 4 0 1 3 4 0 1 2 4 0 1 2 3
Base 11 => 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 2 3 4</pre>
 
=={{header|BASIC}}==
<syntaxhighlight lang="gwbasic">10 DEFINT A-Z
20 DIM B(4): B(1)=2: B(2)=3: B(3)=5: B(4)=11
30 FOR I=1 TO 4
40 B=B(I)
50 PRINT USING "###:";I;
60 FOR N=0 TO 24: GOSUB 100: PRINT USING "###";S;: NEXT N
70 PRINT
80 NEXT I
90 END
100 REM
101 REM S = N'th item of fairshare sequence for B people
102 REM
110 S=0: Z=N
120 S=S+Z MOD B: Z=Z\B: IF Z>0 THEN 120
130 S=S MOD B
140 RETURN</syntaxhighlight>
{{out}}
<pre> 1: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
2: 0 1 2 1 2 0 2 0 1 1 2 0 2 0 1 0 1 2 2 0 1 0 1 2 1
3: 0 1 2 3 4 1 2 3 4 0 2 3 4 0 1 3 4 0 1 2 4 0 1 2 3
4: 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 2 3 4</pre>
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
 
let digitsum(n,b) =
n < b -> n,
n rem b + digitsum(n/b, b)
 
let fairshare(n,b) =
digitsum(n,b) rem b
 
let start() be
$( let bs = table 2, 3, 5, 11
for bi = 0 to 3
$( writef("%I2:", bs!bi)
for n = 0 to 24 do writef("%I2 ", fairshare(n, bs!bi))
wrch('*N')
$)
$)</syntaxhighlight>
{{out}}
<pre> 2: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
3: 0 1 2 1 2 0 2 0 1 1 2 0 2 0 1 0 1 2 2 0 1 0 1 2 1
5: 0 1 2 3 4 1 2 3 4 0 2 3 4 0 1 3 4 0 1 2 4 0 1 2 3
11: 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 2 3 4</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 462 ⟶ 559:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Base 2: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
Line 474 ⟶ 571:
With 50000 people: 1
With 50001 people: Only 50000 have a turn</pre>
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
 
class FairshareBetweenTwoAndMore
{
static void Main(string[] args)
{
foreach (int baseValue in new List<int> { 2, 3, 5, 11 })
{
Console.WriteLine($"Base {baseValue} = {string.Join(", ", ThueMorseSequence(25, baseValue))}");
}
}
 
private static List<int> ThueMorseSequence(int terms, int baseValue)
{
List<int> sequence = new List<int>();
for (int i = 0; i < terms; i++)
{
int sum = 0;
int n = i;
while (n > 0)
{
// Compute the digit sum
sum += n % baseValue;
n /= baseValue;
}
// Compute the digit sum modulo baseValue.
sequence.Add(sum % baseValue);
}
return sequence;
}
}
</syntaxhighlight>
{{out}}
<pre>
Base 2 = 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0
Base 3 = 0, 1, 2, 1, 2, 0, 2, 0, 1, 1, 2, 0, 2, 0, 1, 0, 1, 2, 2, 0, 1, 0, 1, 2, 1
Base 5 = 0, 1, 2, 3, 4, 1, 2, 3, 4, 0, 2, 3, 4, 0, 1, 3, 4, 0, 1, 2, 4, 0, 1, 2, 3
Base 11 = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 2, 3, 4
 
</pre>
 
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
Line 546 ⟶ 688:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Base 2: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
Line 558 ⟶ 700:
With 50000 people: 1
With 50001 people: Only 50000 have a turn</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub fairshare(index: uint16, base: uint16): (result: uint16) is
result := 0;
while index > 0 loop
result := result + index % base;
index := index / base;
end loop;
result := result % base;
end sub;
 
sub printSequence(amount: uint16, base: uint16) is
print_i16(base);
print(": ");
var index: uint16 := 0;
while index < amount loop
print_i16(fairshare(index, base));
print(" ");
index := index + 1;
end loop;
print_nl();
end sub;
 
printSequence(25, 2);
printSequence(25, 3);
printSequence(25, 5);
printSequence(25, 11);</syntaxhighlight>
{{out}}
<pre>2: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
3: 0 1 2 1 2 0 2 0 1 1 2 0 2 0 1 0 1 2 2 0 1 0 1 2 1
5: 0 1 2 3 4 1 2 3 4 0 2 3 4 0 1 3 4 0 1 2 4 0 1 2 3
11: 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 2 3 4</pre>
 
=={{header|EasyLang}}==
{{trans|Cowgol}}
<syntaxhighlight lang="easylang">
func fairshare ind base .
while ind > 0
r += ind mod base
ind = ind div base
.
r = r mod base
return r
.
proc sequence n base . .
write base & ": "
for ind range0 n
write (fairshare ind base) & " "
.
print ""
.
sequence 25 2
sequence 25 3
sequence 25 5
sequence 25 11
</syntaxhighlight>
 
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="d">import std.array;
import std.stdio;
 
Line 629 ⟶ 829:
turnCount(50000, 50000);
turnCount(50001, 50000);
}</langsyntaxhighlight>
{{out}}
<pre>Base 2: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
Line 641 ⟶ 841:
With 50000 people: 1
With 50001 people: Only 50000 have a turn</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
procedure DoFairshare(Memo: TMemo; Base: integer);
{Display 25 fairshare sequence items}
var I, N, Sum: integer;
var S: string;
begin
S:=Format('Base - %2d: ',[Base]);
for I:= 0 to 25-1 do
begin
N:= I; Sum:= 0;
while N>0 do
begin
Sum:= Sum + (N mod Base);
N:= N div Base;
end;
S:=S+' '+IntToStr(Sum mod Base);
end;
Memo.Lines.Add(S);
end;
 
 
procedure ShowFairshare(Memo: TMemo);
begin
DoFairshare(Memo,2);
DoFairshare(Memo,3);
DoFairshare(Memo,5);
DoFairshare(Memo,11);
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Base - 2: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
Base - 3: 0 1 2 1 2 0 2 0 1 1 2 0 2 0 1 0 1 2 2 0 1 0 1 2 1
Base - 5: 0 1 2 3 4 1 2 3 4 0 2 3 4 0 1 3 4 0 1 2 4 0 1 2 3
Base - 11: 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 2 3 4
 
Elapsed Time: 4.753 ms.
 
</pre>
 
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc fairshare(word n, base) word:
word result;
result := 0;
while n>0 do
result := result + n % base;
n := n / base
od;
result % base
corp
 
proc main() void:
[4]word bases = (2,3,5,11);
word b, n;
for b from 0 upto 3 do
write(bases[b]:2, ':');
for n from 0 upto 24 do
write(fairshare(n, bases[b]):3)
od;
writeln()
od
corp</syntaxhighlight>
{{out}}
<pre> 2: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
3: 0 1 2 1 2 0 2 0 1 1 2 0 2 0 1 0 1 2 2 0 1 0 1 2 1
5: 0 1 2 3 4 1 2 3 4 0 2 3 4 0 1 3 4 0 1 2 4 0 1 2 3
11: 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 2 3 4</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<langsyntaxhighlight lang="factor">USING: formatting kernel math math.parser sequences ;
 
: nth-fairshare ( n base -- m )
Line 653 ⟶ 931:
 
{ 2 3 5 11 }
[ 25 over <fairshare> "%2d -> %u\n" printf ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 661 ⟶ 939:
11 -> { 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 2 3 4 }
</pre>
 
 
=={{header|FreeBASIC}}==
{{trans|Visual Basic .NET}}
<langsyntaxhighlight lang="freebasic">
Function Turn(mibase As Integer, n As Integer) As Integer
Dim As Integer sum = 0
Line 728 ⟶ 1,005:
TurnCount(50001, 50000)
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 736 ⟶ 1,013:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 794 ⟶ 1,071:
fmt.Printf(" With %d people: %s\n", base, t)
}
}</langsyntaxhighlight>
 
{{out}}
Line 812 ⟶ 1,089:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Bool (bool)
import Data.List (intercalate, unfoldr)
import Data.Tuple (swap)
Line 871 ⟶ 1,148:
 
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</langsyntaxhighlight>
{{Out}}
<pre>First 25 fairshare terms for a given number of players:
Line 905 ⟶ 1,182:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.Arrays;
Line 935 ⟶ 1,212:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 945 ⟶ 1,222:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,135 ⟶ 1,412:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>First 25 fairshare terms for a given number of players:
Line 1,145 ⟶ 1,422:
=={{header|jq}}==
jq has a built-in for limiting a generator, but does not have a built-in for generating the integers in an arbitrary base, so we begin by defining `integers($base)` for generating integers as arrays beginning with the least-significant digit.
<langsyntaxhighlight lang="jq"># Using a "reverse array" representations of the integers base b (b>=2),
# generate an unbounded stream of the integers from [0] onwards.
# E.g. for binary: [0], [1], [0,1], [1,1] ...
Line 1,171 ⟶ 1,448:
(2,3,5,11)
| "Fairshare \((select(.>2)|"among") // "between") \(.) people: \([fairshare(.; 25)])"
</syntaxhighlight>
</lang>
{{out}}
As for Julia.
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">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
</langsyntaxhighlight>{{out}}
<pre>
Fairshare between 2 people: [0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0]
Line 1,191 ⟶ 1,468:
=={{header|Kotlin}}==
{{trans|Visual Basic .NET}}
<langsyntaxhighlight lang="scala">fun turn(base: Int, n: Int): Int {
var sum = 0
var n2 = n
Line 1,261 ⟶ 1,538:
turnCount(50001, 50000)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>Base 2: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
Line 1,276 ⟶ 1,553:
=={{header|Lua}}==
{{trans|D}}
<langsyntaxhighlight lang="lua">function turn(base, n)
local sum = 0
while n ~= 0 do
Line 1,350 ⟶ 1,627:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>Base 2: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
Line 1,364 ⟶ 1,641:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Fairshare]
Fairshare[n_List, b_Integer : 2] := Fairshare[#, b] & /@ n
Fairshare[n_Integer, b_Integer : 2] := Mod[Total[IntegerDigits[n, b]], b]
Line 1,370 ⟶ 1,647:
Fairshare[Range[0, 24], 3]
Fairshare[Range[0, 24], 5]
Fairshare[Range[0, 24], 11]</langsyntaxhighlight>
{{out}}
<pre>{0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0}
Line 1,378 ⟶ 1,655:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, strutils
 
#---------------------------------------------------------------------------------------------------
Line 1,422 ⟶ 1,699:
 
for base in [2, 3, 5, 11]:
echo "Base ", ($base & ": ").alignLeft(4), thueMorse(base, 25).join(" ")</langsyntaxhighlight>
 
{{out}}
Line 1,429 ⟶ 1,706:
Base 5: 0 1 2 3 4 1 2 3 4 0 2 3 4 0 1 3 4 0 1 2 4 0 1 2 3
Base 11: 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 2 3 4</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">program Fairshare;
{$IFDEF FPC}{$MODE Delphi}{$Optimization ON,ALL}{$ENDIF}
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils;
const
maxDigitCnt = 32;
type
tdigit = Uint32;
tDgtSum = record
dgts : array[0..maxDigitCnt-1] of tdigit;
dgtNum : Uint64;
dgtsum : Uint64;//maxValue maxDigitCnt*(dgtBase-1)
dgtBase,
dgtThue : tDigit;
end;
procedure OutDgtSum(const ds:tDgtSum);
var
i : NativeInt;
Begin
with ds do
Begin
writeln(' base ',dgtBase,' sum of digits : ',dgtSum,' dec number ',dgtNum);
i := Low(dgts);
repeat
write(dgts[i],'|');
inc(i);
until i > High(dgts);
writeln;
end;
end;
 
function IncDgtSum(var ds:tDgtSum):boolean;
//add 1 to dgts and corrects sum of Digits
//return false if overflow happens
var
i,base_1 : NativeInt;
Begin
with ds do
begin
i := High(dgts);
base_1 := dgtBase-1;
inc(dgtNum);
repeat
IF dgts[i] < base_1 then
//add one and done
Begin
inc(dgts[i]);
inc(dgtSum);
BREAK;
end
else
Begin
dgts[i] := 0;
dec(dgtSum,base_1);
end;
dec(i);
until i < Low(dgts);
dgtThue := dgtSum MOD (base_1+1);
result := i < Low(dgts)
end;
end;
 
procedure CheckBase_N_Turns( base:tDigit;turns:NativeUInt);
var
actualNo :tDgtSum;
slots : array of Uint32;
pSlots : pUint32;
i : NativeUInt;
Begin
fillchar(actualNo,SizeOf(actualNo),#0);
setlength(slots,base);
pSlots := @slots[0];
actualNo.dgtBase := Base;
Write(base:3,' [');
while turns>0 do
Begin
inc(pSlots[actualNo.dgtThue],turns);
IncDgtSum(actualNo);
dec(turns);
end;
For i := 0 to Base-1 do
write(pSlots[i],' ');
writeln(']');
end;
 
procedure SumBase_N_Turns( base:tDigit;turns:NativeUInt);
var
actualNo :tDgtSum;
Begin
fillchar(actualNo,SizeOf(actualNo),#0);
actualNo.dgtBase := Base;
Write(base:3,' [');
while turns>1 do
Begin
write(actualNo.dgtThue,',');
IncDgtSum(actualNo);
dec(turns);
end;
writeln(actualNo.dgtThue,']');
end;
 
var
turns : NativeInt;
Begin
turns := 25;
SumBase_N_Turns(2,turns); SumBase_N_Turns(3,turns);
SumBase_N_Turns(5,turns); SumBase_N_Turns(11,turns);
Writeln;
writeln('Summing up descending numbers from turns downto 0');;
turns := 2*3*5*11;
Writeln(turns,' turns = 2*3*5*11');
CheckBase_N_Turns(2,turns); CheckBase_N_Turns(3,turns);
CheckBase_N_Turns(5,turns); CheckBase_N_Turns(11,turns);
 
turns := sqr(2)*sqr(3)*sqr(5)*sqr(11);
Writeln(turns,' turns = sqr(2)*sqr(3)*sqr(5)*sqr(11)');
CheckBase_N_Turns(2,turns); CheckBase_N_Turns(3,turns);
CheckBase_N_Turns(5,turns); CheckBase_N_Turns(11,turns);
end.
</syntaxhighlight>
{{out}}
<pre>
2 [0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0]
3 [0,1,2,1,2,0,2,0,1,1,2,0,2,0,1,0,1,2,2,0,1,0,1,2,1]
5 [0,1,2,3,4,1,2,3,4,0,2,3,4,0,1,3,4,0,1,2,4,0,1,2,3]
11 [0,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,0,2,3,4]
 
Summing up descending numbers from turns downto 0
330 turns = 2*3*5*11
2 [27307 27308 ]
3 [18206 18204 18205 ]
5 [10925 10924 10923 10922 10921 ]
11 [4961 4953 4956 4959 4962 4965 4968 4971 4974 4977 4969 ]
108900 turns = sqr(2)*sqr(3)*sqr(5)*sqr(11)
2 [2964829725 2964829725]
3 [1976553150 1976553150 1976553150]
5 [1185931890 1185931890 1185931890 1185931890 1185931890]
11 [539059950 539059950 539059950 539059950 539059950 539059950 539059950 539059950 539059950 539059950 539059950]
 
</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use Math::AnyNum qw(sum polymod);
Line 1,443 ⟶ 1,865:
for (<2 3 5 11>) {
printf "%3s:%s\n", $_, fairshare($_, 25);
}</langsyntaxhighlight>
{{out}}
<pre> 2: 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0
Line 1,452 ⟶ 1,874:
=={{header|Phix}}==
{{trans|Go}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">fairshare</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
Line 1,471 ⟶ 1,893:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d : %v\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">fairshare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">25</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,479 ⟶ 1,901:
11 : {0,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,0,2,3,4}
</pre>
 
=={{header|PL/M}}==
<syntaxhighlight lang="plm">100H:
 
BDOS: PROCEDURE (F,A); DECLARE F BYTE, A ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; GO TO 0; END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
 
PRINT$NUMBER: PROCEDURE (N);
DECLARE S (7) BYTE INITIAL ('..... $');
DECLARE (N, P) ADDRESS, C BASED P BYTE;
P = .S(5);
DIGIT:
P = P - 1;
C = N MOD 10 + '0';
N = N / 10;
IF N > 0 THEN GO TO DIGIT;
CALL PRINT(P);
END PRINT$NUMBER;
 
FAIR$SHARE: PROCEDURE (N, BASE) ADDRESS;
DECLARE (N, BASE, SUM) ADDRESS;
SUM = 0;
DO WHILE N>0;
SUM = SUM + N MOD BASE;
N = N / BASE;
END;
RETURN SUM MOD BASE;
END FAIR$SHARE;
 
DECLARE BASES (4) BYTE INITIAL (2, 3, 5, 11);
DECLARE (I, N) BYTE;
 
DO I=0 TO LAST(BASES);
CALL PRINT$NUMBER(BASES(I));
CALL PRINT(.': $');
DO N=0 TO 24;
CALL PRINT$NUMBER(FAIR$SHARE(N, BASES(I)));
END;
CALL PRINT(.(13,10,'$'));
END;
CALL EXIT;
EOF</syntaxhighlight>
{{out}}
<pre>2 : 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
3 : 0 1 2 1 2 0 2 0 1 1 2 0 2 0 1 0 1 2 2 0 1 0 1 2 1
5 : 0 1 2 3 4 1 2 3 4 0 2 3 4 0 1 3 4 0 1 2 4 0 1 2 3
11 : 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 2 3 4</pre>
 
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight lang="python">from itertools import count, islice
 
def _basechange_int(num, b):
Line 1,507 ⟶ 1,977:
if __name__ == '__main__':
for b in (2, 3, 5, 11):
print(f"{b:>2}: {str(list(islice(fairshare(b), 25)))[1:-1]}")</langsyntaxhighlight>
 
{{out}}
Line 1,516 ⟶ 1,986:
 
===Functional===
<langsyntaxhighlight lang="python">'''Fairshare between two and more'''
 
from itertools import count, islice
Line 1,668 ⟶ 2,138:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>First 25 fairshare terms for a given number of players:
Line 1,680 ⟶ 2,150:
<code>digitsum</code> is defined at [http://rosettacode.org/wiki/Sum_digits_of_an_integer#Quackery Sum digits of an integer].
 
<langsyntaxhighlight Quackerylang="quackery"> [ dup dip digitsum mod ] is fairshare ( n n --> n )
 
' [ 2 3 5 11 ]
Line 1,687 ⟶ 2,157:
25 times
[ i^ over fairshare echo sp ]
drop cr ]</langsyntaxhighlight>
 
{{out}}
Line 1,698 ⟶ 2,168:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define (Thue-Morse base)
Line 1,732 ⟶ 2,202:
 
(module+ main
(Fairshare-between-two-and-more))</langsyntaxhighlight>
 
{{out}}
Line 1,753 ⟶ 2,223:
A lower correlation factor corresponds to an advantage, higher to a disadvantage, the closer to 1 it is, the fairer the algorithm. Absolute best possible advantage correlation is 0. Absolute worst is 2.
 
<syntaxhighlight lang="raku" perl6line>sub fairshare (\b) { ^∞ .hyper.map: { .polymod( b xx * ).sum % b } }
 
.say for <2 3 5 11>.map: { .fmt('%2d:') ~ .&fairshare[^25]».fmt('%2d').join: ', ' }
Line 1,770 ⟶ 2,240:
$iterations/$people, @range.min, @range.max, @range.max - @range.min;
}
}</langsyntaxhighlight>
<pre> 2: 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0
3: 0, 1, 2, 1, 2, 0, 2, 0, 1, 1, 2, 0, 2, 0, 1, 0, 1, 2, 2, 0, 1, 0, 1, 2, 1
Line 1,806 ⟶ 2,276:
=={{header|REXX}}==
Programming note: &nbsp; the &nbsp; '''base''' &nbsp; function (as coded herein) handles bases from base ten up to &nbsp; '''36'''.
<langsyntaxhighlight lang="rexx">/*REXX program calculates N terms of the fairshare sequence for some group of peoples.*/
parse arg n g /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 25 /*Not specified? Then use the default.*/
Line 1,822 ⟶ 2,292:
do while #>=b; y= substr(@, #//b + 1, 1)y; #= #%b; end; return substr(@, #+1, 1)y
/*──────────────────────────────────────────────────────────────────────────────────────*/
sumDigs: parse arg x; !=0; do i=1 for length(x); != !+pos(substr(x,i,1),@@); end; return !</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,832 ⟶ 2,302:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
str = []
people = [2,3,5,11]
Line 1,867 ⟶ 2,337:
svect = left(svect, len(svect) - 1)
? "[" + svect + "]"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,874 ⟶ 2,344:
5: [ 0, 1, 2, 3, 4, 1, 2, 3, 4, 0, 2, 3, 4, 0, 1, 3, 4, 0, 1, 2, 4, 0, 1, 2, 3]
11: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 2, 3, 4]
</pre>
 
=={{header|RPL}}==
<code>DIGITS</code> is defined at [[Sum digits of an integer#RPL|Sum digits of an integer]].
 
<code>D→B</code> is a slightly modified version of the program defined at [[Non-decimal radices/Convert#RPL|Non-decimal radices/Convert]], using uppercase characters to be compatible with <code>DIGITS</code>
≪ → b
≪ "" SWAP
'''WHILE''' DUP '''REPEAT'''
b MOD LAST / IP
SWAP DUP 9 > 55 48 IFTE + CHR
ROT + SWAP
'''END''' DROP
≫ ≫ '<span style="color:blue">D→B</span>' STO
≪ → b
≪ { 0 } 1 24 '''FOR''' j
j b <span style="color:blue">D→B</span> <span style="color:blue">DIGITS</span> b MOD + '''NEXT'''
≫ ≫ '<span style="color:blue">TASK</span>' STO
 
2 <span style="color:blue">TASK</span> 3 <span style="color:blue">TASK</span> 5 <span style="color:blue">TASK</span> 11 <span style="color:blue">TASK</span>
{{out}}
<pre>
4: { 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 }
3: { 0 1 2 1 2 0 2 0 1 1 2 0 2 0 1 0 1 2 2 0 1 0 1 2 1 }
2: { 0 1 2 3 4 1 2 3 4 0 2 3 4 0 1 3 4 0 1 2 4 0 1 2 3 }
1: { 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 2 3 4 }
</pre>
 
=={{header|Ruby}}==
{{trans|C}}
<langsyntaxhighlight lang="ruby">def turn(base, n)
sum = 0
while n != 0 do
Line 1,944 ⟶ 2,441:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>Base 2: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
Line 1,956 ⟶ 2,453:
With 50000 people: 1
With 50001 people: Only 50000 have a turn</pre>
{{trans|Sidef}}
<syntaxhighlight lang="ruby">
def fairshare(base, upto) = (0...upto).map{|n| n.digits(base).sum % base}
 
upto = 25
[2, 3, 5, 11].each{|b| puts"#{'%2d' % b}: " + " %2d"*upto % fairshare(b, upto)}
</syntaxhighlight>
{{out}}
<pre>
2: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
3: 0 1 2 1 2 0 2 0 1 1 2 0 2 0 1 0 1 2 2 0 1 0 1 2 1
5: 0 1 2 3 4 1 2 3 4 0 2 3 4 0 1 3 4 0 1 2 4 0 1 2 3
11: 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 2 3 4
</pre>
 
=={{header|Rust}}==
{{trans|Python}}
<langsyntaxhighlight lang="rust">struct Digits {
rest: usize,
base: usize,
Line 2,004 ⟶ 2,515:
println!("{}: {:?}", i, fair_sharing(i).take(25).collect::<Vec<usize>>());
}
}</langsyntaxhighlight>
{{out}}
<pre>2: [0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0]
Line 2,012 ⟶ 2,523:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">for b in (2,3,5,11) {
say ("#{'%2d' % b}: ", 25.of { .sumdigits(b) % b })
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,025 ⟶ 2,536:
=={{header|Visual Basic .NET}}==
{{trans|C}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Function Turn(base As Integer, n As Integer) As Integer
Line 2,097 ⟶ 2,608:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Base 2: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
Line 2,109 ⟶ 2,620:
With 50000 people: 1
With 50001 people: Only 50000 have a turn</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn fairshare(n int, base int) []int {
mut res := []int{len: n}
for i in 0..n {
mut j := i
mut sum := 0
for j > 0 {
sum += j % base
j /= base
}
res[i] = sum % base
}
return res
}
fn turns(n int, fss []int) string {
mut m := map[int]int{}
for fs in fss {
m[fs]++
}
mut m2 := map[int]int{}
for _,v in m {
m2[v]++
}
mut res := []int{}
mut sum := 0
for k, v in m2 {
sum += v
res << k
}
if sum != n {
return "only $sum have a turn"
}
res.sort()
mut res2 := []string{len: res.len}
for i,_ in res {
res2[i] = '${res[i]}'
}
return res2.join(" or ")
}
fn main() {
for base in [2, 3, 5, 11] {
println("${base:2} : ${fairshare(25, base):2}")
}
println("\nHow many times does each get a turn in 50000 iterations?")
for base in [191, 1377, 49999, 50000, 50001] {
t := turns(base, fairshare(50000, base))
println(" With $base people: $t")
}
}</syntaxhighlight>
 
{{out}}
<pre>
2 : [0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0]
3 : [0, 1, 2, 1, 2, 0, 2, 0, 1, 1, 2, 0, 2, 0, 1, 0, 1, 2, 2, 0, 1, 0, 1, 2, 1]
5 : [0, 1, 2, 3, 4, 1, 2, 3, 4, 0, 2, 3, 4, 0, 1, 3, 4, 0, 1, 2, 4, 0, 1, 2, 3]
11 : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 2, 3, 4]
 
How many times does each get a turn in 50000 iterations?
With 191 people: 261 or 262
With 1377 people: 36 or 37
With 49999 people: 1 or 2
With 50000 people: 1
With 50001 people: only 50000 have a turn
</pre>
 
=={{header|Wren}}==
Line 2,114 ⟶ 2,693:
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
import "./sort" for Sort
 
var fairshare = Fn.new { |n, base|
Line 2,172 ⟶ 2,751:
var t = turns.call(base, fairshare.call(50000, base))
Fmt.print(" With $5d people: $s", base, t)
}</langsyntaxhighlight>
 
{{out}}
Line 2,187 ⟶ 2,766:
With 50000 people: 1
With 50001 people: only 50000 have a turn
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">proc Fair(Base); \Show first 25 terms of fairshare sequence
int Base, Count, Sum, N, Q;
[RlOut(0, float(Base)); Text(0, ": ");
for Count:= 0 to 25-1 do
[Sum:= 0; N:= Count;
while N do
[Q:= N/Base;
Sum:= Sum + rem(0);
N:= Q;
];
RlOut(0, float(rem(Sum/Base)));
];
CrLf(0);
];
 
[Format(3,0);
Fair(2); Fair(3); Fair(5); Fair(11);
]</syntaxhighlight>
 
{{out}}
<pre>
2: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
3: 0 1 2 1 2 0 2 0 1 1 2 0 2 0 1 0 1 2 2 0 1 0 1 2 1
5: 0 1 2 3 4 1 2 3 4 0 2 3 4 0 1 3 4 0 1 2 4 0 1 2 3
11: 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 2 3 4
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn fairShare(n,b){ // b<=36
n.pump(List,'wrap(n){ n.toString(b).split("").apply("toInt",b).sum(0)%b })
}
foreach b in (T(2,3,5,11)){
println("%2d: %s".fmt(b,fairShare(25,b).pump(String,"%2d ".fmt)));
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,204 ⟶ 2,811:
</pre>
For any base > 1
<langsyntaxhighlight lang="zkl">fcn fairShare(n,base){
sum,r := 0,0; while(n){ n,r = n.divr(base); sum+=r }
sum%base
Line 2,210 ⟶ 2,817:
foreach b in (T(2,3,5,11)){
println("%2d: %s".fmt(b,[0..24].pump(String,fairShare.fp1(b),"%2d ".fmt)));
}</langsyntaxhighlight>
338

edits