Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 141
Image: ubuntu2004
Kernel: Python 3 (system-wide)
def forloop(n): if n>0: sum = 0 for x in range(1,n+1): sum = sum + x return sum else: return 0
forloop(5)
15
def whileloop(n): if n>0: sum=0 x=1 while x<= n: sum = sum+x x=x+1 return sum else: return 0
whileloop(5)
15
whileloop(1)
1
i = 1+.044/12 1000*i**30*(1-i**30)/(1-i)
35324.16724932064
x=1+0.03/12 -140000*x**360*(1-x)/(1-x**360)
-590.2456472212258
140000*x**120-590.25*(1-x**120)/(1-x)
106427.12411496
x=1+0.035/12 -106427*x**240*(1-x)/(1-x**240)
-617.2337290578675
106427*x**60-617.23*(1-x**60)/(1-x)
86340.82364340735
x=1+0.04/12 -86340.82*x**180*(1-x)/(1-x**180)
-638.6526204120379
86340.82*x**60-638.65*(1-x)/(1-x**60)/(1-x )
110617.45808025483
x=1+0.045/12 -110617.45*x**120*(1-x)/(1-x**120)
-1146.4216498758651
110617.45*x**120-1146*(1-x**120)/(1-x)
63.75264899616013
(1+r/12)**22-86.63*(1-(1+r/12)**22)/(1-(r/12))
x=1+0.16/12 -3000*x**24*(1-x)/(1-x**24)
-146.8893315528303
3000*x**6-146.89*(1-x**6)/(1-x)
2336.89812581878
-2336.90*x**18*(1-x)/(1-x**18)
-146.88918870582367
146.89*x**18-146.89*(1-x**18)/(1-x)
-2779.6529593304626
146.89*18
2644.0199999999995
x=1+0.03/12 -140000*x**360*(1-x)/(1-x**360)
-590.2456472212258
[cube**3 for cube in range(0,11)if cube % 2==0]
[0, 8, 64, 216, 512, 1000]
import numpy as np
x = np.array([0,2,4,6,8,10])
x**3
array([ 0, 8, 64, 216, 512, 1000])
i=[] def forloop(n): for i in (n): if length(n) == 1: return i else: return n[2]
#forloop([7,8,9])
def rec_max(L): if len(L) == 1: return L[0] else: m = rec_max(L[1:]) if L[0] > m: return L[0] else: return m
rec_max([5,6,7,3,9,3,4])
9
def rec_in(n): if len(n)==1: return n[0] else: rest= rec_in(n[1:]) r=rest+10 y=n[:-1] return y
rec_in([2,3,5])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-65-1bf3671e8b60> in <module> ----> 1 rec_in([2,3,5]) <ipython-input-64-eeecef688398> in rec_in(n) 6 7 rest= rec_in(n[1:]) ----> 8 r=rest+10 9 y=n[:-1] 10 return y TypeError: can only concatenate list (not "int") to list
def list(j): if len(j)==0: return j[0] else: k=len(j) while k>1: k=len(j)-1 fg=10+j[-1] return list(j[:-1])
def list(j): if len(j)==1: return j[0] else: return list(j[:-1])*10+j[-1]
list([6,6,7,3])
6673
def rec_in(x): if len(x)==1: return x[0] else: n=len(x[:-1]) return x[0]*10**n+rec_in(x[1:])
rec_in([7,1,5])
715
p(n)=c*p(n)-1 p(0)=1 c>0
c=.4 def p(n): if n == 0: return 1 else: return c*p(n-1)

the p(n) sequences converges when c<1

p(0)
1
p(1)
0.4
p(2)
0.16000000000000003
p(3)
0.06400000000000002
p(4)
0.025600000000000008
p(0)= c*1=1 c=1 p(1)=1*1=1 p(2)=1*1=1 p(3)=1*1=1
def sign(x): if x<0: return-1 else: return 1
sign(5)
1
def psum(n): a=0 for v in range(1,n+1): a=a+v**2 return a
psum(5)
55

Plot f(x) = x^3-x+1 on the interval [-2,2]

import numpy as np import matplotlib.pyplot as plt
x = np.linspace(-2,2,100) y = x**3+1 plt.xlabel('x') plt.ylabel('y')
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-c97b7dc2d9fb> in <module> ----> 1 x = np.linspace(-2,2,100) 2 y = x**3+1 3 plt.xlabel('x') 4 plt.ylabel('y') NameError: name 'np' is not defined
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x7f1efb9d3df0>]
Image in a Jupyter notebook
def rec_in(n): if len(n)==1: return n[0] else: rest= rec_in(n[1:]) r=rest+10 y=n[:-1] return y
rec_max([5,6,7,3,9,3,4])
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-8-84bca8af7d8c> in <module> ----> 1 rec_max([5,6,7,3,9,3,4]) NameError: name 'rec_max' is not defined
def quad_tan(a,b,c): x = [-10+i/1000*20 for i in range(1001)] yquad = [a*n*n+b*n+c for n in x] ytan = [b*n+c for n in x] plt.plot(x,yquad,color='blue') plt.plot(x,ytan,color='red') plt.show()
def has_3(arr): for n in arr: if n == 3: return 1 return -1
has_3([1,2,3,3,4])
1
20000*(1+0.082/12)**(48)/((1-(1+0.082/12)**(48))/(-0.082/12))
490.1382323808438
quad_tan(3,2,1)
Image in a Jupyter notebook
def line_intersect(m1,b1,m2,b2): x = [-10+i/1000*20 for i in range(1001)] yline1 = [m1*n+b1 for n in x] yline2 = [m2*n+b2 for n in x] X = (b2-b1)/(m1-m2) Y = m1*X+b1 plt.plot(x,yline1,color='blue') plt.plot(x,yline2,color='red') plt.scatter(X,Y,color='black',s=40) plt.show()
line_intersect(3,2,-3,4)
Image in a Jupyter notebook
def min_cost(n): if n*10.99 > 100: print(100) else: print(n*10.99)
min_cost(9)
98.91
for n in range(501): for m in range(501): if n*n-7*m*m == 1: print(n)
1 8 127
for n in range(501):
[odd for odd in range(0,51)if odd%2==1]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
t=3*a*x**(3-1)+2*b*x**(2-1)+1*c*x+d(0)
cube_deriv(3,-2,0,5)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-8-1a2e8871728c> in <module> ----> 1 cube_deriv(3,-2,0,5) <ipython-input-7-56e8ca8586bd> in cube_deriv(a, b, c, d) 1 def cube_deriv(a,b,c,d): ----> 2 y = a*x**3+b*x**2+c*x+d 3 t=3*a*x**(3-1)+2*b*x**(2-1)+1*c*x+d(0) 4 prin(t) NameError: name 'x' is not defined
import numpy as np
np.random.randint(0,2,size=100)
array([0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0])
import numpy as np import matplotlib .pyplot as plt
def bisection(a,b,c,e): c = (a+b)/2 while c-a > e: if f(c)==0: return c elif f(a)*f(c)>0: a=c else: b=c x= (a+b)/2 return c
zero=heads one=tails
coin_f=np.random.randint(0,2,size=10)
coin_f
array([0, 1, 1, 0, 1, 0, 0, 1, 0, 1])
def filp(n): for i in range(100): coin_f=np.random.randint(0,2,size=n) list=[] f_st=0 for coin in coin_f: if coin ==1: f_st +=1 else: if f_st>=4: list.append(f_st) f_st =0 return list
filp(10)
[]
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-42-d85ea1407ed1> in <module> ----> 1 np.mean([filp(10) for i in range(100)]) <__array_function__ internals> in mean(*args, **kwargs) /usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py in mean(a, axis, dtype, out, keepdims) 3370 return mean(axis=axis, dtype=dtype, out=out, **kwargs) 3371 -> 3372 return _methods._mean(a, axis=axis, dtype=dtype, 3373 out=out, **kwargs) 3374 /usr/local/lib/python3.8/dist-packages/numpy/core/_methods.py in _mean(a, axis, dtype, out, keepdims) 170 ret = ret.dtype.type(ret / rcount) 171 else: --> 172 ret = ret / rcount 173 174 return ret TypeError: unsupported operand type(s) for /: 'list' and 'int'
for i in range(100): coin_f=np.random.randint(0,2,size=10) list=[] f_st=0 for coin in range(100): if coin ==1: f_st +=1 else: if f_st==4: list.append(f_st) f_st=0
list
[]
def flip(n): list=[] for i in range(n): coin_f=np.random.randint(0,2,size=10) return list.append(coin_f)
flip(100)
coin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] [1 1 0 1 0 0 0 1 0 1] [0 0 0 1 1 0 0 1 0 0] [1 1 0 0 1 1 1 0 0 0] [1 1 1 0 1 0 0 1 0 0] [0 1 1 0 0 1 0 0 0 1]
def dyn_fib(n): fib_list =[0 for i in range(n+1)] fib_list[0]=0 fib_list[1]=1 for i in range(2,n+1): fib_list[i]=fib_list[i-1]+fib_list[i-2] return fib_list[-1]
dyn_fib(4)
3

The lightbulb problem Suppose that a company sells lightbulbs in packages of size 1, 2, 6, and 8. If a customer orders 30 bulbs, what is the least number of packages (of any size) that can be used to put this order together?

def bulb(n): L = [0 for i in range(n)] L[0] = 1 # 1 bulb L[1] = 1 # 2 bulbs L[2] = 2 # 3 bulbs L[3] = 2 # 4 bulbs L[4] = 3 # 5 bulbs L[5] = 1 # 6 bulbs L[6] = 2 # 7 bulbs L[7] = 1 # 8 bulbs for i in range(8,n): L[i] = 1 + min(L[i-1],L[i-2],L[i-6],L[i-8]) print(L) return L[n-1]
bulb(30)
[1, 1, 2, 2, 3, 1, 2, 1, 2, 2, 3, 2, 3, 2, 3, 2, 3, 3, 4, 3, 4, 3, 4, 3, 4, 4, 5, 4, 5, 4]
4

Suppose that a company sells computer chips in packages of size 1, 5, and 8. Find the least number of packages that can be used to order 20 chips.

def chips(n): L = [0 for i in range(n)] L[0] = 1 # 1 chip L[1] = 2 # 2 chips L[2] = 3 # 3 chips L[3] = 4 # 4 chips L[4] = 1 # 5 chips L[5] = 2 # 6 chips L[6] = 3 # 7 chips L[7] = 1 # 8 chips for i in range(8,n): L[i] = 1 + min(L[i-1],L[i-5],L[i-8]) print(L) return L[n-1]
chips(20)
[1, 2, 3, 4, 1, 2, 3, 1, 2, 2, 3, 4, 2, 3, 3, 2, 3, 3, 4, 4]
4
j=[60] a=[140] for i in range(5): j_n=j[-1] a_n=a[-1] j_time=0.05*j_n+0.65*a_n-52 a_time=0.45*j_n+0.69*a_n+72 j.append(j_time) a.append(a_time)
import numpy as np import random
listt=[] stat=0 def spin(): if total < 100 and total >85: return 1 else: return 0 #print(num1,num2,total) for i in range(100): num1= random.randrange(0,101,5) num2= random.randrange (0,101,5) total=num1+num2 listt.append(spin()) for u in listt: stat=stat+u length=len(listt) print(listt,stat,length) print(stat/length)
[1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 7 100 0.07