<jupyter_start><jupyter_text>第12讲 找出所有能整除120的整数  Assignments 作业 1. create an empty list with the variable name `my_friends`, use `append` method to add at least 5 of your friend names to this list, then use the method `remove` to remove one or several of your friend names from the list. Each time after you add or remove a friend name, print out the whole `my_friends` list and its length.     创建一个空的列表，变量名设为`my_friends`，使用`append`方法往这个列表里添加至少5个你的朋友名字，随后再使用`remove`方法从中移除一个或多个朋友名字。每一次当你使用`append`或`remove`方法后，都将整个`my_friends`列表的内容以及列表的长度打印出来。<jupyter_code>my_friends = []
print(my_friends)
my_friends.append("<NAME>")
print(my_friends)
my_friends.append("<NAME>")
print(my_friends)
my_friends.append("<NAME>")
print(my_friends)
my_friends.append("<NAME>")
print(my_friends)
my_friends.append("<NAME>")
print(my_friends)
my_friends.remove("<NAME>")
print(my_friends)<jupyter_output>[]
['<NAME>']
['<NAME>', '<NAME>']
['<NAME>', '<NAME>', '<NAME>']
['<NAME>', '<NAME>', '<NAME>', '<NAME>']
['<NAME>', '<NAME>', '<NAME>', '<NAME>', '<NAME>']
['<NAME>', 'Song Kang', '<NAME>', '<NAME>']<jupyter_text>2. Write a program to find all factors of 144 and 385, put their factors in to two lists, one is for 144, and the other is for 385. Print out the two lists and also tell which integer has more factors.     编写程序分别查找整数144和385各自的因数，把他们分别放到两个不同的列表中，编程输出这两个列表并输出谁的因数数量多<jupyter_code>num1, num2 = 144, 385
factors1, factors2 = [], []

## begin added codes
i = 1
max_num = num1 if num1 >= num2 else num2
while i <= max_num:
    if num1 % i == 0:
        factors1.append(i)
    if num2 % i == 0:
        factors2.append(i)
    i += 1
## end added codes

print("{} 的因数是 {}, 个数是 {}".format(num1, factors1, len(factors1)))
print("{} 的因数是 {}, 个数是 {}".format(num2, factors2, len(factors2)))
if len(factors1) > len(factors2):
    print("{} 的因数个数多于{}的因数个数".format(num1, num2))
else:
    print("{} 的因数个数少于或等于{}的因数个数".format(num1, num2))<jupyter_output>144 的因数是 [1, 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 36, 48, 72, 144], 个数是 15
385 的因数是 [1, 5, 7, 11, 35, 55, 77, 385], 个数是 8
144 的因数个数多于385的因数个数<jupyter_text>3. Following the previous question. After you have two lists for the factors of 144 and 385, Write a program to check if these two integers shared some factors, If yes, print out the shared factor(s).      续前一题。 当你找到144和385的所有因数后，编写程序检查这两个数除了整数1以外还有没有共同的因数<jupyter_code>factors_for_144 = [1, 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 36, 48, 72, 144]
factors_for_385 = [1, 5, 7, 11, 35, 55, 77, 385]

factors_shared = []
i = 0
while i < len(factors_for_144):
    factor_of_144 = factors_for_144[i]
    if factor_of_144 in factors_for_385 and factor_of_144 > 1:
        factors_shared.append(factor_of_144)
        
    i += 1
    
print("{} shared factors: {}".format(len(factors_shared), factors_shared))<jupyter_output>0 shared factors: []<jupyter_text>4. if an integer only have two factors(1 and the integer itself), this integer is a Prime number; Otherwise, it is a Composite number. Write a program to find all the prime and composite numbers between 2 and 100 (both included). Use two lists to store all these prime and composite numbers. Finally, print out the two lists.      如果一个整数的因数个数为2个（整数1和它自己），那么这个整数为“质数”，否则这个数则为合数。编写程序找出从2-100这100个连续整数中哪些是质数，哪些是合数，分别用两个列表来存储质数和合数，并最后将其打印出来。<jupyter_code>num_start, num_end = 2, 100
list_prime = []
list_composite = []

num = num_start
while num <= num_end:
    factors_for_num = []
    i = 1
    while i <= num:
        if num % i == 0:
            factors_for_num.append(i)
            
        i += 1
    
    if len(factors_for_num) == 2:
        list_prime.append(num)
    else:
        list_composite.append(num)
        
    num += 1

print("Prime numbers: {}".format(list_prime))
print("Composite numbers: {}".format(list_composite))<jupyter_output>Prime numbers: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Composite numbers: [4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99, 100]<jupyter_text>5. The following list `list_a` has 2 elements, both of which are integer `1`. Please write a program to add 20 more new elements to the end of `list_a` step by step while respecting the following rule: **every time when you add a new element to the end of the list, its value is the sum of the values of the last two elements in the list.** Print out the final list when you finish adding elements. The final list you get should have 22 elements with `17711` as its last element. Are your familiar with the first 10 element in this list?   下面名为`list_a`的列表一开始有2个元素且值均为`1`。请编写程序往这个列表的末端逐次再添加20个新元素，**要求每次新添加的元素值是当时列表的最末两位元素的值的和。** 添加完毕后打印输出这个列表。你最后得到的列表应该具有22个元素，且最后一个元素的值为`17711`。你熟悉这个列表的前10个元素吗？<jupyter_code>list_a = [1, 1]
n_element_added = 0
n_element_required = 20

while n_element_added < n_element_required:
    element = list_a[-1] + list_a[-2]
    list_a.append(element)
    n_element_added += 1

if len(list_a) != 22 or list_a[-1] != 17711:
    print("your answer is not correct")
else:
    print("list a: {}".format(list_a))<jupyter_output>list a: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711]
{'fraction_non_alphanumeric': 0.13656633221850614, 'fraction_numerical': 0.09513192121887774, 'mean_word_length': 4.481670061099797, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '615119', 'license': 'MIT', 'n_tokens_mistral': 2684, 'n_tokens_neox': 2312, 'n_words': 671, 'path': 'source/2021/500Answer/content/012A_factor_composite_prime_with_answer.ipynb', 'repo_name': 'qqiang00/ElementaryMathPython', 'size': '5454'}