RosettaCodeData/Task/Van-der-Corput-sequence/Pluto/van-der-corput-sequence.pluto

36 lines
970 B
Plaintext

do --[[ show members of the van der Corput sequence in various bases
translated from the C sample
]]
local int = require( "int" ) -- RC integer module - includes gcd
-- returns the numerator and denominator of the nth member of the van der Corput sequence
-- in the specified base
local function vc( nth : number, base : number )
local p, q, n = 0, 1, nth
while n != 0 do
p *= base
p += n % base
q *= base
n //= base
end
num, denom = p, q
-- return the numerator and denominator reduced by their gcd
q = int.gcd( num, denom )
return num // q, denom // q
end
-- task
for b = 2, 5 do
io.write( $"base {b}:" )
for i = 0, 9 do
local n, d = vc( i, b )
io.write( if n != 0 then $" {n}/{d}" else " 0" end )
end
io.write( "\n" )
end
end