How to find all taxicab numbers less than N?

前端 未结 8 1520
攒了一身酷
攒了一身酷 2021-01-30 14:11

A taxicab number is an integer that can be expressed as the sum of two cubes of integers in two different ways: a^3+b^3 = c^3+d^3. Design an algorithm to find all t

8条回答
  •  孤独总比滥情好
    2021-01-30 14:55

    1. create an array: 1^3, 2^3, 3^3, 4^3, ....... k^3. such that k^3 < N and (k+1)^3 > N. the array size would be ~ (N)^(1/3). the array is sorted order.
    2. use 2sum technique (link) in lineal time proportional to the array size. if we find 2 pairs of numbers, that is a hit.
    3. looping through step 2 by decreasing N by 1 each time.

    This will use O(N^(1/3)) extra space and ~ O(N^(4/3)) time.

提交回复
热议问题