Something along these lines.
results = [1]
i_index = 0
j_index = 0
for(count=0, count<n, count ++){
i_incr = results[i_index]*2 // next value of expression by incrementing i
j_incr = results[j_index]*3 // next value of expression by incrementing j
if (i_incr > j_incr)
results << j_incr
j_index += 1
else if (i_incr < j_incr)
results << i_incr
i_index += 1
else
results << i_incr
i_index += 1
j_index += 1
end
}
The state is maintained by i_index
and j_index
, they keep track of the last value that has not been incremented for those indices resp. .