Fairshare between two and more: Difference between revisions
m
syntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 41:
{{trans|Python}}
<
‘
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])</
{{out}}
Line 73:
=={{header|AppleScript}}==
<
on thueMorse(base)
-- Non-finite sequence of Thue-Morse terms for a given base.
Line 348:
set my text item delimiters to dlm
s
end unlines</
{{Out}}
<pre>First 25 fairshare terms for N players:
Line 358:
=={{header|Arturo}}==
<
i: 0
result: new []
Line 373:
(pad.right "Base "++(to :string b) 7)++" =>"
join.with:" " map to [:string] thueMorse b 25 'x -> pad x 2
]</
{{out}}
Line 383:
=={{header|C}}==
<
#include <stdlib.h>
Line 462:
return 0;
}</
{{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 477:
=={{header|C++}}==
{{trans|C}}
<
#include <vector>
Line 546:
return 0;
}</
{{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 561:
=={{header|D}}==
{{trans|Kotlin}}
<
import std.stdio;
Line 629:
turnCount(50000, 50000);
turnCount(50001, 50000);
}</
{{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 644:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<
: nth-fairshare ( n base -- m )
Line 653:
{ 2 3 5 11 }
[ 25 over <fairshare> "%2d -> %u\n" printf ] each</
{{out}}
<pre>
Line 665:
=={{header|FreeBASIC}}==
{{trans|Visual Basic .NET}}
<
Function Turn(mibase As Integer, n As Integer) As Integer
Dim As Integer sum = 0
Line 728:
TurnCount(50001, 50000)
Sleep
</syntaxhighlight>
{{out}}
<pre>
Line 736:
=={{header|Go}}==
<
import (
Line 794:
fmt.Printf(" With %d people: %s\n", base, t)
}
}</
{{out}}
Line 812:
=={{header|Haskell}}==
<
import Data.List (intercalate, unfoldr)
import Data.Tuple (swap)
Line 871:
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</
{{Out}}
<pre>First 25 fairshare terms for a given number of players:
Line 905:
=={{header|Java}}==
<
import java.util.ArrayList;
import java.util.Arrays;
Line 935:
}
</syntaxhighlight>
{{out}}
<pre>
Line 945:
=={{header|JavaScript}}==
<
'use strict';
Line 1,135:
// MAIN ---
return main();
})();</
{{Out}}
<pre>First 25 fairshare terms for a given number of players:
Line 1,145:
=={{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.
<
# generate an unbounded stream of the integers from [0] onwards.
# E.g. for binary: [0], [1], [0,1], [1,1] ...
Line 1,171:
(2,3,5,11)
| "Fairshare \((select(.>2)|"among") // "between") \(.) people: \([fairshare(.; 25)])"
</syntaxhighlight>
{{out}}
As for Julia.
=={{header|Julia}}==
<
for n in [2, 3, 5, 11]
println("Fairshare ", n > 2 ? "among" : "between", " $n people: ", fairshare(n, 25))
end
</
<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:
=={{header|Kotlin}}==
{{trans|Visual Basic .NET}}
<
var sum = 0
var n2 = n
Line 1,261:
turnCount(50001, 50000)
}
</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
Line 1,276:
=={{header|Lua}}==
{{trans|D}}
<
local sum = 0
while n ~= 0 do
Line 1,350:
end
main()</
{{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:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
Fairshare[n_List, b_Integer : 2] := Fairshare[#, b] & /@ n
Fairshare[n_Integer, b_Integer : 2] := Mod[Total[IntegerDigits[n, b]], b]
Line 1,370:
Fairshare[Range[0, 24], 3]
Fairshare[Range[0, 24], 5]
Fairshare[Range[0, 24], 11]</
{{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:
=={{header|Nim}}==
<
#---------------------------------------------------------------------------------------------------
Line 1,422:
for base in [2, 3, 5, 11]:
echo "Base ", ($base & ": ").alignLeft(4), thueMorse(base, 25).join(" ")</
{{out}}
Line 1,432:
=={{header|Perl}}==
{{trans|Raku}}
<
use warnings;
use Math::AnyNum qw(sum polymod);
Line 1,443:
for (<2 3 5 11>) {
printf "%3s:%s\n", $_, fairshare($_, 25);
}</
{{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:
=={{header|Phix}}==
{{trans|Go}}
<!--<
<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:
<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>
<!--</
{{out}}
<pre>
Line 1,482:
=={{header|Python}}==
===Procedural===
<
def _basechange_int(num, b):
Line 1,507:
if __name__ == '__main__':
for b in (2, 3, 5, 11):
print(f"{b:>2}: {str(list(islice(fairshare(b), 25)))[1:-1]}")</
{{out}}
Line 1,516:
===Functional===
<
from itertools import count, islice
Line 1,668:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>First 25 fairshare terms for a given number of players:
Line 1,680:
<code>digitsum</code> is defined at [http://rosettacode.org/wiki/Sum_digits_of_an_integer#Quackery Sum digits of an integer].
<
' [ 2 3 5 11 ]
Line 1,687:
25 times
[ i^ over fairshare echo sp ]
drop cr ]</
{{out}}
Line 1,698:
=={{header|Racket}}==
<
(define (Thue-Morse base)
Line 1,732:
(module+ main
(Fairshare-between-two-and-more))</
{{out}}
Line 1,753:
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"
.say for <2 3 5 11>.map: { .fmt('%2d:') ~ .&fairshare[^25]».fmt('%2d').join: ', ' }
Line 1,770:
$iterations/$people, @range.min, @range.max, @range.max - @range.min;
}
}</
<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:
=={{header|REXX}}==
Programming note: the '''base''' function (as coded herein) handles bases from base ten up to '''36'''.
<
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:
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 !</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,832:
=={{header|Ring}}==
<
str = []
people = [2,3,5,11]
Line 1,867:
svect = left(svect, len(svect) - 1)
? "[" + svect + "]"
</syntaxhighlight>
{{out}}
<pre>
Line 1,878:
=={{header|Ruby}}==
{{trans|C}}
<
sum = 0
while n != 0 do
Line 1,944:
end
main()</
{{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,959:
=={{header|Rust}}==
{{trans|Python}}
<
rest: usize,
base: usize,
Line 2,004:
println!("{}: {:?}", i, fair_sharing(i).take(25).collect::<Vec<usize>>());
}
}</
{{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:
=={{header|Sidef}}==
<
say ("#{'%2d' % b}: ", 25.of { .sumdigits(b) % b })
}</
{{out}}
<pre>
Line 2,025:
=={{header|Visual Basic .NET}}==
{{trans|C}}
<
Function Turn(base As Integer, n As Integer) As Integer
Line 2,097:
End Sub
End Module</
{{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,112:
=={{header|Vlang}}==
{{trans|Go}}
<
mut res := []int{len: n}
for i in 0..n {
Line 2,161:
println(" With $base people: $t")
}
}</
{{out}}
Line 2,182:
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
<
import "/sort" for Sort
Line 2,240:
var t = turns.call(base, fairshare.call(50000, base))
Fmt.print(" With $5d people: $s", base, t)
}</
{{out}}
Line 2,258:
=={{header|XPL0}}==
<
int Base, Count, Sum, N, Q;
[RlOut(0, float(Base)); Text(0, ": ");
Line 2,275:
[Format(3,0);
Fair(2); Fair(3); Fair(5); Fair(11);
]</
{{out}}
Line 2,286:
=={{header|zkl}}==
<
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)));
}</
{{out}}
<pre>
Line 2,300:
</pre>
For any base > 1
<
sum,r := 0,0; while(n){ n,r = n.divr(base); sum+=r }
sum%base
Line 2,306:
foreach b in (T(2,3,5,11)){
println("%2d: %s".fmt(b,[0..24].pump(String,fairShare.fp1(b),"%2d ".fmt)));
}</
|