Assignment 1 Python
Assignment 1 Python
def multiplication_table(num):
'''this is a function for multiplication table
'''
multiplication_table(num)
In [2]: #question 2
def prime_number(val):
'''fucntion for finding prime number'''
for n in range(2,val):
if val % n == 0:
return False
break
else:
return val
for i in range(2,1001):
num1 = i
num2 = i+2
if prime_number(num1) and prime_number(num2):
print("(", num1 ,",", num2, ")")
( 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 )
In [8]: #question 3
n = 64
while n % 2 == 0:
print(2)
n = n / 2
2
2
2
2
2
2
In [37]: #question 4
#implimenting the formula of permutation and combination
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n-1)
20.0
10.0
Out[37]: True
In [108]: #question 5
def binary_to_decimal(num):
if num > 1:
binary_to_decimal(num // 2)
print(num % 2)
In [15]: #question 6
def cubesum (a):
a = 370
c = cubesum(a)
isarmstrong(c)
is armstrong
In [101]: #question 7
def prodDigits(lst):
lst = [int(i) for i in str(a)]
product = 1
for e in lst:
product *= e
print(product)
a = input("input a number :")
prodDigits(a)
In [7]: #question 8
def prodDigits(n):
product = 1
while n >= 1:
product = product * (n % 10)
n = n // 10
return product
def MPersistense (n):
product = 1
count = 0
while n >= 9:
n = prodDigits(n)
count = count + 1
return n, count
In [114]: #question 9
def sumpdivisors(a):
sum = 0
for i in range(1,a):
if a % i == 0:
print(i)
sum = sum + i
return sum
a = int(77)
sumpdivisors(a)
1
7
11
Out[114]: 19
In [69]: #question 10
def sumpdivisors(i):
sum = 0
for a in range(1,i):
if i % a == 0:
sum = sum + a
return sum
6 is a perfect number
28 is a perfect number
496 is a perfect number
In [107]: #question 11
def sumpdivisors(i):
sum = 0
for a in range(1,i):
if i % a == 0:
sum = sum + a
return sum
def amicable_num(k):
for i in range(1,k):
n = sumpdivisors(i)
if sumpdivisors(n) == i and n != i:
In [67]: #question 12
def odd_numbers(n):
while n % 2 != 0:
return n
n = [2,31,9,-7,1,2,6,7]
list(filter(odd_numbers, n))
In [98]: #question 13
def cube(n):
i = 1
while n!=0:
return n**3
if n == 0:
return 0
n = [2,3,5,9,8,-4,0]
length = len(n)
list(map(cube, n))
In [100]: #question 14
def even_numbers(n):
while n % 2 == 0:
return n
def cube(n):
i = 1
while n!=0:
return n**3
if n == 0:
return 0
n = [2,3,5,8,9,-3,-4,6,-6,0]
new_list = list(filter(even_numbers,n))
list(map(cube,new_list))
In [ ]: