Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
35
Task/Mian-Chowla-sequence/Nim/mian-chowla-sequence.nim
Normal file
35
Task/Mian-Chowla-sequence/Nim/mian-chowla-sequence.nim
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
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"
|
||||
Loading…
Add table
Add a link
Reference in a new issue