0% found this document useful (0 votes)
25 views

SageMath Lecture 8

Uploaded by

Sun Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

SageMath Lecture 8

Uploaded by

Sun Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

SageMath Lecture 8

July 12, 2021

0.1 Introduction to Programming in SageMath


0.1.1 $. Basic Input Output
In [50]: a = float(input('Enter the value of a:'))
b = float(input('Enter the value of b:'))
s=a+b
print("the sum is")
s
Enter the value of a:5
Enter the value of b:6
the sum is

Out[50]: 11.0
In [53]: a = int(input('Enter the value of a:'))
b = int(input('Enter the value of b:'))
s=a+b
print("the sum is")
s
Enter the value of a:5
Enter the value of b:8
the sum is

Out[53]: 13
In [55]: a = float(input('Enter the value of a:'))
b = float(input('Enter the value of b:'))
p=a*b
print("the product is")
p
Enter the value of a:15
Enter the value of b:43
the product is

Out[55]: 645.0

1
0.1.2 $. Generating sequence of numbers
0.1.3 Lists
In [56]: L = [2,3,5,7,11,13,17,19,23,29]

In [57]: len(L)

Out[57]: 10

In [58]: L[0]

Out[58]: 2

In [59]: L[1:5]

Out[59]: [3, 5, 7, 11]

In [60]: L[9]

Out[60]: 29

In [62]: L[-1]

Out[62]: 29

In [63]: L[-3]

Out[63]: 19

In [64]: L.append(32);L

Out[64]: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 32]

In [65]: L[2]=10
L

Out[65]: [2, 3, 10, 7, 11, 13, 17, 19, 23, 29, 32]

In [66]: M=[5,17,26,32,85]

In [67]: L+M

Out[67]: [2, 3, 10, 7, 11, 13, 17, 19, 23, 29, 32, 5, 17, 26, 32, 85]

In [70]: A=[[1,2,3],[4,5,6],[7,8,9]]
A

Out[70]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

In [71]: list(range(10))

Out[71]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

2
In [72]: list(range(1,30,4))
Out[72]: [1, 5, 9, 13, 17, 21, 25, 29]
In [73]: list(srange(1,10,1/2))
Out[73]: [1, 3/2, 2, 5/2, 3, 7/2, 4, 9/2, 5, 11/2, 6, 13/2, 7, 15/2, 8, 17/2, 9, 19/2]
In [6]: list(srange(1,6,0.8))
Out[6]: [1.00000000000000,
1.80000000000000,
2.60000000000000,
3.40000000000000,
4.20000000000000,
5.00000000000000,
5.80000000000000]

0.1.4 Tuples
In [74]: T=(34,56,35,78,98,67)
In [76]: len(T)
Out[76]: 6
In [77]: T[0]
Out[77]: 34
In [78]: T[2:4]
Out[78]: (35, 78)

0.1.5 $. Defining functions


In [31]: f(x)=x*sin(x)-exp(2*x)
In [80]: def f(x):
return sin(x)-exp(2*x)
In [81]: f(2.3)
Out[81]: -98.7386104297571
In [36]: L=[0,1.0,-2.1,3.4,-4.7,5.9,6.0]
In [37]: list(map(f,L)) #Applying f to every member of the list
Out[37]: [-1,
-6.54758511412275,
-0.878204943469351,
-898.102832752444,
0.999840533498544,
-133252.726822196,
-162755.070834502]

3
0.1.6 $. Use of conditionals
0.1.7 If statement
In [25]: x=2021
if(x.is_prime()):
print("the number", x, " is prime!")
else:
print("the number", x, " is NOT prime!")

the number 2021 is NOT prime!

In [24]: x = 2015
if x.is_prime():
print(x, "is prime")
else:
print(x, "=", x.factor())

2015 = 5 * 13 * 31

In [85]: a=3.56
b=3.56
if a>b:
print("a is greater than b")
elif a==b:
print("a is equal to b")
else:
print("a is less than b")

a is equal to b

In [86]: a=8
b=15
c=10
if a>b and a>c:
print("a is greater than b and c")
elif b>c and b>a:
print("b is greater than c and a")
elif c>a and c>b:
print("c is greater than a and b")

b is greater than c and a

0.1.8 Leap year


In [90]: year = int(input("Enter year:"))
if (year%4==0 and year%100 != 0):

4
print(year,"is a leap year")
elif (year%400 == 0):
print(year,"is a leap year")
else:
print(year, "is NOT a leap year")

Enter year:2016
2016 is a leap year

0.1.9 Program to find the greatest of three integers


In [87]: a=float(input("Enter a:"))
b=float(input("Enter b:"))
c=float(input("Enter c:"))
if a>b and a>c:
print("a is greater than b and c")
elif b>c and b>a:
print("b is greater than c and a")
elif c>a and c>b:
print("c is greater than a and b")

Enter a:25
Enter b:46
Enter c:33
b is greater than c and a

0.1.10 Solution of Quadratic equation


In [91]: a = float(input('Enter the value of a:'))
b = float(input('Enter the value of b:'))
c = float(input('Enter the value of c:'))

disc = b**2 - 4*a*c


if(disc>=0):
print("Roots are real")
x1 = (-b+sqrt(disc))/(2*a)
x2 = (-b-sqrt(disc))/(2*a)
print("x1=",x1, "x2 = ",x2)
else:
print("roots are imaginary")

Enter the value of a:5


Enter the value of b:8
Enter the value of c:16
roots are imaginary

5
In [92]: def quadratic(a,b,c):
disc = b**2 - 4*a*c
if(disc>=0):
print("Roots are real")
x1 = (-b+sqrt(disc))/(2*a)
x2 = (-b-sqrt(disc))/(2*a)
print("x1=",x1, "x2 = ",x2)
else:
print("roots are imaginary")

In [93]: quadratic(1,6,9)

Roots are real


x1= -3 x2 = -3

In [95]: x=float(input("Enter x:"))


def f(x):
if x<0:
return x*sin(1/x)
elif x>=0 and x<=1:
return x+exp(-x^2)
else:
return (x^3)*cos(x)
print("for x=",x,"f(x)=",f(x))

Enter x:3.2
for x= 3.2 f(x)= -32.71212321324248

0.1.11 $. For loop and while loop


In [55]: for k in range(5):
s=k^2
print "for k=",k,", s=",s

for k= 0 , s= 0
for k= 1 , s= 1
for k= 2 , s= 4
for k= 3 , s= 9
for k= 4 , s= 16

In [15]: s=0
for i in range (1,5):
s=s+i
s=5*s
print(s)

6
970

In [16]: s=0
for i in range (1,5):
s=s+i
s=5*s
print(s)

50

In [20]: s=0
for x in srange(0.3,1.5,0.1):
s=x+1/x
print("The sum =",s)

The sum = 2.11428571428571

In [22]: def f(x):


return exp(-x^2)
for x in srange(0.0,10.0,1.0):
print("for x=",x," f(x)=",f(x))

for x= 0.000000000000000 f(x)= 1.00000000000000


for x= 1.00000000000000 f(x)= 0.367879441171442
for x= 2.00000000000000 f(x)= 0.0183156388887342
for x= 3.00000000000000 f(x)= 0.000123409804086680
for x= 4.00000000000000 f(x)= 1.12535174719259e-7
for x= 5.00000000000000 f(x)= 1.38879438649640e-11
for x= 6.00000000000000 f(x)= 2.31952283024357e-16
for x= 7.00000000000000 f(x)= 5.24288566336346e-22
for x= 8.00000000000000 f(x)= 1.60381089054864e-28
for x= 9.00000000000000 f(x)= 6.63967719958073e-36

0.1.12 Sum of first n natural numbers 1+2+3+. . . . . . . . . . . . . . . .+n


In [41]: n=int(input("Enter n"))
s=0
for i in range(1,n+1):
s=s+i
print("The sum of first n natural numbers =",s)

Enter n100
The sum of first n natural numbers = 5050

7
In [14]: n=int(input("Enter n"))
s=0
i=1
while i<=n:
s=s+i
i=i+1
print("s=",s)

Enter n20
s= 210

0.1.13 Sum of first n odd natural numbers 1+3+5+. . . . . . . . . . . . . . . . . . . . . . . . ..+(2n-1)


In [11]: n=int(input("Enter n"))
s=0
for i in range(1,n+1,2):
s=s+i
print("The sum of first n natural numbers =",s)

Enter n10
The sum of first n natural numbers = 25

0.1.14 To find the gcd of two integers


In [62]: a=int(input("enter a"))
b=input("Enter b")

r = a%b
while r != 0:
a=b
b=r
r = a%b
print"gcd=",b

enter a35
Enter b95
gcd= 5

0.1.15 Program to find grade agains a given Marks


In [19]: marks=float(input("Enter Marks"))

if marks>=90:
print("Grade = A")
if marks>=80 and marks<90:
print("Grade = B")

8
if marks>=70 and marks<80:
print("Grade = C")
if marks>=60 and marks<70:
print("Grade = D")
if marks>= 0 and marks<60:
print("Grade = F")
if marks <0:
print("error!")

Enter Marks70
Grade = C

0.1.16 Program to find the sum 1+1/2+. . . . . . ..+1/n.


In [42]: n=int(input("Enter n"))
s=0
for i in srange(1,n+1) :
s=s+1.0/i
print("Required sum ={}".format(s))

Enter n10
Required sum =2.92896825396825

0.1.17 Program to find the factorial of a number


In [43]: n=int(input("Enter n"))
f=1
for i in srange(1,n+1) :
f=f*i
print("{}!={}".format(n,f))

Enter n20
20!=2432902008176640000

0.1.18 Program to check prime numbers


In [47]: n=int(input("Enter n"))
if n==1 :
print("{} is not prime".format(n))
if n==2 :
print("{} is prime".format(n))
else :
for i in range(2,n):
if n%i==0 :
print("{} is not prime".format(n))
break

9
elif i>sqrt(n):
print("{} is prime".format(n))
break

Enter n2057
2057 is not prime

0.1.19 Program to find the GCD of two numbers usig Euclidean Algorithm
In [49]: a=int(input("Enter a"))
b=int(input("Enter b"))
if a==0 and b==0 :
print("GCD undefined")
elif a*b==0 :
print("GCD = {}".format(max(abs(a),abs(b))))
else :
r=a%b
while r!= 0 :
a=b
b=r
r=a%b
print("GCD={}".format(b))

Enter a36
Enter b84
GCD=12

0.1.20 Program to find all the primes between two given integers.
In [84]: lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
print("the prime numbers between {}".format(lower),"and {}".format(upper),"are")

for n in range(lower,upper+1):
if n==2 :
print(n)
else :
for i in range(2,n):
if n%i==0 :

break
elif i>sqrt(n):
print(n)
break

10
Enter lower range: 1
Enter upper range: 10
the prime numbers between 1 and 10 are
2
3
5
7

0.1.21 Number of prime integers less than a given integer


In [98]: a = int(input("Enter upper range a: "))
print("the prime numbers between 1 and {}".format(a),"are")

count=1

for n in range(1,a):

if n==2 :
print(n)
else:
for i in range(2,n):
if n%i==0 :

break
elif i>sqrt(n):
print(n)
count=count+1
break

print("number of primes less than {} is".format(a),count)

Enter upper range a: 100


the prime numbers between 1 and 100 are
2
3
5
7
11
13
17
19
23
29
31
37
41

11
43
47
53
59
61
67
71
73
79
83
89
97
number of primes less than 100 is 25

0.1.22 Program to find the next prime to a given integer


In [5]: def NextPrime(n):
while True:
n=n+1
for i in range(2,n):
if n%i==0:
break
else:
return(n)

In [23]: NextPrime(3)

Out[23]: 5

0.2 Statistics
In [116]: data = [1, 2, 3, 4, 5]
n = len(data)
data[4]

Out[116]: 5

0.2.1 Arithmetic Mean


In [138]: data = [78, 43, 6, 50, 47, 94, 37, 70, 66, 32, 1, 34, 93, 30, 99, 82, 22, 74, 18, 40]
n = len(data)
sum=0
for i in range(0,n):
sum=sum+data[i]
mean = sum / n

print("Mean is: " + str(mean))

12
Mean is: 254/5

0.2.2 Geometric Mean


In [130]: data = [9,27,81]
n = len(data)
product=1
for i in range(0,n):
product=product*data[i]
geometric_mean = (product^(1/n)).n()

print("Geometric Mean is: " + str(geometric_mean))

Geometric Mean is: 27.0000000000000

0.2.3 Sorting of data


In [136]: data = [78, 43, 6, 50, 47, 94, 37, 70, 66, 32, 1, 34, 93, 30, 99, 82, 22, 74, 18, 40]
n = len(data)
data.sort()
print(data)

[1, 6, 18, 22, 30, 32, 34, 37, 40, 43, 47, 50, 66, 70, 74, 78, 82, 93, 94, 99]

0.2.4 Median
In [137]: data = [78, 43, 6, 50, 47, 94, 37, 70, 66, 32, 1, 34, 93, 30, 99, 82, 22, 74, 18, 40,
n = len(data)
data.sort()

if n % 2 == 0:
median1 = data[n//2]
median2 = data[n//2 - 1]
median = (median1 + median2)/2
else:
median = data[n//2]
print("Median is: " + str(median))

Median is: 47

0.2.5 Mode
In [122]: import collections
num_list = [21, 13, 19, 13,19,13]
print(num_list)

13
# calculate the frequency of each item
data = collections.Counter(num_list)
data_list = dict(data)

# Print the items with frequency


print(data_list)

# Find the highest frequency


max_value = max(list(data.values()))
mode_val = [num for num, freq in data_list.items() if freq == max_value]
if len(mode_val) == len(num_list):
print("No mode in the list")
else:
print("The Mode of the list is : " + ', '.join(map(str, mode_val)))

[21, 13, 19, 13, 19, 13]


{21: 1, 13: 3, 19: 2}
The Mode of the list is : 13

In [ ]:

14

You might also like