Fairshare between two and more: Difference between revisions

Add C# implementation
No edit summary
(Add C# implementation)
 
(2 intermediate revisions by 2 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)
r = 0{
foreach (int baseValue in new List<int> { 2, 3, 5, 11 })
write r & " "{
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 2,649 ⟶ 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