I just started programming 1 week ago so please forgive the chaos you are about to see I'm trying to find the first x taxi numbers but with my "nested for loop" the program takes ages to go through all possibilities. Taxi number: if there is a number a^3+b^3 that is equal to c^3+d^3 that sum is a taxinumber. Example 12^3+1^3 == 10^3+9^3 == 1729
For me it's a success if i can find around 20 taxinumber Thanks beforehand for any tips or tricks!
Here is my code:
import math
def main():
numbersOfResultsToFind = getNumberOfTaxisToFind()
foundResults = 0
numberToCheck = 1
while(foundResults < numbersOfResultsToFind):
result = getTaxi(numberToCheck)
if len(result) > 1: #if more then one a+b
foundResults = foundResults + 1
print(numberToCheck, result)
numberToCheck = numberToCheck + 1
def getNumberOfTaxisToFind():
return int(input("How many taxinumbers do you want to find? "))
def getThirdSquareFloored(value):
value = value**(1/3)
value = math.floor(value) #floor value
return value
def getTaxi(numberToCheck):
result = []
upperLimit = getThirdSquareFloored(numberToCheck)
for a in range(1, upperLimit+1):
for b in range(1, upperLimit+1):
aCubed = a**3
bCubed = b**3
sumCub = aCubed + bCubed
if(sumCub == numberToCheck and a < b):
result.append((a, b))
return result
main()