Sei sulla pagina 1di 1

1.

Write a function that inputs a number and prints the multiplication table of that number
In [17]: def times_table(x):

'''This function prints the multiplication table of any real number'''

t = range(1,13)
for i in t:
z = i*x

print('{0} X {1} = {2}'.format(x,i,z))

times_table(6.5)

6.5 X 1 = 6.5
6.5 X 2 = 13.0
6.5 X 3 = 19.5
6.5 X 4 = 26.0
6.5 X 5 = 32.5
6.5 X 6 = 39.0
6.5 X 7 = 45.5
6.5 X 8 = 52.0
6.5 X 9 = 58.5
6.5 X 10 = 65.0
6.5 X 11 = 71.5
6.5 X 12 = 78.0

2. 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 [18]: def prime(x):
'''This function returns primes of a number'''
for i in range(2,x):
if x%i==0:
return False

return True

def twin_prime(x):
'''This function prints twin primes of a number '''
for i in range(2,x):
j = i + 2
if prime(i) and prime(j):
print(i,j)
twin_prime(1000)

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

3. Write a program to find out the prime factors of a number. Example: prime factors of 56 - 2, 2, 2,
7
In [19]: def prime_factors(x):
'''This function prints prime factors of a number'''
l = []
for i in range(2,x):
while x%i==0:
l.append(i)
x//=i
print(l)

prime_factors(56)

[2, 2, 2, 7]

4. A function that converts decimal numbers to binary


In [20]: def dec2bin(x):
'''This function converts a decimal number to a binary'''
if x > 1:
dec2bin(x//2)
print(x%2, end='')

dec2bin(10)

1010

In [21]: 3//10
Out[21]:
0

5. Write a function cubesum() that accepts an integer and returns the sum of the cubes of individual
digits of that number. Use this function to make functions PrintArmstrong() and isArmstrong() to print
Armstrong numbers and to find whether is an Armstrong number.
In [22]: def cubesum(x):
'''This function returns the sum of the cubes of individual digits of a number'''
sum_ = 0
item = x
while item > 0:
sum_ += (item%10)**3

item = item//10
return sum_

def PrintArmstrong(x):
'''This function returns the Armstrong numbers within a range of numbers'''
for i in range(1,x):
if cubesum(i)==i:
print(i,'',end='')

def isArmstrong(x):

'''This function checks if a number is Armstrong'''


if cubesum(x)==x:
print('{} is an Armstrong'.format(x))
else:
print('{} is not an Armstrong'.format(x))

isArmstrong(371)

371 is an Armstrong

6. Write a program to implement these formulae of permutations and combinations. Number of


permutations of n objects taken r at a time: 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 [23]: def fact(x): #factorial
y = 1
for i in range(2,x+1):
y = y*i
return y

print(fact(5))

def nP(x,y): #permutation


return fact(x)/fact(x-y)
print(nP(5,3))

def nC(x,y): #combination


return (fact(x) / (fact(y)
* fact(x - y)))
print(nC(5,3))

120
60.0
10.0

7. Write a function prodDigits() that inputs a number and returns the product of digits of that
number.
In [24]: def proDigits(x):
'''This function returns the product of the digits of a number'''
product = 1
while x > 0:
product = product * (x%10)
x = x//10
return product
proDigits(86)
Out[24]:
48

8. If all digits of a number n are multiplied by each other repeating with the product, the one digit
number obtained at last is called the multiplicative digital 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, MPersistence 3) 341 -> 12->2 (MDR 2, MPersistence 2) Using the function
prodDigits() of previous exercise write functions MDR() and MPersistence() that input a number and
return its multiplicative digital root and multiplicative persistence respectively
In [25]: def MDR(y):
'''This function returns the multiplicative digital root of a number'''

while y != 0:
y = proDigits(y)
if y//10==0:
return y

def MPersistence(y):
'''This function returns the multiplicative persistence of a number'''
pers=0
while y != 0:
y = proDigits(y)
pers += 1
if y//10 ==0:
return pers

MPersistence(86)
Out[25]:
3

9. Write a function sumPdivisors() that finds the sum of proper divisors of a number. Proper divisors
of a number are those numbers by which the number is divisible, except the number itself. For
example proper divisors of 36 are 1, 2, 3, 4, 6, 9, 12, 18
In [26]: def sumPdivisors(x):

'''This function returns the sum of proper divisors of a number'''


sum_ = 0
for i in range(1,x):
if x%i==0:
sum_ = sum_ + i
return(sum_)

print(sumPdivisors(678))

690

10. A number is called perfect if the sum of proper divisors of that 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]: def perfNum(x):
'''This funtion returns perfect numbers in a given range'''
return([i for i in range(1,x) if sumPdivisors(i)==i] )

print(perfNum(29))

[6, 28]

11. 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 divisors
of 220 = 1+2+4+5+10+11+20+22+44+55+110 = 284 Sum of proper divisors of 284 = 1+2+4+71+142
= 220. Write a function to print pairs of amicable numbers in a range
In [28]: def amicable_pair(x):
''' This function returns all the amicable numbers in a range '''
lst = []
for i in range(1,x):
j = sumPdivisors(i)
if sumPdivisors(j) == i and i != j:
lst.append((i,j))
return(lst)

print(amicable_pair(5000))

[(220, 284), (284, 220), (1184, 1210), (1210, 1184), (2620, 2924), (2924, 2620)]

12. Write a program which can filter odd numbers in a list by using filter function
In [29]: def oddFilter(x):
'''This function filters odd numbers in a given range of numbers'''
t = range(1,x)
y = list(filter(lambda i: (i%2!=0),t))
return y
print(oddFilter(23))

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]

13. Write a program which can map() to make a list whose elements are cube of elements in a given
list
In [30]: def cube_item(x):
'''This function returns the a list of cubes of elements in a list'''
t = range(x)
y = list(map(lambda i: i**3, t ))
return y

print(cube_item(45))

[0, 1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744, 3375, 4096, 4913, 5832, 6859, 8000, 9261, 10648, 1216
7, 13824, 15625, 17576, 19683, 21952, 24389, 27000, 29791, 32768, 35937, 39304, 42875, 46656, 50653, 54872, 59319, 64000, 6892
1, 74088, 79507, 85184]

14. Write a program which can map() and filter() to make a list whose elements are cube of even
number in a given list
In [31]: def map_cube_of_evens(x):
'''this function filters the even numbers of a range and maps their cubes'''
t = range(x)
a = list(filter(lambda i: i%2==0, t))
b = list(map(lambda i:i**3,a))
return b

print(map_cube_of_evens(89))

[0, 8, 64, 216, 512, 1000, 1728, 2744, 4096, 5832, 8000, 10648, 13824, 17576, 21952, 27000, 32768, 39304, 46656, 54872, 64000,
74088, 85184, 97336, 110592, 125000, 140608, 157464, 175616, 195112, 216000, 238328, 262144, 287496, 314432, 343000, 373248, 4
05224, 438976, 474552, 512000, 551368, 592704, 636056, 681472]

In [ ]:

Potrebbero piacerti anche