Op First
Op First
Op First
Write a function that inputs a number and prints the multiplication table
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.
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!
[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.
407
Out[13]:
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
1. Write a function prodDigits() that inputs a number and returns the product of digits of that number.
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.
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.
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.
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.
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)
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)