Mian-Chowla sequence: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
(3 intermediate revisions by 2 users not shown)
Line 1,236:
 
<syntaxhighlight lang="kotlin">
fun sumsRemainDistinct(candidate: Int, sequenceseq: Iterable<Int>, mcSumssums: HashSetMutableSet<Int>): Boolean {
val candidateSums = mutableListOf<Int>()
 
for (s in sequenceseq) {
when ((candidate + s) !in mcSumssums) {
true -> candidateSums.add(candidate + s)
false -> return false
}
}
with(mcSumssums) {
addAll(candidateSums)
add(candidate + candidate)
Line 1,253:
 
fun mianChowla(n: Int): List<Int> {
val mcSequencebufferSeq = linkedSetOf<Int>(1)
val mcSumsbufferSums = linkedSetOf<Int>(2)
 
val sequence = generateSequence(1) { it + 1 } // [1,2,3,..]
var candidate = 2
if.filter { (sumsRemainDistinct(candidateit, mcSequencebufferSeq, mcSums)bufferSums) {}
while (mcSequence.size < n) {
.onEach { bufferSeq.add(it) }
if (sumsRemainDistinct(candidate, mcSequence, mcSums)) {
 
mcSequence.add(candidate)
return mcSequencesequence.take(n).toList()
}
candidate++
}
return mcSequence.toList()
}
 
Line 1,952 ⟶ 1,949:
=={{header|Sidef}}==
{{trans|Go}}
<syntaxhighlight lang="ruby">var (n, sums, ts, mc) = (100, Set([2]), [], [1])
var st = Time.micro_secmicro
for i in (1 ..^ n) {
for j in (mc[i-1]+1 .. Inf) {
Line 1,959 ⟶ 1,956:
for k in (0 .. i) {
var sum = mc[k]+j
if (sums.existshas(sum)) {
ts.clear
break
Line 1,966 ⟶ 1,963:
}
if (ts.len > 0) {
sums |= (sums|Set(ts...))
break
}
}
}
var et = (Time.micro_secmicro - st)
var s = " of the Mian-Chowla sequence are:\n"
say "The first 30 terms#{s}#{mc.ftfirst(0, 2930).join(' ')}\n"
say "Terms 91 to 100#{s}#{mc.ftslice(90, 99).first(10).join(' ')}\n"
say "Computation time was #{et} seconds."</syntaxhighlight>
{{out}}
Line 1,983 ⟶ 1,980:
22526 23291 23564 23881 24596 24768 25631 26037 26255 27219
 
Computation time was 32.98316288 seconds.</pre>
 
=={{header|Swift}}==
Line 2,185 ⟶ 2,182:
=={{header|Wren}}==
{{trans|C#}}
<syntaxhighlight lang="ecmascriptwrent">var mianChowla = Fn.new { |n|
var mc = List.filled(n, 0)
var sums = {}
9,482

edits