100% found this document useful (1 vote)
51 views

Function Assignment PDF

The document contains 9 programming problems related to number theory concepts like prime numbers, factorials, binary conversion, digital roots etc. and solutions to these problems in Python. Functions are defined to find multiplication tables, twin primes, prime factors, permutations, combinations, binary conversion, digital sum, digital product and sum of proper divisors. These functions take a number as input and perform the relevant calculations, printing or returning the results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
51 views

Function Assignment PDF

The document contains 9 programming problems related to number theory concepts like prime numbers, factorials, binary conversion, digital roots etc. and solutions to these problems in Python. Functions are defined to find multiplication tables, twin primes, prime factors, permutations, combinations, binary conversion, digital sum, digital product and sum of proper divisors. These functions take a number as input and perform the relevant calculations, printing or returning the results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

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

In [1]: number = input("Enter the number:")


for i in range(1,11):
print(number, "x", i,"=", int(number)*i)

Enter the number:13


13 x 1 = 13
13 x 2 = 26
13 x 3 = 39
13 x 4 = 52
13 x 5 = 65
13 x 6 = 78
13 x 7 = 91
13 x 8 = 104
13 x 9 = 117
13 x 10 = 130

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 [8]: def is_prime(n):


for i in range(2, n):
if n % i == 0:
return False
return True

def generate_twins(n):
for i in range(1, n):
j = i + 2
if(is_prime(i) and is_prime(j)):
print("{:d} and {:d}".format(i, j))

generate_twins(int(input("Enter the maximum number:")))

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Enter the maximum number:100
1 and 3
3 and 5
5 and 7
11 and 13
17 and 19
29 and 31
41 and 43
59 and 61
71 and 73

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

In [ ]: number = int(input("Enter the numbe:"))


while number != 0:
for i in range(2,int(number+1)):
if number % i == 0:
number/=i
print(i)
break

Enter the numbe:2310


2
3
5
7
11

4. 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!

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

In [7]: def dec2bin(n):

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
if n>=1:
dec2bin(n/2)
print(int(n%2),end='')

dec2bin(int(input("Enter the decimal Number:")))

Enter the decimal Number:8


1000

6. 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 [61]: def isArmstrong(New_number,old_number):


if(New_number==old_number):
PrintArmstrong(old_number)
else:
print("Number is not Prime")

def PrintArmstrong(old_number):
print(old_number, "is Prime.")

def general_sum(n):
summ = 0
rem = 0
count = 0
original = n
n1 = n
while n1!= 0:
n1 = int(n1/10)
count = int(count +1)
while n!=0:
rem = int(n%10)
n = int(n/10)
summ = summ + (rem**count)
isArmstrong(summ,original)

general_sum(int(input("Enter the number:")))

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Enter the number:372
Number is not Prime

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

In [10]: def prodDigit(n):


product = 1
while n!=0:
product = int(product * (int(n%10)))
n = int(n/10)
print(product)

prodDigit(int(input("Enter the number:")))

Enter the number:123


6

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 [16]: def prodDigit(n,count1):


n1=n
product = 1
if count1!=1:
while n!=0:
print("--> ",end='')
product = int(product * (int(n%10)))
n = int(n/10)
print(n)
else:

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
print(n1)

def Digit_Count(n):
n1=n
print(n1,end='')
count = 0
while n!=0:
n = int(n/10)
count = count + 1
prodDigit(n1,count)

Digit_Count(int(input("Enter the number:")))

Enter the number:86


86--> --> 0

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, 18

In [6]: def sumPdivisor(n):


n1=n
for i in range(1,int(n/2)+1):
if int(n%i)==0:
print(i,end=' ')

sumPdivisor(int(input("Enter the number:")))

Enter the number:81


1 3 9 27

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 [24]: def properDivisor(original_number,number_to_be_checked):

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
if original_number%number_to_be_checked == 0:
properDivisor(original_number,(number_to_be_checked-1))
print(number_to_be_checked)

def func(n):
properDivisor(n,int(n/2))

func(int(input("Enter the number:")))

Enter the number:28


14

In [44]: def func(n):


summ = 0

for i in range(1,int(n/2)+1):
if n%i==0:
summ = summ + i

if summ==n:
print(n,"is perfect number.")
else:
print(n,"is not perfect number.")

func(int(input("Enter the number:")))

Enter the number:6


6 is perfect number.

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD

You might also like