Assignment 1 Solutions - PDF
Assignment 1 Solutions - PDF
Q1. Write a function that inputs a number and prints the multiplication table of that number.
In [ ]:
a = int(input("enter the number here:"))
mul_table = []
for i in range(1,11):
p = a*i
mul_table.append(p)
if i == 10:
print(mul_table)
In [ ]:
# https://www.codespeedy.com/write-a-python-program-to-print-all-twin-primes-less-th
def prime_1000(num):
f = 0
for i in range(1,num+1):
if num%i == 0:
f = f+1
if f == 2:
return True
num =2
while num < 1000:
if prime_1000(num) == True and prime_1000(num+2) == True:
print("({},{})".format(num,num+2), end= ' ')
num += 1
(3,5) (5,7) (11,13) (17,19) (29,31) (41,43) (59,61) (71,73) (101,103) (107,109) (13
7,139) (149,151) (179,181) (191,193) (197,199) (227,229) (239,241) (269,271) (281,28
3) (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)
Q3. write a program to find out the prime factors of a number. example: prime factors of 56 -
2,2,2,7.
In [ ]:
def prime_fs(m):
fs_list = []
a = 2
while m >= a:
if m%a == 0:
fs_list.append(a)
m = m/a
else:
a = a + 1
return fs_list
print(prime_fs(56))
[2, 2, 2, 7]
Q4. write a program to implement these formulae of permutations and combinations.
In [ ]:
# program for permutation
def perm(n,r):
if r == 0:
return [[]]
L = []
for i in range(len(n)):
rem = [ a for a in n if n[i] != a]
file:///C:/Users/HP/OneDrive/Applied ai/Module 1/assignment/assignment 1 solutions.html 1/6
9/14/22, 10:59 PM assignment 1 solutions
m = perm(rem, r-1)
for j in m:
L.append([n[i]] + j)
return L
n = [1,2,3,4]
r = 2
print(perm(n,r))
[[1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4,
2], [4, 3]]
In [ ]:
#program for combination
def comb(n,r):
if r == 0:
return [[]]
L = []
for i in range(len(n)):
e1 = n[i]
rem = n[i+1:]
m = comb(rem,r-1)
for j in m:
L.append([e1] + j)
return L
n = [1,2,3,4]
r = 2
print(comb(n,r))
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
Q5. write a function that converts a decimal number to binary number.
In [ ]:
def dtob(d):
r = ''
while d > 1:
a = d%2
d = d//2
r = r + str(a)
return r
res = dtob(57898)
print(res)
010101000100011
Q6. Write a function cubesum() that accepts an integer and returns the sum of cubes of
individual digits of that number. use this function to make functions PrintArmstrong() and
isArmstrong() to print Armstrong numbers and to find wether is an Armstrong number.
In [ ]:
def cubesum(n):
a = str(n)
sum = 0
for i in range(len(a)):
c = int(a[i])**3
sum = sum + int(c)
return sum
def isArmstrong(n):
if cubesum(n) == n:
file:///C:/Users/HP/OneDrive/Applied ai/Module 1/assignment/assignment 1 solutions.html 2/6
9/14/22, 10:59 PM assignment 1 solutions
216
not an Armstrong number
0
Q7. write a function prodDigits() that inputs a number and returns the product of digits of that
number.
In [ ]:
def prodDigits(n):
prod = 1
for i in n:
prod = prod*int(i)
return prod
n = str(input("enter the number:"))
print(prodDigits(n))
48
Q8. If all digits of a number 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 each one digits is called multiplicative persistance of n. Using the
function prodDigits() of previous exercise write functions MDR() and MPersistance() that input a
number and return its multiplicative digital root and multiplicative persistence respectively
In [ ]:
def prodDigits(n):
prod = 1
for i in str(n):
prod = prod*int(i)
return prod
def MDR(n):
a = ''
mdrl = [n]
while len(str(n)) > 1:
a= prodDigits(n)
n = a
mdrl.append(str(a))
else:
return mdrl
def MPersistance(n):
count = 0
a = ''
mdrl = [n]
while len(str(n)) > 1:
a= prodDigits(n)
n = a
count = count + 1
else:
return count
48
['86', '48', '32', '6']
3
Q9. Write a function sumPdivisors() that find the sum of proper divisors of a number. Proper
divisors of a number are those numbers by which number is divisible, except the number itself.
In [ ]:
def sumPdivisors(n):
a = []
for i in range(1,n):
if n%i == 0:
a.append(i)
return [a,sum(a)]
print(sumPdivisors(28))
In [ ]:
def sumPdivisors(n):
a = []
for i in range(1,n):
if n%i == 0:
a.append(i)
return [a,sum(a)]
def comb(rng,r=2):
if r == 0:
return [[]]
comb_list = []
for i in range(10,rng):
e1 = i
e_rem = i+1
comb_rem = comb(e_rem,r-1)
for j in comb_rem:
comb_list.append([e1] + j)
return comb_list
def ami_pair(rg):
comb_list = comb(rg)
ami_list = []
for k in range(10,len(comb_list)):
x = int(comb_list[k][0])
y = int(comb_list[k][1])
xd = sumPdivisors(x)
yd = sumPdivisors(y)
if xd[1] == y and yd[1] == x and x!=y:
ami_list.append((x,y))
return ami_list
#print(comb(300))
print(ami_pair(1300))
In [ ]: def sumPdivisors(n):
a = []
for i in range(1,n):
if n%i == 0:
a.append(i)
return [a,sum(a)]
def perf_num(rng):
perf_list = []
for j in range(1,rng):
x = sumPdivisors(j)
if x[1] == j:
perf_list.append(j)
else:
j = j+1
return perf_list
print(perf_num(100))
[6, 28]
Q12. Write a program which can filter odd numbers in a list by using filter function.
In [ ]:
lst = [*range(10,1000,1)]
def oddno(n):
if n%2 != 0:
return True
else:
return False
odd_filter = filter(oddno,lst)
for j in odd_filter:
print(j, end=', ')
11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51,
53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93,
95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 12
9, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 1
63, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195,
197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229,
231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255, 257, 259, 261, 263,
265, 267, 269, 271, 273, 275, 277, 279, 281, 283, 285, 287, 289, 291, 293, 295, 297,
299, 301, 303, 305, 307, 309, 311, 313, 315, 317, 319, 321, 323, 325, 327, 329, 331,
333, 335, 337, 339, 341, 343, 345, 347, 349, 351, 353, 355, 357, 359, 361, 363, 365,
367, 369, 371, 373, 375, 377, 379, 381, 383, 385, 387, 389, 391, 393, 395, 397, 399,
401, 403, 405, 407, 409, 411, 413, 415, 417, 419, 421, 423, 425, 427, 429, 431, 433,
435, 437, 439, 441, 443, 445, 447, 449, 451, 453, 455, 457, 459, 461, 463, 465, 467,
469, 471, 473, 475, 477, 479, 481, 483, 485, 487, 489, 491, 493, 495, 497, 499, 501,
503, 505, 507, 509, 511, 513, 515, 517, 519, 521, 523, 525, 527, 529, 531, 533, 535,
537, 539, 541, 543, 545, 547, 549, 551, 553, 555, 557, 559, 561, 563, 565, 567, 569,
571, 573, 575, 577, 579, 581, 583, 585, 587, 589, 591, 593, 595, 597, 599, 601, 603,
605, 607, 609, 611, 613, 615, 617, 619, 621, 623, 625, 627, 629, 631, 633, 635, 637,
639, 641, 643, 645, 647, 649, 651, 653, 655, 657, 659, 661, 663, 665, 667, 669, 671,
673, 675, 677, 679, 681, 683, 685, 687, 689, 691, 693, 695, 697, 699, 701, 703, 705,
707, 709, 711, 713, 715, 717, 719, 721, 723, 725, 727, 729, 731, 733, 735, 737, 739,
741, 743, 745, 747, 749, 751, 753, 755, 757, 759, 761, 763, 765, 767, 769, 771, 773,
775, 777, 779, 781, 783, 785, 787, 789, 791, 793, 795, 797, 799, 801, 803, 805, 807,
809, 811, 813, 815, 817, 819, 821, 823, 825, 827, 829, 831, 833, 835, 837, 839, 841,
843, 845, 847, 849, 851, 853, 855, 857, 859, 861, 863, 865, 867, 869, 871, 873, 875,
877, 879, 881, 883, 885, 887, 889, 891, 893, 895, 897, 899, 901, 903, 905, 907, 909,
911, 913, 915, 917, 919, 921, 923, 925, 927, 929, 931, 933, 935, 937, 939, 941, 943,
945, 947, 949, 951, 953, 955, 957, 959, 961, 963, 965, 967, 969, 971, 973, 975, 977,
979, 981, 983, 985, 987, 989, 991, 993, 995, 997, 999,
Q13. Write a program which can map() to make a list whose elements are cube of elements in a
given list.
In [ ]:
def cubeit(n):
file:///C:/Users/HP/OneDrive/Applied ai/Module 1/assignment/assignment 1 solutions.html 5/6
9/14/22, 10:59 PM assignment 1 solutions
return n**3
lst = [*range(1,20)]
res = map(cubeit,lst)
print(list(res))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744, 3375, 4096, 49
13, 5832, 6859]
Q14. Write a program which can map() and filter() to make a list whose elements are cube of
even number in a given list.
In [ ]:
def cubeit(n):
return n**3
lst = [*range(1,100)]
res = map(cubeit,lst)
r = list(res)
print(r)
def evenno(m):
if m%2 == 0:
return True
else:
return False
even_filter = filter(evenno,r)
for j in even_filter:
print(j, end=', ')
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744, 3375, 4096, 49
13, 5832, 6859, 8000, 9261, 10648, 12167, 13824, 15625, 17576, 19683, 21952, 24389,
27000, 29791, 32768, 35937, 39304, 42875, 46656, 50653, 54872, 59319, 64000, 68921,
74088, 79507, 85184, 91125, 97336, 103823, 110592, 117649, 125000, 132651, 140608, 1
48877, 157464, 166375, 175616, 185193, 195112, 205379, 216000, 226981, 238328, 25004
7, 262144, 274625, 287496, 300763, 314432, 328509, 343000, 357911, 373248, 389017, 4
05224, 421875, 438976, 456533, 474552, 493039, 512000, 531441, 551368, 571787, 59270
4, 614125, 636056, 658503, 681472, 704969, 729000, 753571, 778688, 804357, 830584, 8
57375, 884736, 912673, 941192, 970299]
8, 64, 216, 512, 1000, 1728, 2744, 4096, 5832, 8000, 10648, 13824, 17576, 21952, 270
00, 32768, 39304, 46656, 54872, 64000, 74088, 85184, 97336, 110592, 125000, 140608,
157464, 175616, 195112, 216000, 238328, 262144, 287496, 314432, 343000, 373248, 4052
24, 438976, 474552, 512000, 551368, 592704, 636056, 681472, 729000, 778688, 830584,
884736, 941192,
In [ ]: