basic math

144 days ago by krshull

def is_even(n): return n%2 == 0 
       
is_even(2) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{True}
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{True}
is_even(3) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{False}
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{False}
 
       
def is_divisible_by(number, divisor=2): return number%divisor ==0 
       
is_divisible_by(13,7) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{False}
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{False}
def even(n): v=[] for i in range(3,n): if i%2==0: v.append(i) return v 
       
even(3) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\right]
even(10) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[4, 6, 8\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[4, 6, 8\right]
range(3) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[0, 1, 2\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[0, 1, 2\right]
range(3.1) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[0, 1, 2\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[0, 1, 2\right]
for i in range(5): print '%6s %6s %6s'%(i, i^2, i^3) 
       
     0      0      0
     1      1      1
     2      4      8
     3      9     27
     4     16     64
     0      0      0
     1      1      1
     2      4      8
     3      9     27
     4     16     64
x=var('x') solve(x^2+3*x+2,x) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = \left(-2\right), x = \left(-1\right)\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = \left(-2\right), x = \left(-1\right)\right]
b,c=var('b c') solve([x^2+b*x+c==0],x) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = -\frac{1}{2} \, b - \frac{1}{2} \, \sqrt{b^{2} - 4 \, c}, x = -\frac{1}{2} \, b + \frac{1}{2} \, \sqrt{b^{2} - 4 \, c}\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = -\frac{1}{2} \, b - \frac{1}{2} \, \sqrt{b^{2} - 4 \, c}, x = -\frac{1}{2} \, b + \frac{1}{2} \, \sqrt{b^{2} - 4 \, c}\right]
y=var('y') solve([x+y==6,x-y==4],x,y) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[x = 5, y = 1\right]\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[x = 5, y = 1\right]\right]
var('x y p q') eq1=p+q==9 eq2=q*y+p*x==-6 eq3=q*y^2+p*x^2==24 solve({eq1,eq2,eq3,p==1],p,q,x,y) 
       
Traceback (click to the left of this block for traceback)
...
SyntaxError: invalid syntax
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_75.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dmFyKCd4IHkgcCBxJykKZXExPXArcT09OQplcTI9cSp5K3AqeD09LTYKZXEzPXEqeV4yK3AqeF4yPT0yNApzb2x2ZSh7ZXExLGVxMixlcTMscD09MV0scCxxLHgseSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpHG7KOF/___code___.py", line 7
    solve({eq1,eq2,eq3,p==_sage_const_1 ],p,q,x,y)
              ^
SyntaxError: invalid syntax
var('x y p q') 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left(x, y, p, q\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(x, y, p, q\right)
eq1=p+q==9 
       
eq1 
       
\newcommand{\Bold}[1]{\mathbf{#1}}p + q = 9
\newcommand{\Bold}[1]{\mathbf{#1}}p + q = 9
eq2=q*y+p*x==-6 
       
eq2 
       
\newcommand{\Bold}[1]{\mathbf{#1}}p x + q y = \left(-6\right)
\newcommand{\Bold}[1]{\mathbf{#1}}p x + q y = \left(-6\right)
eq3=q*y^2+p*x^2==24 
       
eq3 
       
\newcommand{\Bold}[1]{\mathbf{#1}}p x^{2} + q y^{2} = 24
\newcommand{\Bold}[1]{\mathbf{#1}}p x^{2} + q y^{2} = 24
solve([eq1,eq2,eq3,p==1],p,q,x,y) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[p = 1, q = 8, x = -\frac{4}{3} \, \sqrt{10} - \frac{2}{3}, y = \frac{1}{6} \, \sqrt{2} \sqrt{5} - \frac{2}{3}\right], \left[p = 1, q = 8, x = \frac{4}{3} \, \sqrt{10} - \frac{2}{3}, y = -\frac{1}{6} \, \sqrt{2} \sqrt{5} - \frac{2}{3}\right]\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[p = 1, q = 8, x = -\frac{4}{3} \, \sqrt{10} - \frac{2}{3}, y = \frac{1}{6} \, \sqrt{2} \sqrt{5} - \frac{2}{3}\right], \left[p = 1, q = 8, x = \frac{4}{3} \, \sqrt{10} - \frac{2}{3}, y = -\frac{1}{6} \, \sqrt{2} \sqrt{5} - \frac{2}{3}\right]\right]
solns = solve([eq1,eq2,eq3,p==1],p,q,x,y, solution_dict=True) [[s[p].n(30), s[q].n(30), s[x].n(30), s[y].n(30)] for s in solns] 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[1.0000000, 8.0000000, -4.8830369, -0.13962039\right], \left[1.0000000, 8.0000000, 3.5497035, -1.1937129\right]\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[1.0000000, 8.0000000, -4.8830369, -0.13962039\right], \left[1.0000000, 8.0000000, 3.5497035, -1.1937129\right]\right]
var('theta') 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\theta
\newcommand{\Bold}[1]{\mathbf{#1}}\theta
solve(cos(theta)==sin(theta),theta) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\sin\left(\theta\right) = \cos\left(\theta\right)\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\sin\left(\theta\right) = \cos\left(\theta\right)\right]
find_root(cos(theta)==sin(theta),0,pi/2) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}0.785398163397
\newcommand{\Bold}[1]{\mathbf{#1}}0.785398163397
diff(sin(theta)) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\cos\left(\theta\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\cos\left(\theta\right)
diff(sin(theta^2)) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}2 \, \theta \cos\left(\theta^{2}\right)
\newcommand{\Bold}[1]{\mathbf{#1}}2 \, \theta \cos\left(\theta^{2}\right)
diff(sin(x^2),x,4) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}16 \, x^{4} \sin\left(x^{2}\right) - 48 \, x^{2} \cos\left(x^{2}\right) - 12 \, \sin\left(x^{2}\right)
\newcommand{\Bold}[1]{\mathbf{#1}}16 \, x^{4} \sin\left(x^{2}\right) - 48 \, x^{2} \cos\left(x^{2}\right) - 12 \, \sin\left(x^{2}\right)
f=x^2+17*y^2 f.diff(x),f.diff(y) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left(2 \, x, 34 \, y\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(2 \, x, 34 \, y\right)
integral(x*sin(x^2),x,0,1) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}-\frac{1}{2} \, \cos\left(1\right) + \frac{1}{2}
\newcommand{\Bold}[1]{\mathbf{#1}}-\frac{1}{2} \, \cos\left(1\right) + \frac{1}{2}
var('t') x=function('x',t) DE=diff(x,t)+x-1 desolve(DE,[x,t]) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(c + e^{t}\right)} e^{\left(-t\right)}
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(c + e^{t}\right)} e^{\left(-t\right)}
bessel_I(1,1) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}0.565159103992485
\newcommand{\Bold}[1]{\mathbf{#1}}0.565159103992485
cos(pi) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}-1
\newcommand{\Bold}[1]{\mathbf{#1}}-1