represent a number by the sum of cubes of 2 distinct pairs of integers

312 Views Asked by At

I resolve this question by using kinds of iteration approach of subset sum (https://en.wikipedia.org/wiki/Subset_sum_problem), show my code below in Python 2.7. Wondering if any pure mathematical solution?

Source code in Python 2.7,

'''
# Print all integers that can be obtained as
# the sum of cubes of 2 distinct pairs of integers.

# Example: 12 pow 3 + 1 pow 3 =  1729 = 10 pow 3 + 9 pow 3


# x * x * x + y * y * y = num
# k * k * k + z * z * z = num

# (x,y) != (k, z) != (y, x)
'''
def two_sum(numbers, target):
    r = 0
    i = 0
    j = len(numbers) - 1
    while i<j:
        if numbers[i] + numbers[j] == target:
            r += 1
            i += 1
            j -= 1
        elif numbers[i] + numbers[j] > target:
            j -= 1
        else:
            i += 1
    return r

def find_number():
    cube_numbers = []
    result = []
    for i in range(100):
        cube_numbers.append(i*i*i)
    for i in range(1, 10001):
        r = two_sum(cube_numbers, i)
        if r == 2:
            result.append(i)
    return result

if __name__ == "__main__":
    print find_number()