Calculus¶
In [1]:
Copied!
n = int(input( " > " ))
print( n - 1 , n , n + 1)
n = int(input( " > " ))
print( n - 1 , n , n + 1)
--------------------------------------------------------------------------- StdinNotImplementedError Traceback (most recent call last) Cell In[1], line 1 ----> 1 n = int(input( " > " )) 2 print( n - 1 , n , n + 1) File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/ipykernel/kernelbase.py:1281, in Kernel.raw_input(self, prompt) 1279 if not self._allow_stdin: 1280 msg = "raw_input was called, but this frontend does not support input requests." -> 1281 raise StdinNotImplementedError(msg) 1282 return self._input_request( 1283 str(prompt), 1284 self._parent_ident["shell"], 1285 self.get_parent("shell"), 1286 password=False, 1287 ) StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
In [2]:
Copied!
n = int(input( " > " ))
print( 1000 * (n % 8) + 100 * (n % 8) + 10 * (n % 30) + n % (30) )
n = int(input( " > " ))
print( 1000 * (n % 8) + 100 * (n % 8) + 10 * (n % 30) + n % (30) )
--------------------------------------------------------------------------- StdinNotImplementedError Traceback (most recent call last) Cell In[2], line 1 ----> 1 n = int(input( " > " )) 2 print( 1000 * (n % 8) + 100 * (n % 8) + 10 * (n % 30) + n % (30) ) File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/ipykernel/kernelbase.py:1281, in Kernel.raw_input(self, prompt) 1279 if not self._allow_stdin: 1280 msg = "raw_input was called, but this frontend does not support input requests." -> 1281 raise StdinNotImplementedError(msg) 1282 return self._input_request( 1283 str(prompt), 1284 self._parent_ident["shell"], 1285 self.get_parent("shell"), 1286 password=False, 1287 ) StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
In [3]:
Copied!
n = int(input( " > " ))
print( n - 1 , n , n + 1)
n = int(input( " > " ))
print( 1000 * (n % 81) + 100 * (n % 81) + 10 * (n % 30) + n % (30) )
n = int(input( " > " ))
print( n - 1 , n , n + 1)
n = int(input( " > " ))
print( 1000 * (n % 81) + 100 * (n % 81) + 10 * (n % 30) + n % (30) )
--------------------------------------------------------------------------- StdinNotImplementedError Traceback (most recent call last) Cell In[3], line 1 ----> 1 n = int(input( " > " )) 2 print( n - 1 , n , n + 1) 5 n = int(input( " > " )) File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/ipykernel/kernelbase.py:1281, in Kernel.raw_input(self, prompt) 1279 if not self._allow_stdin: 1280 msg = "raw_input was called, but this frontend does not support input requests." -> 1281 raise StdinNotImplementedError(msg) 1282 return self._input_request( 1283 str(prompt), 1284 self._parent_ident["shell"], 1285 self.get_parent("shell"), 1286 password=False, 1287 ) StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
In [4]:
Copied!
for i in range( 1 , 6 ) :
print( ( str(i) + ' ' ) * ( i ))
for i in range( 1 , 6 ) :
print( ( str(i) + ' ' ) * ( i ))
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
In [5]:
Copied!
for i in range(10, 100):
if i % 7 == 0 or i % 9 == 0:
continue
print(i, end=" ")
for i in range(10, 100):
if i % 7 == 0 or i % 9 == 0:
continue
print(i, end=" ")
10 11 12 13 15 16 17 19 20 22 23 24 25 26 29 30 31 32 33 34 37 38 39 40 41 43 44 46 47 48 50 51 52 53 55 57 58 59 60 61 62 64 65 66 67 68 69 71 73 74 75 76 78 79 80 82 83 85 86 87 88 89 92 93 94 95 96 97
In [6]:
Copied!
a = int( input( " a = " ))
b = int( input( " b = " ))
if a != b :
print( " different " )
else :
print( " same " )
a = int( input( " a = " ))
b = int( input( " b = " ))
if a != b :
print( " different " )
else :
print( " same " )
--------------------------------------------------------------------------- StdinNotImplementedError Traceback (most recent call last) Cell In[6], line 1 ----> 1 a = int( input( " a = " )) 2 b = int( input( " b = " )) 3 if a != b : File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/ipykernel/kernelbase.py:1281, in Kernel.raw_input(self, prompt) 1279 if not self._allow_stdin: 1280 msg = "raw_input was called, but this frontend does not support input requests." -> 1281 raise StdinNotImplementedError(msg) 1282 return self._input_request( 1283 str(prompt), 1284 self._parent_ident["shell"], 1285 self.get_parent("shell"), 1286 password=False, 1287 ) StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
In [7]:
Copied!
for i in range( 10 , 100 ) :
if i % 2 == 0 or i % 3 == 0 or i % 5 ==0 : continue
print( i , end =" ")
for i in range( 10 , 100 ) :
if i % 2 == 0 or i % 3 == 0 or i % 5 ==0 : continue
print( i , end =" ")
11 13 17 19 23 29 31 37 41 43 47 49 53 59 61 67 71 73 77 79 83 89 91 97
In [8]:
Copied!
a , b = eval(input( " > "))
ans = 0
for i in range( a , b + 1 ) :
ans += i
print( a, "+" , a + 1 , "+" , "..." , "+" , b , "=" , ans )
a , b = eval(input( " > "))
ans = 0
for i in range( a , b + 1 ) :
ans += i
print( a, "+" , a + 1 , "+" , "..." , "+" , b , "=" , ans )
--------------------------------------------------------------------------- StdinNotImplementedError Traceback (most recent call last) Cell In[8], line 1 ----> 1 a , b = eval(input( " > ")) 2 ans = 0 3 for i in range( a , b + 1 ) : File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/ipykernel/kernelbase.py:1281, in Kernel.raw_input(self, prompt) 1279 if not self._allow_stdin: 1280 msg = "raw_input was called, but this frontend does not support input requests." -> 1281 raise StdinNotImplementedError(msg) 1282 return self._input_request( 1283 str(prompt), 1284 self._parent_ident["shell"], 1285 self.get_parent("shell"), 1286 password=False, 1287 ) StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
In [9]:
Copied!
a , b = eval(input( " > "))
ans = 0
for i in range( a , b + 1 ) :
ans += i
print( a, "+" , a + 1 , "+" , "..." , "+" , b , "=" , ans )
a , b = eval(input( " > "))
ans = 0
for i in range( a , b + 1 ) :
ans += i
print( a, "+" , a + 1 , "+" , "..." , "+" , b , "=" , ans )
--------------------------------------------------------------------------- StdinNotImplementedError Traceback (most recent call last) Cell In[9], line 1 ----> 1 a , b = eval(input( " > ")) 2 ans = 0 3 for i in range( a , b + 1 ) : File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/ipykernel/kernelbase.py:1281, in Kernel.raw_input(self, prompt) 1279 if not self._allow_stdin: 1280 msg = "raw_input was called, but this frontend does not support input requests." -> 1281 raise StdinNotImplementedError(msg) 1282 return self._input_request( 1283 str(prompt), 1284 self._parent_ident["shell"], 1285 self.get_parent("shell"), 1286 password=False, 1287 ) StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
In [10]:
Copied!
n = input( " > " )
print( "*"*( 10 - len(n)) + n )
n = input( " > " )
print( "*"*( 10 - len(n)) + n )
--------------------------------------------------------------------------- StdinNotImplementedError Traceback (most recent call last) Cell In[10], line 1 ----> 1 n = input( " > " ) 2 print( "*"*( 10 - len(n)) + n ) File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/ipykernel/kernelbase.py:1281, in Kernel.raw_input(self, prompt) 1279 if not self._allow_stdin: 1280 msg = "raw_input was called, but this frontend does not support input requests." -> 1281 raise StdinNotImplementedError(msg) 1282 return self._input_request( 1283 str(prompt), 1284 self._parent_ident["shell"], 1285 self.get_parent("shell"), 1286 password=False, 1287 ) StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
In [11]:
Copied!
for i in range(5) :
print( " * " * ( i + 1 ))
for i in range(5) :
print( " * " * ( i + 1 ))
* * * * * * * * * * * * * * *
Practice
In [12]:
Copied!
import pylab
def f(x):
return pylab.cos(x)**2 / pylab.sqrt(pylab.maximum(1, 2*x-1))
a, b, n = 0, 10*pylab.pi, 100
xs = pylab.linspace(a,b,n)
ys = f(xs)
pylab.plot(xs,ys)
pylab.show()
import pylab
def f(x):
return pylab.cos(x)**2 / pylab.sqrt(pylab.maximum(1, 2*x-1))
a, b, n = 0, 10*pylab.pi, 100
xs = pylab.linspace(a,b,n)
ys = f(xs)
pylab.plot(xs,ys)
pylab.show()
In [13]:
Copied!
import pylab, numpy
def factorial(n) :
f = 1
for i in range(2, n+1) :
f *= i
return f
def taylor_poly(n, x) :
s = 0
for k in range(n) :
s = s + ((-1) ** (k) * (x ** (2 * k)) / factorial(2 * k)) + ((-1) ** (k) * (x ** ((2 * k) + 1)) / factorial((2 * k) + 1))
return s
a, b, m = 0 , 3 * numpy.pi, 100
xs = numpy.linspace(a, b, m)
n = 10
for i in range(1, n+1) :
ys = taylor_poly(i, xs)
pylab.plot(xs, ys, label = "P" + str(2 * i - 1))
pylab.plot(xs,numpy.sin(xs) + numpy.cos(xs), label = "sin(x)+cos(x)")
pylab.title("HW1"
"-"
"Taylor polynomilas with different orders for sin(x)+cos(x)")
pylab.legend()
pylab.grid()
pylab.xlabel("X")
pylab.ylabel("Y")
pylab.ylim(-2, 2)
pylab.show()
import pylab, numpy
def factorial(n) :
f = 1
for i in range(2, n+1) :
f *= i
return f
def taylor_poly(n, x) :
s = 0
for k in range(n) :
s = s + ((-1) ** (k) * (x ** (2 * k)) / factorial(2 * k)) + ((-1) ** (k) * (x ** ((2 * k) + 1)) / factorial((2 * k) + 1))
return s
a, b, m = 0 , 3 * numpy.pi, 100
xs = numpy.linspace(a, b, m)
n = 10
for i in range(1, n+1) :
ys = taylor_poly(i, xs)
pylab.plot(xs, ys, label = "P" + str(2 * i - 1))
pylab.plot(xs,numpy.sin(xs) + numpy.cos(xs), label = "sin(x)+cos(x)")
pylab.title("HW1"
"-"
"Taylor polynomilas with different orders for sin(x)+cos(x)")
pylab.legend()
pylab.grid()
pylab.xlabel("X")
pylab.ylabel("Y")
pylab.ylim(-2, 2)
pylab.show()
In [14]:
Copied!
import pylab
a , b , n = -4 , 4 , 500
def g(t):
glist = []
for i in range(0,n):
if t[i]%pylab.pi>0 and t[i]%pylab.pi<pylab.pi/2:
glist.append(1)
else:
glist.append(-1)
return glist
gxs = pylab.linspace(a,b,n)
gys = g(gxs)
pylab.plot(gxs,gys)
pylab.grid()
pylab.show()
def dg(s):
dglist=[]
h = (b-a)/(n-1)
for j in range(0,n):
if abs((g(s+h)[j]-g(s)[j])/h) < 100:
dglist.append((g(s+h)[j]-g(s)[j])/h)
else:
dglist.append(0)
return dglist
dgxs = pylab.linspace(a,b,n)
dgys = dg(dgxs)
pylab.plot(dgxs,dgys)
pylab.show()
def h(u):
return 1.2732*pylab.sin(2*u)+0.4244*pylab.sin(6*u)+0.25465*pylab.sin(10*u)+0.18189*pylab.sin(14*u)+0.14147*pylab.sin(18*u)
hxs = pylab.linspace(a,b,n)
hys = h(hxs)
pylab.plot(hxs,hys)
pylab.show()
def dh(v):
p = (b-a)/(n-1)
return (h(v+p)-h(v))/p
dhxs = pylab.linspace(a,b,n)
dhys = dh(dhxs)
pylab.plot(dhxs,dhys)
pylab.show()
import pylab
a , b , n = -4 , 4 , 500
def g(t):
glist = []
for i in range(0,n):
if t[i]%pylab.pi>0 and t[i]%pylab.pi