September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1 @@
--- {}

View file

@ -0,0 +1,15 @@
USING: formatting fry io kernel math math.functions math.parser
math.ranges sequences ;
IN: rosetta-code.van-der-corput
: vdc ( n base -- x )
[ >base string>digits <reversed> ]
[ nip '[ 1 + neg _ swap ^ * ] ] 2bi map-index sum ;
: vdc-demo ( -- )
2 5 [a,b] [
dup "Base %d: " printf 10 <iota>
[ swap vdc "%-5u " printf ] with each nl
] each ;
MAIN: vdc-demo

View file

@ -0,0 +1,36 @@
func vanDerCorput(n: Int, base: Int, num: inout Int, denom: inout Int) {
var n = n, p = 0, q = 1
while n != 0 {
p = p * base + (n % base)
q *= base
n /= base
}
num = p
denom = q
while p != 0 {
n = p
p = q % p
q = n
}
num /= q
denom /= q
}
var num = 0
var denom = 0
for base in 2...5 {
print("base \(base): 0 ", terminator: "")
for n in 1..<10 {
vanDerCorput(n: n, base: base, num: &num, denom: &denom)
print("\(num)/\(denom) ", terminator: "")
}
print()
}

View file

@ -0,0 +1,24 @@
Private Function vdc(ByVal n As Integer, BASE As Variant) As Variant
Dim res As String
Dim digit As Integer, g As Integer, denom As Integer
denom = 1
Do While n
denom = denom * BASE
digit = n Mod BASE
n = n \ BASE
res = res & CStr(digit) '+ "0"
Loop
vdc = IIf(Len(res) = 0, "0", "0." & res)
End Function
Public Sub show_vdc()
Dim v As Variant, j As Integer
For i = 2 To 5
Debug.Print "Base "; i; ": ";
For j = 0 To 9
v = vdc(j, i)
Debug.Print v; " ";
Next j
Debug.Print
Next i
End Sub

View file

@ -0,0 +1,30 @@
Module Module1
Function ToBase(n As Integer, b As Integer) As String
Dim result = ""
If b < 2 Or b > 16 Then
Throw New ArgumentException("The base is out of range")
End If
Do
Dim remainder = n Mod b
result = "0123456789ABCDEF"(remainder) + result
n = n \ b
Loop While n > 0
Return result
End Function
Sub Main()
For b = 2 To 5
Console.WriteLine("Base = {0}", b)
For i = 0 To 12
Dim s = "." + ToBase(i, b)
Console.Write("{0,6} ", s)
Next
Console.WriteLine()
Console.WriteLine()
Next
End Sub
End Module