Mian-Chowla sequence: Difference between revisions

Line 1,146:
[22526, 23291, 23564, 23881, 24596, 24768, 25631, 26037, 26255, 27219]
</pre>
 
=={{header|Nim}}==
<lang Nim>import intsets, strutils, times
 
proc mianchowla(n: Positive): seq[int] =
result = @[1]
var sums = [2].toIntSet()
var candidate = 1
 
while result.len < n:
# Test successive candidates.
var fit = false
result.add 0 # Make room for next value.
while not fit:
inc candidate
fit = true
result[^1] = candidate
# Check the sums.
for val in result:
if val + candidate in sums:
# Failed to satisfy criterium.
fit = false
break
# Add the new sums to the set of sums.
for val in result:
sums.incl val + candidate
 
let t0 = now()
let seq100 = mianchowla(100)
echo "The first 30 terms of the Mian-Chowla sequence are:"
echo seq100[0..29].join(" ")
echo ""
echo "Terms 91 to 100 of the sequence are:"
echo seq100[90..99].join(" ")
 
echo ""
echo "Computation time: ", (now() - t0).inMilliseconds, " ms"</lang>
 
{{out}}
<pre>The first 30 terms of the Mian-Chowla sequence are:
1 2 4 8 13 21 31 45 66 81 97 123 148 182 204 252 290 361 401 475 565 593 662 775 822 916 970 1016 1159 1312
 
Terms 91 to 100 of the sequence are:
22526 23291 23564 23881 24596 24768 25631 26037 26255 27219
 
Computation time: 2 ms</pre>
 
=={{header|Pascal}}==
Anonymous user