35 lines
1.1 KiB
Text
35 lines
1.1 KiB
Text
do --[[ Find Mian-Chowla numbers: an
|
|
where: ai = 1,
|
|
and: an = smallest integer such that ai + aj is unique
|
|
for all i, j in 1 .. n && i <= j
|
|
]]
|
|
local fmt = require( "fmt" )
|
|
|
|
local maxMc, mcCount, mc, isSum = 100, 1, {}, {}
|
|
local i = 0
|
|
while mcCount <= maxMc do
|
|
++ i
|
|
-- assume i will be part of the sequence
|
|
mc[ mcCount ] = i
|
|
-- check the sums
|
|
local isUnique = true
|
|
for mcPos = 1, mcCount do
|
|
if isSum[ i + mc[ mcPos ] ] then
|
|
isUnique = false
|
|
break
|
|
end
|
|
end
|
|
if isUnique then
|
|
-- i is a sequence element - store the sums
|
|
for k = 1, mcCount do isSum[ i + mc[ k ] ] = true end
|
|
++ mcCount
|
|
end
|
|
end
|
|
|
|
-- print parts of the sequence
|
|
io.write( "Mian Chowla sequence elements 1..30:\n " )
|
|
fmt.lprint( table.slice( mc, 1, 30 ), " ", "" )
|
|
io.write( "Mian Chowla sequence elements 91..100:\n " )
|
|
fmt.lprint( table.slice( mc, 91, 100 ), " ", "" )
|
|
|
|
end
|