标签:nbsp define fine 相加 cal orm efi 验证 form
1.先定义生成器
2.用next(generator)调用生成器,相加,验证
# todo: define my_generater and be called by next() def my_generater(k): i = 1 while True: yield i ** k i += 1 gen_1 = my_generater(1) gen_3 = my_generater(3) # todo: (1 + 2 + 3 + ... + n)^2 = 1^3 + 2^3 + 3^3 ... + n^3 def get_sum(n): sum_1, sum_3 = 0, 0 for i in range(n): i_value_1 = next(gen_1) i_value_3 = next(gen_3) sum_1 += i_value_1 sum_3 += i_value_3 print(‘sum_1 * sum_1 : {}, sum_3: {}‘.format(sum_1 * sum_1, sum_3)) get_sum(8)
todo: 验证(1 + 2 + 3 + ... + n)^2 = 1^3 + 2^3 + 3^3 ... + n^3 巧用next(generator)
标签:nbsp define fine 相加 cal orm efi 验证 form
原文地址:https://www.cnblogs.com/hixiaowei/p/14359948.html