I am working on a assignment where my program shall ask the user about a positive integer n which the prgogram will found the positive integers a and b, satsifying a ^3 + b ^3 = n .
My work and progress thus far is shown below
def ramunajan(n):
list=[]
u = int(n**(1/3)+1)
for a in range (0,u):
b = (n-a**3)**(1/3)
b = round(b)
if a**3+b**3 == n:
list.append((a,b))
return list
while True:
try:
n = input("För vilket positivt heltal n vill du hitta a och b där a^3 + b^3 = n ?\n")
except ValueError or n <= 0:
continue
else:
break
list_1 = ramunajan(int(n))
print (list_1)
(The input text message is in swedish.)
My problem now is that when a user types in, eg 1729, the program gives output accordingly
For what postivie integer n would you like to find a and b, satisfying a^3 + b^3 = n ?
1729
[(1, 12), (9, 10), (10, 9), (12, 1)]
How do I get rid of the reverse duplicates, ie (10, 9) and (12, 1), from the list?
Thanks!
replace u = int(n**(1/3)+1)
with u = int((n**(1/3))/2) + 1
so it checks only the first half of the pool, since the second half are duplicates
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.