Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,27 @@
|
|||
PROGRAM VAN_DER_CORPUT
|
||||
|
||||
!
|
||||
! for rosettacode.org
|
||||
!
|
||||
|
||||
PROCEDURE VDC(N%,B%->RES)
|
||||
LOCAL V,S%
|
||||
S%=1
|
||||
WHILE N%>0 DO
|
||||
S%*=B%
|
||||
V+=(N% MOD B%)/S%
|
||||
N%=N% DIV B%
|
||||
END WHILE
|
||||
RES=V
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
FOR BASE%=2 TO 5 DO
|
||||
PRINT("Base";STR$(BASE%);":")
|
||||
FOR NUMBER%=0 TO 9 DO
|
||||
VDC(NUMBER%,BASE%->RES)
|
||||
WRITE("#.##### ";RES;)
|
||||
END FOR
|
||||
PRINT
|
||||
END FOR
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
' version 03-12-2016
|
||||
' compile with: fbc -s console
|
||||
|
||||
Function num_base(number As ULongInt, _base_ As UInteger) As String
|
||||
|
||||
If _base_ > 9 Then
|
||||
Print "base not handled by function"
|
||||
Sleep 5000
|
||||
Return ""
|
||||
End If
|
||||
|
||||
Dim As ULongInt n
|
||||
Dim As String ans
|
||||
|
||||
While number <> 0
|
||||
n = number Mod _base_
|
||||
ans = Str(n) + ans
|
||||
number = number \ _base_
|
||||
Wend
|
||||
|
||||
If ans = "" Then ans = "0"
|
||||
|
||||
Return "." + ans
|
||||
|
||||
End Function
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As ULong k, l
|
||||
For k = 2 To 5
|
||||
Print "Base = "; k
|
||||
For l = 0 To 12
|
||||
Print left(num_base(l, k) + " ",6);
|
||||
Next
|
||||
Print : print
|
||||
Next
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
enum BASE, FRAC, DECIMAL
|
||||
constant DESC = {"Base","Fraction","Decimal"}
|
||||
|
||||
function vdc(integer n, atom base, integer flag)
|
||||
object res = ""
|
||||
atom num = 0, denom = 1, digit, g
|
||||
while n do
|
||||
denom *= base
|
||||
digit = remainder(n,base)
|
||||
n = floor(n/base)
|
||||
if flag=BASE then
|
||||
res &= digit+'0'
|
||||
else
|
||||
num = num*base+digit
|
||||
end if
|
||||
end while
|
||||
if flag=FRAC then
|
||||
g = gcd(num,denom)
|
||||
return {num/g,denom/g}
|
||||
elsif flag=DECIMAL then
|
||||
return num/denom
|
||||
end if
|
||||
return {iff(length(res)=0?"0":"0."&res)}
|
||||
end function
|
||||
|
||||
procedure show_vdc(integer flag, string fmt)
|
||||
object v
|
||||
for i=2 to 5 do
|
||||
printf(1,"%s %d: ",{DESC[flag],i})
|
||||
for j=0 to 9 do
|
||||
v = vdc(j,i,flag)
|
||||
if flag=FRAC and v[1]=0 then
|
||||
printf(1,"0 ")
|
||||
else
|
||||
printf(1,fmt,v)
|
||||
end if
|
||||
end for
|
||||
puts(1,"\n")
|
||||
end for
|
||||
end procedure
|
||||
|
||||
show_vdc(BASE,"%s ")
|
||||
show_vdc(FRAC,"%d/%d ")
|
||||
show_vdc(DECIMAL,"%g ")
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
decimals(4)
|
||||
for base = 2 to 5
|
||||
see "base " + string(base) + " : "
|
||||
for number = 0 to 9
|
||||
see "" + corput(number, base) + " "
|
||||
next
|
||||
see nl
|
||||
next
|
||||
|
||||
func corput n, b
|
||||
vdc = 0
|
||||
denom = 1
|
||||
while n
|
||||
denom *= b
|
||||
rem = n % b
|
||||
n = floor(n/b)
|
||||
vdc += rem / denom
|
||||
end
|
||||
return vdc
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
func vdc(value, base=2) {
|
||||
while (value[-1] > 0) {
|
||||
value.append(value[-1] / base -> int);
|
||||
}
|
||||
var (x, sum) = (1, 0);
|
||||
value.each { |i|
|
||||
sum += ((i % base) / (x *= base));
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
2.to(5).each { |base|
|
||||
var seq = (10.range.map {|i| vdc([i], base) });
|
||||
"base %d: %s\n".printf(base, seq.map{|n| "%.4f" % n}.join(', '));
|
||||
}
|
||||
16
Task/Van-der-Corput-sequence/jq/van-der-corput-sequence-1.jq
Normal file
16
Task/Van-der-Corput-sequence/jq/van-der-corput-sequence-1.jq
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
# vdc(base) converts an input decimal integer to a decimal number based on the van der
|
||||
# Corput sequence using base 'base', e.g. (4 | vdc(2)) is 0.125.
|
||||
#
|
||||
def vdc(base):
|
||||
|
||||
# The helper function converts a stream of residuals to a decimal,
|
||||
# e.g. if base is 2, then decimalize( (0,0,1) ) yields 0.125
|
||||
def decimalize(stream):
|
||||
reduce stream as $d # state: [accumulator, power]
|
||||
( [0, 1/base];
|
||||
.[1] as $power | [ .[0] + ($d * $power), $power / base] )
|
||||
| .[0];
|
||||
|
||||
if . == 0 then 0
|
||||
else decimalize(recurse( if . == 0 then empty else ./base | floor end ) % base)
|
||||
end ;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
def round(n):
|
||||
(if . < 0 then -1 else 1 end) as $s
|
||||
| $s*10*.*n | if (floor%10)>4 then (.+5) else . end | ./10 | floor/n | .*$s;
|
||||
|
||||
range(2;6) | . as $base | "Base \(.): \( [ range(0;11) | vdc($base)|round(1000) ] )"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
$ jq -n -f -c -r van_der_corput_sequence.jq
|
||||
Base 2: [0,0.5,0.25,0.75,0.125,0.625,0.375,0.875,0.063,0.563,0.313]
|
||||
Base 3: [0,0.333,0.667,0.111,0.444,0.778,0.222,0.556,0.889,0.037,0.37]
|
||||
Base 4: [0,0.25,0.5,0.75,0.063,0.313,0.563,0.813,0.125,0.375,0.625]
|
||||
Base 5: [0,0.2,0.4,0.6,0.8,0.04,0.24,0.44,0.64,0.84,0.08]
|
||||
Loading…
Add table
Add a link
Reference in a new issue