Python
Python
B.Tech
Computer Science & Engineering
# Everything divides 0
if (a == 0):
return b
if (b == 0):
return a
# base case
if (a == b):
return a
# a is greater
if (a > b):
return gcd(a-b, b)
return gcd(a, b-a)
while (1) :
count += 1
# Update root
x = root
return root
# Driver code
if __name__ == "__main__" :
n = 327
l = 0.00001
print(squareRoot(n, l))
Experiment 3 : To write a Python program to find the exponentiation of a
number.
def CalculatePower(N,X):
P=1
P=P*N
return P
N,X=2,3
print(CalculatePower(N,X))
N,X=3,4
print(CalculatePower(N,X))
largest = a[0]
for val in a:
# update it
largest = val
print(largest)
Experiment 5 : To write a Python Program to perform Linear Search.
if arr[index] == target:
return index
return -1
# Example usage:
target = 70
# Function call
if result != -1:
else:
# else returns -1
low = 0
high = len(arr) - 1
mid = 0
if arr[mid] < x:
low = mid + 1
high = mid - 1
else:
return mid
return -1
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
# Function call
result = binary_search(arr, x)
if result != -1:
else:
min_index = ind
min_index = j
size = len(arr)
selectionSort(arr, size)
print(arr)
Experiment 8 : To write a Python Program to perform insertion sort.
def insertionSort(arr):
if n <= 1:
return # If the array has 0 or 1 element, it is already sorted, so
return
for i in range(1, n): # Iterate over the array starting from the second
element
j = i-1
while j >= 0 and key < arr[j]: # Move elements greater than key one
position ahead
j -= 1
insertionSort(arr)
print(arr)
Experiment 9 : To write a Python program to find first n prime numbers.
# Driver code
N = 100;
#check for every number from 1 to N
for i in range(1,N+1):
#check if current number is prime
if(isPrime(i)):
print(i,end=" ")