python 问题:找到2种不同的方式将数字1729表示为2个立方体的总和

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 问题:找到2种不同的方式将数字1729表示为2个立方体的总和相关的知识,希望对你有一定的参考价值。

#########################################################################################
#        Problem:                                                                       #
#        find 2 different ways to express the number 1729 as the sum of 2 cubes         #
#        *** link to course @ comments ***                                              #
#########################################################################################


#═════     Find All Numbers Which Are Less Than 1800 ( When Cubed )     ════════════════#

CubedNumbers = {}

i = 1

while i ** 3 < 1800:

	CubedNumbers [ i ] = ( i ** 3 )
	i = i + 1

#═════     Find All Pairs Which Equate To 1729 ( When Added )     ══════════════════════#

AnswersX = []
AnswersY = []

for x in CubedNumbers:
	for y in CubedNumbers:

		if CubedNumbers [ x ] + CubedNumbers [ y ] == 1729 \
		and not y in AnswersX:

			AnswersX.append ( x )
			AnswersY.append ( y )

#═════     Print Results     ═══════════════════════════════════════════════════════════#

for j in range ( 0, len ( AnswersX ) ):
	print ( str ( AnswersX [ j ] ) + "^3 + " + str ( AnswersY [ j ] ) + "^3 = 1729" )

以上是关于python 问题:找到2种不同的方式将数字1729表示为2个立方体的总和的主要内容,如果未能解决你的问题,请参考以下文章