Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 351
Image: ubuntu2004
%md <div align='center'><strong> <font size=5>SAGE WORKSHOP 1</font></strong></div>
################################################## # Example 1. Write a loop to computes powers of 2# ################################################## a = 1 for x in range(10): a = pow(2, x) print("2 to the power %d is %d" % (x, a)) ######################################################################## # Example 2. Write a while loop that prints the integers from 1 to 100 # ######################################################################## print ('printing integers from 1 to 100') a = 1 while a <= 100: print(a) a = a +1 ######################################################################### # Example 3. Write a loop that counts backwards by threes from 12 to -9 # ######################################################################### a = 12 while a >= -9: print (a) a = a-3 ##################################################################### # Example 4. Write a loop that computes the first ten prime numbers # ##################################################################### number = 1 count = 0 while count < 10: if is_prime(number): count = count +1 print ("prime %d" % (number)) number = number + 1;
2 to the power 0 is 1 2 to the power 1 is 2 2 to the power 2 is 4 2 to the power 3 is 8 2 to the power 4 is 16 2 to the power 5 is 32 2 to the power 6 is 64 2 to the power 7 is 128 2 to the power 8 is 256 2 to the power 9 is 512 printing integers from 1 to 100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 12 9 6 3 0 -3 -6 -9 prime 2 prime 3 prime 5 prime 7 prime 11 prime 13 prime 17 prime 19 prime 23 prime 29
################################################################################################### # Example 5. Create a list of all the numbers between 1 and 1000 that are multiples of 3 or/and 5 # ################################################################################################### for i in range(1, 1001): if i%5 ==0 and i%3 ==0: print("multiples of 3 and 5: %d" % (i)) print("----------------------NEXT---------------------") for i in range(1, 1001): if i%5 ==0 or i%3 ==0: print("multiples of 3 or 5: %d" % (i))
################################################################################################# # Example 6. Write a loop that finds all the odd numbers up to 100 that are multiple of 3 or 5 # ################################################################################################# for i in range(3,101): if i % 2 == 2: continue else: divisors_list = divisors(i) if 5 in divisors_list and 3 in divisors_list: print("multiples of 3 and 5: %d" % (i)) if 5 in divisors_list or 3 in divisors_list: print("multiples of 3 or 5: %d" % (i))
multiples of 3 or 5: 3 multiples of 3 or 5: 5 multiples of 3 or 5: 6 multiples of 3 or 5: 9 multiples of 3 or 5: 10 multiples of 3 or 5: 12 multiples of 3 and 5: 15 multiples of 3 or 5: 15 multiples of 3 or 5: 18 multiples of 3 or 5: 20 multiples of 3 or 5: 21 multiples of 3 or 5: 24 multiples of 3 or 5: 25 multiples of 3 or 5: 27 multiples of 3 and 5: 30 multiples of 3 or 5: 30 multiples of 3 or 5: 33 multiples of 3 or 5: 35 multiples of 3 or 5: 36 multiples of 3 or 5: 39 multiples of 3 or 5: 40 multiples of 3 or 5: 42 multiples of 3 and 5: 45 multiples of 3 or 5: 45 multiples of 3 or 5: 48 multiples of 3 or 5: 50 multiples of 3 or 5: 51 multiples of 3 or 5: 54 multiples of 3 or 5: 55 multiples of 3 or 5: 57 multiples of 3 and 5: 60 multiples of 3 or 5: 60 multiples of 3 or 5: 63 multiples of 3 or 5: 65 multiples of 3 or 5: 66 multiples of 3 or 5: 69 multiples of 3 or 5: 70 multiples of 3 or 5: 72 multiples of 3 and 5: 75 multiples of 3 or 5: 75 multiples of 3 or 5: 78 multiples of 3 or 5: 80 multiples of 3 or 5: 81 multiples of 3 or 5: 84 multiples of 3 or 5: 85 multiples of 3 or 5: 87 multiples of 3 and 5: 90 multiples of 3 or 5: 90 multiples of 3 or 5: 93 multiples of 3 or 5: 95 multiples of 3 or 5: 96 multiples of 3 or 5: 99 multiples of 3 or 5: 100