Fairshare between two and more: Difference between revisions

Add C# implementation
(Add C# implementation)
 
(4 intermediate revisions by 4 users not shown)
Line 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 693 ⟶ 738:
{{trans|Cowgol}}
<syntaxhighlight lang="easylang">
procfunc fairshare ind base . r .
r = 0
while ind > 0
r = r += ind mod base
ind = ind div base
.
r = r mod base
return r
.
proc sequence n base . .
write base & ": "
for ind range0 n
callwrite (fairshare ind base) r& " "
write r & " "
.
print ""
.
call sequence 25 2
call sequence 25 3
call sequence 25 5
call sequence 25 11
</syntaxhighlight>
 
Line 797 ⟶ 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}}==
Line 2,359 ⟶ 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,585 ⟶ 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