You are on page 1of 1

1.

Write a function that inputs a number and prints the multiplication table

In [8]: def mul_table(n):


'''This function prints the multiplication table a the given number'''
for i in range (1,11):
m=i*n
print("{}x{}={}".format(n,i,m))
n = int(input("Enter a number : "))
print("The multiplication table of {} is : ".format(n))
mul_table(n)

Enter a number : 5
The multiplication table of 5 is :
5x1=5
5x2=10
5x3=15
5x4=20
5x5=25
5x6=30
5x7=35
5x8=40
5x9=45
5x10=50

1. Write a program to print twin primes less than 1000. If two consecutive odd numbers are both prime then they are known as twin
primes.

In [9]: def comp_prm(b):


'''This function gets the prime numbers less than 1000'''
for e in range(2,(b//2)+1):
if b%e==0:
return False
return True
def twin_no(a,b):
'''This function prints the twin primes less than 1000'''
for c in range(a,b):
d=c+2
if (comp_prm(c)&comp_prm(d)):
print("[{},{}]".format(c,d))
print("twin prime less then 1000 are : ")
twin_no(2,1000)

twin prime less then 1000 are :


[3,5]
[5,7]
[11,13]
[17,19]
[29,31]
[41,43]
[59,61]
[71,73]
[101,103]
[107,109]
[137,139]
[149,151]
[179,181]
[191,193]
[197,199]
[227,229]
[239,241]
[269,271]
[281,283]
[311,313]
[347,349]
[419,421]
[431,433]
[461,463]
[521,523]
[569,571]
[599,601]
[617,619]
[641,643]
[659,661]
[809,811]
[821,823]
[827,829]
[857,859]
[881,883]

1. Write a program to find out the prime factors of a number example : prime factors of 56-2,2,2,7

In [10]: n=56
print("Prime factors of {} is : ".format(n),end=" ")
if n<2:
print(int(n))
while(n%2==0):
print(2,end=" ")
n=n/2
for i in range(3,int(n)+1,2):
while(n%i==0):
print(i,end=" ")
n=n/i

Prime factors of 56 is : 2 2 2 7

1. Write a program to implement these formulae of permutations and combinations. Number of permutations of n objects taken r at a
time is : p(n,r)=n!/(n-r)!. Number of combinations of n objects taken r at a time is : c(n,r)=n!/(r!(n-r)!)=p(n,r)/r!

In [11]: def factorial(n):


'''This function gets the factorial of a number'''
return 1 if n==1 else(n*factorial(n-1))
n=15
r=4
print("Number of permutations of n objects taken r at a time : p(n,r) = {}".format(int(factorial(n)/factorial(n
print("Number of combinations of n objects taken r at a time : c(n,r) = {}".format(int(factorial(n)/(factorial(

Number of permutations of n objects taken r at a time : p(n,r) = 32760


Number of combinations of n objects taken r at a time : c(n,r) = 1365

1. Write a function that converts a decimal number to binary number.

In [12]: def deci_to_bin(n):


'''This function converts decimal number to binary'''
if n>0:
x=[]
while n:
r=n%2
x.append(r)
n=n//2
y=x[::-1]
y.insert(0,0)
return(y)
elif(n==0):
return 0
elif(n<0):
m=-n
x=[]
while m:
r=m%2
x.append(r)
m=m//2
y=x[::-1]
y.insert(0,1)
return(y)
n=-10
print(deci_to_bin(n))

[1, 1, 0, 1, 0]

1. Write a function cubesum() that accepts an integer and return the sum of the cubes of individual digits of that number. Use this
function to move function print Armstrong() and isArmstrong() to print Armstrong numbers and to find whether is an Armstrong
number.

In [13]: def cubesum(n):


'''This function returns the sum of cubes of individual digits of the number'''
x=n
sum=0
while x>0:
i=x%10
sum=sum+i**3
x=x//10
return(sum)
n=407
cubesum(n)

407
Out[13]:

In [14]: def PrintArmstrong(n):


'''This function prints Armstrong number within a range'''
for n in range(n):
if n==cubesum(n):
print("{} is an armstrong number".format(n))
n=1000
PrintArmstrong(n)

0 is an armstrong number
1 is an armstrong number
153 is an armstrong number
370 is an armstrong number
371 is an armstrong number
407 is an armstrong number

In [15]: def isArmstrong(n):


'''This function determines whether a number is an Armstrong number or not'''
if n==cubesum(n):
return("Yes, it is an armstrong number")
else:
return("No, it is not an armstrong number")
n=371
isArmstrong(n)

'Yes, it is an armstrong number'


Out[15]:

1. Write a function prodDigits() that inputs a number and returns the product of digits of that number.

In [23]: def ProdDigits(x):


'''Fuction to print the product of the digits'''
y=x
prd=1
while(y>0):
z=y%10
prd=prd*z
y=int(y/10)
return(prd)
x=int(input("Enter the digits : "))
ProdDigits(x)

Enter the digits : 23


6
Out[23]:

1. If all digits of a number n are multiplied by each other repeating with the product, the one digits number obtained at last is called
the Multiplicative digits root of n. The number of times digits need to be multiplied to reach one digit is called the multiplicative
persistance of n. Example: 86 -> 48 -> 32 -> 6 (MDR 6, MPersistance 3)
341 -> 12 -> 2 (MDR 2, MPersistance 2)
Using the function prodDigits() of previous excercise write functions MDR() and MPersistance() that inputs a number and return its
Multiplicative digital root and Multiplicative persistence respectively.

In [24]: def MDR():


'''This function prints the Multiplicative digital root of a number'''
a1=ProdDigits(x)
while(a1>10):
a2=ProdDigits(a1)
a1=a2
return(a2)
x=int(input("Enter the number : "))
print("The Multiplicative digital root of {} is {}".format(x,MDR()))

Enter the number : 34


The Multiplicative digital root of 34 is 2

In [25]: def MPersistence():


'''This function print the Multiplicative persistence of a number '''
a1=ProdDigits(x)
count=1
while(a1>10):
a2=ProdDigits(a1)
a1=a2
count+=1
return(count)
x=int(input("Enter the number : "))
print("The Multiplicative Persistence of {} is {}".format(x,MPersistence()))

Enter the number : 34


The Multiplicative Persistence of 34 is 2

1. Write a function sumPdivisors() that finds the sum of proper divisors of a number. Properdivisors of a number are those numbers by
which the number is divisible, except the numbers itself. For example proper divisors of 36 are 1,2,3,4,6,9,18.

In [26]: def sumPdivisors(n):


'''This fuction finds the sum of proper divisors of a number'''
sum=0
for i in range(1,n):
if (n%i==0):
sum=sum+i
return(sum)
n=9
sumPdivisors(n)

4
Out[26]:

1. A number is called perfect if the sum of proper divisors of the number is equal to the number. For example 28 is perfect number,
since 1+2+4+7+14=28. Write a program to print all the perfect numbers in a given range.

In [27]: for j in range (5,10000):


sum=0
for i in range(1,j):
if (j%i==0):
sum=sum+i
if (sum==j):
print("{} is a perfect number".format(sum))

6 is a perfect number
28 is a perfect number
496 is a perfect number
8128 is a perfect number

1. Two different numbers are called amicable numbers if the sum of the proper divisors of each is equal to the other number. For
example 220 and 284 are amicable numbers. sum of proper divisor of 220 = 1+2+4+5+10+11+20+22+44+55+110=284 sum of
proper divisor of 284 = 1+2+4+71+142=220. write a function to print pairs of amicable numbers in a range.

In [29]: def amicable(n):


'''This functions prints pairs of amicable numbers in a range'''
for i in range(n):
for j in range(n):
if (i!=j):
if (i<j):
if (sumPdivisors(i)==j and sumPdivisors(j)==i):
print("{} and {} are amicable numbers within range {}".format(i,j,n))
n=1220
amicable(n)

220 and 284 are amicable numbers within range 1220


1184 and 1210 are amicable numbers within range 1220

1. Write a program which can filter odd numbers in a list by using filter function.

In [30]: n=[8,5,66,99,45,13,11,9,7,12,98,77,57]
odd_list=list(filter(lambda x:(x%2==1),n))
print(odd_list)

[5, 99, 45, 13, 11, 9, 7, 77, 57]

1. Write a program which can map() to make a list whose elements are cube of elements in a given list

In [31]: n=[8,5,66,99,45,13,11,9,7,12,98,77,57]
cube_of_eles=list(map(lambda x:x**3,n))
print(cube_of_eles)

[512, 125, 287496, 970299, 91125, 2197, 1331, 729, 343, 1728, 941192, 456533, 185193]

1. Write a program which can map() and filter() to make a list whose elements are cube of even number in a given list.

In [32]: n=[8,5,66,99,45,13,11,9,7,12,98,77,57]
even_list=list(filter(lambda x:(x%2==0),n))
cube_of_even=list(map(lambda x:(x**3),even_list))
print(cube_of_even)

[512, 287496, 1728, 941192]

You might also like