不用乘、除、減、平方打印前N個平方數

其實挺簡單的一個問題,原題鏈接:http://www.geeksforgeeks.org/print-squares-first-n-natural-numbers-without-using/

def PrintFirstNSquareNumbers(n):
	"""
	Print first n numbers without using *, /, -, ** or power
	By the simple fact that 1 + 3 + ... + 2n-1 = (1 + 2n-1)/2 * n = n^2
	"""
	s, x = 0, -1
	for i in range(n):
		print(s)
		x += 2
		s += x

# test case
PrintFirstNSquareNumbers(10)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章