Fairshare between two and more: Difference between revisions

Content added Content deleted
m (highlighted the "variable" b.)
Line 1,174: Line 1,174:
With 50000 people: 1
With 50000 people: 1
With 50001 people: Only 50000 have a turn</pre>
With 50001 people: Only 50000 have a turn</pre>

=={{header|Nim}}==
<lang Nim>import math, strutils

#---------------------------------------------------------------------------------------------------

iterator countInBase(b: Positive): seq[Natural] =
## Yield the successive integers in base "b" as a sequence of digits.
var value = @[Natural 0]
yield value
while true:
var c = 1
for i in countdown(value.high, 0):
let val = value[i] + c
if val < b:
value[i] = val
c = 0
else:
value[i] = val - b
c = 1
if c == 1:
# Add a new digit at the beginning.
# In this case, for better performances, we could have add it at the end.
value.insert(c, 0)
yield value

#---------------------------------------------------------------------------------------------------

func thueMorse(b: Positive; count: Natural): seq[Natural] =
## Return the "count" first elements of Thue-Morse sequence for base "b".
var count = count
for n in countInBase(b):
result.add(sum(n) mod b)
dec count
if count == 0: break

#———————————————————————————————————————————————————————————————————————————————————————————————————

for base in [2, 3, 5, 11]:
echo "Base ", ($base & ": ").alignLeft(4), thueMorse(base, 25).join(" ")</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
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|Perl}}==
=={{header|Perl}}==