Fairshare between two and more: Difference between revisions

Add C# implementation
(add APL)
(Add C# implementation)
 
(11 intermediate revisions by 8 users not shown)
Line 70:
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|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>
 
Line 531 ⟶ 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++}}==
Line 615 ⟶ 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}}==
Line 698 ⟶ 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}}==
Line 718 ⟶ 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}}==
Line 1,486 ⟶ 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}}==
Line 1,536 ⟶ 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}}==
Line 1,931 ⟶ 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>
 
Line 2,013 ⟶ 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}}==
Line 2,239 ⟶ 2,693:
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./sort" for Sort
 
var fairshare = Fn.new { |n, base|
338

edits