##- # Author: Brian Tiffin # Dedicated to the public domain # # Date: October 2016 # Modified: 2016-10-17/05:47-0400 ##+ # # twosum.icn, find two array elements that add up to a given sum # a modification of http://rosettacode.org/wiki/Two_Sum # link fullimag procedure main(arglist) # predictable results for the UP docset &random := 1 # target sum sum := pop(arglist) | ?100 # create a list of the rest of the arguments, or make it up L := [] if *arglist > 0 then every put(L, integer(!arglist)) else every 1 to 20 do put(L, ?100) writes(sum, ", ") write(fullimage(L)) # keep track of result count results := 0 every write(fullimage(pair := twosum(sum, L)), " ", L[pair[1]], " ", L[pair[2]]) do results +:= 1 if results = 0 then write("[], no solution") end # O(n^2), nested iterations procedure twosum(sum, L) every i := 1 to *L-1 do every j := i+1 to *L do if L[i] + L[j] = sum then suspend [i,j] end