|
|
|
|
|
by xenophanes
5692 days ago
|
|
Thanks! I worked out a different way, too. SPOILER For just positive numbers, have the first number count up to the second number. Then increment the second number and reset the first to 0. Then adjusting for negatives is no big deal. Pretty simple in concept. Here's non-simple ruby code for calculating the pair for any step directly: def inefficient(n)
count = 0
while n > count
count += 1
n -= count
end
[n, count]
end
def with_negatives(n)
# wastes some steps trying to get the negative version of 0. whatever.
tmp = inefficient(n/4)
if n%4 == 1
tmp[0] *= -1
elsif n%4 == 2
tmp[1] *= -1
elsif n%4 == 3
tmp[0] *= -1
tmp[1] *= -1
end
tmp
end
50.times {|n| puts "#{n}: " + with_negatives(n).inspect}
|
|