Python在学习

来源:互联网 发布:佳明飞耐时3软件 编辑:程序博客网 时间:2024/06/04 18:11

Question 1

Convert the following English description into code.Initialize n to be 1000. Initialize numbers to be a list of numbers from 2 to n, but not including n.With results starting as the empty list, repeat the following as long as numbers contains any numbers.Add the first number in numbers to the end of results.Remove every number in numbers that is evenly divisible by (has no remainder when divided by) the number that you had just added to results.How long is results?To test your code, when n is instead 100, the length of results is 25.

def list_co(n):    results = []    numbers = range(2, n, 1)    while len(numbers) != 0:        results.append(numbers[0])        j = len(numbers)        while 1:            if numbers[j-1] % results[-1] == 0:                del numbers[j-1]            j -= 1            if j <= 0:                break;    return len(results)            print "when n is instead 100, the length of results is ", list_co(100), " ."print "when n is instead 1000, the length of results is ", list_co(1000), " ."


0 0
原创粉丝点击