不用乘、除、减、平方打印前N个平方数

来源:互联网 发布:手机写歌软件 编辑:程序博客网 时间:2024/05/16 12:09

其实挺简单的一个问题,原题链接:http://www.geeksforgeeks.org/print-squares-first-n-natural-numbers-without-using/

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

0 0
原创粉丝点击