Skip to content

Instantly share code, notes, and snippets.

@ogijun
Created January 30, 2011 20:24
Show Gist options
  • Save ogijun/803211 to your computer and use it in GitHub Desktop.
Save ogijun/803211 to your computer and use it in GitHub Desktop.
aj = [[0,1,2],[2,0,1],[1,2,0]]
n = 100
m = 10
while (n > 0)
hm = m/2
(1..(m-6)/4).each { |a| a3 = a**3
((a+1)..(((m2=m-a)-3)/3)).each { |b| b3 = b**3
# a+b+c+d == m; a+d == b+c (mod 6) => c == m/2-b (mod 3)
c = b+1; c += aj[c%3][(hm-b)%3]
upper_c = ((m3=m2-b)-1)/2
while (c <= upper_c)
d = m3-c
if ((s = a3 + d**3) == b3 + c**3)
p [s, [a,d], [b,c]]
exit if ((n -= 1) <= 0)
end
c += 3
end
}
}
m += 2
end
def sumcube e
e[0]**3+e[1]**3
end
def taxi_nums n
b = yield ; found = 0
while found < n
a = yield
#puts "#{a.inspect} => #{sumcube a}"
if a[0] == b[0]
a,b = b,a if a[1][0] > b[1][0]
puts "#{a[1].inspect} , #{b[1].inspect} => #{a[0]}"
found += 1
end
b = a
end
end
queue = [[9,[1,2]]]
taxi_nums(30){
r = queue.shift
a = r[1][0]
b = r[1][1]
c = [a,b+1]
queue << [sumcube(c),c]
if a+2 == b
c = [a+1,b]
queue << [sumcube(c),c]
end
queue.sort!{|a,b|
a[0] <=> b[0]
}
r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment