from turtle import * speed(0) for i in range(100): forward(200) left(89) ''' by Ching-Shoei Chiang #1 The sum from 0 to 10 total = 0 for i in range(11): total = total + i print('The sum from 0 to ', i, "is equal to", total) #2 Print the final result only total = 0 for i in range(11): total = total + i print('The sum from 0 to ', 10, "is equal to", total) #3 Function def sum2n(n): total = 0 for i in range(n+1): total = total + i return total for i in [10,20,8,17]: print("The sum from 0 to", i, "is", sum2n(i)) #4 print the prime number from 2 to 100 def prime(n): for i in range(2,n): if n%i==0: return False return True for i in range(2,101): if prime(i): print(i, end='; ') #5 save the result in #4 into a list, tell me how many prime numbers def prime(n): for i in range(2,n): if n%i==0: return False return True L=[] for i in range(2,101): if prime(i): L.append(i) print(L) print(len(L)) #6 Chinese remainder Theorem using for for i in range(4, 100): if i%3==1 and i%4==2 and i%5==4: print(i) #7 Chinese remainder Theorem using while answer = False i = 4 while not answer: if i%3==1 and i%4==2 and i%5==4: print(i) answer = True i = i+1 #8 lcm(m,n) def lcm(m,n): for i in range(m,m*n+1): if i%m==0 and i%n==0: return i print(lcm(12,8)) #9 gcd(m,n) def gcd(m,n): for i in range(m, 1, -1): if m%i==0 and n%i==0: return i print(gcd(12,8)) #10 1000到2000之間,可被3整除,但不被5整除的數有幾個? L=[] for i in range(1000, 2001): if i%3==0 and i%5!=0: L.append(i) print(L) print(len(L)) #Recursive #11 define a function to judge whether n is an even number def evenp(n): if n%2==0: return True else: return False print(evenp(6)) print(evenp(9)) #12 Solve the problem #11 recursively def evenp(n): if n==0: return True if n==1: return False return evenp(n-2) print(evenp(6)) print(evenp(9)) #13 Solve #10 by 輾轉相除法 # if a = b*d + m, then gcd(a,b)=gcd(b,m) def gcd(m,n): if n == 0: return m return gcd(n, m%n) print(gcd(12,8)) print(gcd(7,3)) #4 217 a prime number m = 20 prime=True for i in range(2, m): if m%i==0: prime=False if prime: print(m, 'is a prime') else: print(m, 'is not a prime') '''