简体   繁体   中英

For a user input n, and 1<=i<j<=n, find the number of pairs where i*i*i=j*j using python

For a user input n, and 1<=i<j<=n, find the number of pairs where i*i*i=j*j using python

The program needs to take input from the user and if user input is 50, the output should be 3 as there are 3 such pairs :(1,1), (4,8), (9,27)using python.

def solution(n):
    count=0
    for i in range(1,n):
       for j in range(i,n):
          if (i**3==j*j):
             count+=1
    return count
n=int(input())
out=solution(n)
print(out)

This is the function I wrote. It works, but in the site I am practicing, it times out and asks me to optimize it further. What can I do?

You may not count how many time you find a match, but save the indices :

def solution(n):
    result = []
    for i in range(1, n):
        for j in range(i, n):
            if i ** 3 == j ** 2:
                result.append((i, j))
    return result

# with list comprehension
def solution(n):
    return [(i, j) for i in range(1, n) for j in range(i, n) if i ** 3 == j ** 2]

OPTIMIZE

By looking at the values, you can determine which values can match, to get i**2 == j**3 t=you need i = x**3 and j = x**2 so one loop is sufficient :

def solution(n):
    result = []
    for i in range(1, ceil(n ** (1 / 3))):
        result.append((i ** 2, i ** 3))
    return result

# with list comprehension
def solution(n):
    return [(i ** 2, i ** 3) for i in range(1, ceil(n ** (1 / 3)))]

Being a programmer should not prevent to keep one minute away from the computer and thinking about the mathematical problem. As an integer can be factorized as a product of prime factors at a power, i and j have to share the same prime factors, and the equality will be true for each of those prime factors. But for prime factors is is evident that you need to have a common number k with: k 2 = i and k 3 = j.

So the problem can be reduced to finding all numbers k, k >= 1 and k 3 <= n. And the i,j pairs if you need them are just k 2 , k 3

A trivial way is:

def solution(n)
    count = 0
    for i in range(n):
        if i * i * i * i * i * i <= n:
            count += 1
        else:
            break
    return count

with one single loop.

But you can guess that the result will be close to n 1/6 , which will lead immediately to the result:

def solution(n):
    def i6(i):
        j = i *i * i
        return j * j
    i = int(n ** (1./6))
    if (i == 0): return 1 # should never occur but floating point
                          #  inaccuracy can give WEIRD results
    if i6(i) > n:
        return i - 1  # still floating point inaccuracy
    if i6(i+1) <= n:      # over convervative
        return i + 1
    return i

Only 3 tests whatever the value of n, at least up to 2 48 (mantissa size of a double value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM