Two sum: Difference between revisions

Content added Content deleted
m (→‎{{header|Sidef}}: Fix link: Perl 6 --> Raku)
(Added Wren)
Line 2,270: Line 2,270:
ret
ret
</lang>
</lang>

=={{header|Wren}}==
<lang ecmascript>var twosum = Fn.new { |a, n|
var c = a.count
if (c < 2) return []
for (i in 0...c-1) {
for (j in i+1...c) {
var s = a[i] + a[j]
if (s == n) return [i, j]
if (s > n) break
}
}
return []
}

var a = [0, 2, 11, 19, 90]
System.print("Numbers: %(a)\n")
for (n in [21, 25, 90]) {
var pair = twosum.call(a, n)
if (pair.count == 2) {
System.print("Indices: %(pair) sum to %(n) (%(a[pair[0]]) + %(a[pair[1]]) = %(n))")
} else {
System.print("No pairs of the above numbers sum to %(n).")
}
System.print()
}</lang>

{{out}}
<pre>
Numbers: [0, 2, 11, 19, 90]

Indices: [1, 3] sum to 21 (2 + 19 = 21)

No pairs of the above numbers sum to 25.

Indices: [0, 4] sum to 90 (0 + 90 = 90)
</pre>


=={{header|zkl}}==
=={{header|zkl}}==