Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

FINAL

Views: 87
#2 def attitude(): i=randint(1,10) if i<=5: print "Dont look at me" else: print "Are you looking?"
attitude()
Are you looking?
#3 L=[] for i in [1..300]: L.append(randint(1,100))
L
[74, 20, 27, 9, 52, 35, 82, 50, 45, 38, 90, 70, 83, 87, 55, 96, 5, 90, 32, 46, 5, 66, 86, 61, 95, 63, 34, 74, 77, 32, 53, 77, 35, 46, 71, 11, 92, 75, 48, 65, 2, 75, 4, 42, 18, 85, 14, 85, 44, 64, 5, 52, 41, 66, 53, 70, 9, 67, 60, 100, 40, 46, 26, 27, 38, 78, 83, 29, 98, 26, 12, 66, 77, 48, 11, 44, 51, 81, 26, 3, 46, 89, 38, 100, 26, 36, 76, 24, 24, 17, 11, 36, 76, 15, 56, 45, 37, 49, 79, 1, 34, 78, 74, 79, 94, 2, 29, 51, 77, 25, 69, 18, 19, 92, 75, 13, 80, 32, 29, 54, 21, 2, 39, 31, 18, 58, 39, 74, 67, 48, 70, 39, 70, 72, 49, 18, 10, 64, 90, 34, 68, 40, 48, 88, 97, 53, 72, 17, 4, 69, 97, 33, 53, 96, 17, 76, 40, 86, 71, 92, 40, 21, 62, 75, 94, 94, 17, 79, 16, 46, 68, 90, 36, 89, 3, 60, 52, 45, 57, 81, 39, 35, 96, 35, 73, 26, 82, 79, 60, 84, 59, 78, 9, 100, 4, 88, 60, 36, 46, 3, 88, 66, 10, 13, 87, 30, 10, 68, 56, 8, 46, 93, 30, 10, 74, 14, 5, 47, 34, 80, 64, 3, 85, 48, 91, 61, 15, 85, 50, 84, 3, 42, 52, 16, 54, 79, 20, 64, 82, 23, 96, 97, 50, 69, 97, 86, 18, 61, 50, 84, 45, 4, 48, 69, 89, 17, 94, 89, 30, 32, 71, 11, 95, 68, 28, 80, 5, 61, 4, 22, 50, 3, 50, 77, 4, 15, 46, 75, 18, 40, 45, 87, 46, 58, 85, 83, 37, 37, 34, 31, 40, 96, 100, 4, 6, 94, 79, 33, 1, 88]
#4 def apples(a,b): #a=randint(1,10) #b=randint(1,10) print "I picked {} apples and {} oranges".format(a,b)
apples(5,8)
I picked 5 apples and 8 oranges
#5 def prime_sum(n): prime = [True] * (n + 1) p = 2 while p * p <= n: if prime[p] == True: i = p * 2 while i <= n: prime[i] = False i += p p += 1 sum = 0 for i in range (2, n + 1): if(prime[i]): sum += i return sum
prime_sum(4)
5
#8 def sum_digits(n): num_str = str(n) sum = 0 for i in range(0, len(num_str)): sum += int(num_str[i]) return sum sum_digits(939)
21
#6 def constant_matrix(n,m,c): L =[c]*(n*m) M=matrix(n,m,L) return M constant_matrix(3,3,100)
[100 100 100] [100 100 100] [100 100 100]
#7 M=matrix(3,3,[0,0,5,0,0,0,0,0,0]) M
[0 0 5] [0 0 0] [0 0 0]
#9 def triples(l): c=0 m=2 while(c<l): for n in range(1,m+1): a=m*m-n*n b=2*m*n c=m*m+n*n if(c>l): break if(a==0 or b==0 or c==0): break print(a,b,c) m=m+1 triples(10)
(3, 4, 5) (8, 6, 10)
#9 ReDo for a in [1..10]: for b in [1..10]: for c in [1..10]: if a^2 + b^2 == c^2: print (a,b,c)
(3, 4, 5) (4, 3, 5) (6, 8, 10) (8, 6, 10)
#10 occurances def a_count(str): return str.count('a') a_count("Mary had a little lamb")
4
#11 for i in [1..100]: x=str(randint(1,100)) afile = open("rand_store.txt", "a" ) print (af.read()) afile.write(x+'\n') afile.close()
53 90 23 64 91 77 14 61 58 50 79 74 23 57 38 29 53 9 3 3 7 20 15 32 29 49 7 66 10 5 65 92 60 60 88 65 45 97 13 42 80 21 91 50 7 9 100 37 48 55 74 77 42 14 47 79 25 84 94 37 63 96 41 39 55 18 95 22 74 15 43 86 21 12 84 97 45 79 70 27 100 49 12 83 1 45 61 55 43 82 10 84 84 82 97 27 41 7 67 93
#12 - dont think will be asked def read_data(file): L=[] x = open("%s"%file, "r") for i in x: w="" for j in range(len(i)): if i[j]!="\"" and i[j]!='n': w += i[j] L.append(int(w)) return L read_data(L)
Error in lines 11-11 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute flags=compile_flags) in namespace, locals File "", line 1, in <module> NameError: name 'L' is not defined
#13 Map anonymous to get cubes L = [3,7,2,5] final = map(lambda x: (x**3) , L) print(final)
[27, 343, 8, 125]
#14 Recursive Fin def fib(n): if n == 0: return 0 if n == 1: return 1 return fib(n-2)+fib(n-1) for i in [0..10]: print i, fib(i) #14 Continues def rawr(n): if n == 1: return 3 if n == 2: return 4 if n == 3: return 5 return rawr(n-1)+rawr(n-2)+rawr(n-3) rawr(10)
0 0 1 1 2 1 3 2 4 3 5 5 6 8 7 13 8 21 9 34 10 55 440
#15 total_sum = set(list(range(0, 100, 3)) + list(range(0, 100, 5))) print total_sum
set([0, 3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42, 45, 48, 50, 51, 54, 55, 57, 60, 63, 65, 66, 69, 70, 72, 75, 78, 80, 81, 84, 85, 87, 90, 93, 95, 96, 99])
#16 def random_average(n): num = randint(1,100) total_sum = 0 for n in range(num): while (num >= 0): total_sum += num num-=1 avg = total_sum/num return num, total_sum, avg random_average(10)
(-1, 2556, -2556)
#17 - skipping scatter plot b = random_average(100) scatter_plot([(1,x) for x in b])
#18 n = graphs.RandomGNP(10,.5) def product(n): V=max(n.vertices()) Z=n.size() return V*Z V Z product(n)
9 26 198
#19 total_sum = 0 for i in [1..1000]: if (i % 3 == 0 or i % 5 == 0): total_sum = total_sum + i print total_sum
234168
#20 twin primes def twinPrime(n): for i in [1..n]: if is_prime(i) and is_prime(i+2): print (i,(i+2)) twinPrime(302)
(3, 5) (5, 7) (11, 13) (17, 19) (29, 31) (41, 43) (59, 61) (71, 73) (101, 103) (107, 109) (137, 139) (149, 151) (179, 181) (191, 193) (197, 199) (227, 229) (239, 241) (269, 271) (281, 283)
#21 Fib def fib(n): F=[0,1] for i in [2..n]: F.append(F[i-1]+F[i-2]) return F[n] fib(1000)
43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875
#21 Fib revist def fib(n): F=[0,1] for i in [2..n]: F.append(F[i-1]+F[i-2]) return F[n] fib_sum = 0 current = 1 test = 0 while test < 400000: test = fib(current) current = current + 1 if test%2 ==0: fib_sum=fib_sum +test print fib_sum # def solution(n): return ceil(4.78497 * n - 3.1127) solution(1000)
257114 4782
#22 Factors def print_factors(x): print("The factors of",x,"are:") for i in range(1, x + 1): if x % i == 0: print(i) num=320 print_factors(num) # other solution max(factor(5000)) def largest_factor(n): return max(factor(n))[0] largest_factor(5000)
('The factors of', 320, 'are:') 1 2 4 5 8 10 16 20 32 40 64 80 160 320 (5, 4) 5
#23 Ramanujan def ramanujuan(n): for a in [1..n]: a2 = a*a if a2 >= n: break for b in range(a, n): b2 = b*b if a2+b2 >= n: break for c in range(a+1, n): c2 = c*c if c2 >= a2 + b2: break for d in range(c,n): d2 = d*d cd = c2+d2 ab = a2+b2 if cd > ab: break if cd == ab: print(a,"^2 + ",b,"^2 = ",c,"^2 +",d,"^2") return ramanujuan(1000)
(1, '^2 + ', 7, '^2 = ', 5, '^2 +', 5, '^2')
#24 100! def factorial_sum(n): print(sum(map(int, str(math.factorial(n))))) factorial_sum(100)
648
#25 def get_digit(n): return int(float(e)*(10**n))%10 get_digit(5)
8