Jump to content

Fairshare between two and more: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 41:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F _basechange_int(=num, b)
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])</langsyntaxhighlight>
 
{{out}}
Line 73:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">-- thueMorse :: Int -> [Int]
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</langsyntaxhighlight>
{{Out}}
<pre>First 25 fairshare terms for N players:
Line 358:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">thueMorse: function [base, howmany][
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
]</langsyntaxhighlight>
 
{{out}}
Line 383:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 462:
 
return 0;
}</langsyntaxhighlight>
{{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}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
Line 546:
 
return 0;
}</langsyntaxhighlight>
{{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}}
<langsyntaxhighlight lang="d">import std.array;
import std.stdio;
 
Line 629:
turnCount(50000, 50000);
turnCount(50001, 50000);
}</langsyntaxhighlight>
{{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}}
<langsyntaxhighlight lang="factor">USING: formatting kernel math math.parser sequences ;
 
: nth-fairshare ( n base -- m )
Line 653:
 
{ 2 3 5 11 }
[ 25 over <fairshare> "%2d -> %u\n" printf ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 665:
=={{header|FreeBASIC}}==
{{trans|Visual Basic .NET}}
<langsyntaxhighlight lang="freebasic">
Function Turn(mibase As Integer, n As Integer) As Integer
Dim As Integer sum = 0
Line 728:
TurnCount(50001, 50000)
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 736:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 794:
fmt.Printf(" With %d people: %s\n", base, t)
}
}</langsyntaxhighlight>
 
{{out}}
Line 812:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Bool (bool)
import Data.List (intercalate, unfoldr)
import Data.Tuple (swap)
Line 871:
 
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</langsyntaxhighlight>
{{Out}}
<pre>First 25 fairshare terms for a given number of players:
Line 905:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.Arrays;
Line 935:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 945:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,135:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="jq"># Using a "reverse array" representations of the integers base b (b>=2),
# 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>
</lang>
{{out}}
As for Julia.
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">fairshare(nplayers,len) = [sum(digits(n, base=nplayers)) % nplayers for n in 0:len-1]
 
for n in [2, 3, 5, 11]
println("Fairshare ", n > 2 ? "among" : "between", " $n people: ", fairshare(n, 25))
end
</langsyntaxhighlight>{{out}}
<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}}
<langsyntaxhighlight lang="scala">fun turn(base: Int, n: Int): Int {
var sum = 0
var n2 = n
Line 1,261:
turnCount(50001, 50000)
}
</syntaxhighlight>
</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
Line 1,276:
=={{header|Lua}}==
{{trans|D}}
<langsyntaxhighlight lang="lua">function turn(base, n)
local sum = 0
while n ~= 0 do
Line 1,350:
end
 
main()</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Fairshare]
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]</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight Nimlang="nim">import math, strutils
 
#---------------------------------------------------------------------------------------------------
Line 1,422:
 
for base in [2, 3, 5, 11]:
echo "Base ", ($base & ": ").alignLeft(4), thueMorse(base, 25).join(" ")</langsyntaxhighlight>
 
{{out}}
Line 1,432:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use Math::AnyNum qw(sum polymod);
Line 1,443:
for (<2 3 5 11>) {
printf "%3s:%s\n", $_, fairshare($_, 25);
}</langsyntaxhighlight>
{{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}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,482:
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight lang="python">from itertools import count, islice
 
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]}")</langsyntaxhighlight>
 
{{out}}
Line 1,516:
 
===Functional===
<langsyntaxhighlight lang="python">'''Fairshare between two and more'''
 
from itertools import count, islice
Line 1,668:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{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].
 
<langsyntaxhighlight Quackerylang="quackery"> [ dup dip digitsum mod ] is fairshare ( n n --> n )
 
' [ 2 3 5 11 ]
Line 1,687:
25 times
[ i^ over fairshare echo sp ]
drop cr ]</langsyntaxhighlight>
 
{{out}}
Line 1,698:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define (Thue-Morse base)
Line 1,732:
 
(module+ main
(Fairshare-between-two-and-more))</langsyntaxhighlight>
 
{{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" perl6line>sub fairshare (\b) { ^∞ .hyper.map: { .polymod( b xx * ).sum % b } }
 
.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;
}
}</langsyntaxhighlight>
<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: &nbsp; the &nbsp; '''base''' &nbsp; function (as coded herein) handles bases from base ten up to &nbsp; '''36'''.
<langsyntaxhighlight lang="rexx">/*REXX program calculates N terms of the fairshare sequence for some group of peoples.*/
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 !</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,832:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
str = []
people = [2,3,5,11]
Line 1,867:
svect = left(svect, len(svect) - 1)
? "[" + svect + "]"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,878:
=={{header|Ruby}}==
{{trans|C}}
<langsyntaxhighlight lang="ruby">def turn(base, n)
sum = 0
while n != 0 do
Line 1,944:
end
 
main()</langsyntaxhighlight>
{{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}}
<langsyntaxhighlight lang="rust">struct Digits {
rest: usize,
base: usize,
Line 2,004:
println!("{}: {:?}", i, fair_sharing(i).take(25).collect::<Vec<usize>>());
}
}</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight lang="ruby">for b in (2,3,5,11) {
say ("#{'%2d' % b}: ", 25.of { .sumdigits(b) % b })
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,025:
=={{header|Visual Basic .NET}}==
{{trans|C}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Function Turn(base As Integer, n As Integer) As Integer
Line 2,097:
End Sub
 
End Module</langsyntaxhighlight>
{{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}}
<langsyntaxhighlight lang="vlang">fn fairshare(n int, base int) []int {
mut res := []int{len: n}
for i in 0..n {
Line 2,161:
println(" With $base people: $t")
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,182:
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
import "/sort" for Sort
 
Line 2,240:
var t = turns.call(base, fairshare.call(50000, base))
Fmt.print(" With $5d people: $s", base, t)
}</langsyntaxhighlight>
 
{{out}}
Line 2,258:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">proc Fair(Base); \Show first 25 terms of fairshare sequence
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);
]</langsyntaxhighlight>
 
{{out}}
Line 2,286:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn fairShare(n,b){ // b<=36
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)));
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,300:
</pre>
For any base > 1
<langsyntaxhighlight lang="zkl">fcn fairShare(n,base){
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)));
}</langsyntaxhighlight>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.