DEV Community

Vasanthakumari N
Vasanthakumari N

Posted on

Python Day 16: Solving Mathematical Problems Using Functions and Loops

1. Find the Smallest and Biggest Two-Digit Prime Numbers

Problem: Write a Python program to find the smallest and biggest two-digit prime numbers.

A prime number has exactly two divisors: 1 and itself.

Step 1: Create a Function to Check Prime Numbers

def find_prime(no): #12
    div = 2
    while div <= no//2:     #10 Times 
        if no % div == 0:
            return False 
        div+=1
    else:
        return True
Enter fullscreen mode Exit fullscreen mode

The function returns:

  • True if the number is prime.
  • False if the number is not prime.

Step 2: Find the Smallest and Biggest Prime Numbers

no = 11
first = True 
last_prime = no 
while no <= 99:
    result = find_prime(no) #Calling Statement
    if result == True and first == True: 
        print("First Prime Number", no)
        first = False 
    elif result == True: 
        last_prime = no #13 17  19 23 29 31 37 43 47  
    no+=2

print("Last Prime Number is", last_prime)
Enter fullscreen mode Exit fullscreen mode
Output:

First Prime Number: 11
Last Prime Number: 97
Enter fullscreen mode Exit fullscreen mode

Understanding the logic:

  • Start from 11 because we are checking two-digit prime numbers.
  • Increase the number by 2 to skip even numbers.
  • Use find_prime() to check whether each number is prime.
  • Use first to identify the first prime number.
  • Update last_prime whenever another prime number is found.

2.Find the Two-Digit Prime Number Using Conditions

Problem:I am a two-digit prime number. The sum of my digits is 10. I am also one of the factors of 57.

Who am I?
Step 1: Create a Function to Calculate the Sum of Digits

def sum_of_digits(no):
    total = 0
    while no > 0:
        total = total + no%10
        no = no //10
    return total
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Prime-Checking Function

def find_prime(no): #12
    div = 2
    while div <= no//2:     #10 Times 
        if no % div == 0:
            return False 
        div+=1
    else:
        return True
Enter fullscreen mode Exit fullscreen mode

Step 3: Find the Answer

no = 11
while no <= 99:
    result = find_prime(no) #Calling Statement
    if result == True: 
        #print("Prime Number", no)
        total = sum_of_digits(no)
        if total == 10:
            if 57 % no == 0:
                print("Yes, I found", no)
    no+=2
Enter fullscreen mode Exit fullscreen mode
Output:

Yes, I found: 19
Enter fullscreen mode Exit fullscreen mode

Let's verify:

  • 19 is a two-digit number.
  • 19 is a prime number.
  • Sum of digits: 1 + 9 = 10.
  • 57 % 19 = 0.

Therefore, the answer is 19.

3. Find the Smallest Five-Digit Number Divisible by 72 and 108

Problem: What is the smallest five-digit number that is exactly divisible by both 72 and 108?

no = 10000
while no <= 99999:
    if no%72==0 and no%108==0:
        print(no)
        break
    no+=1
Enter fullscreen mode Exit fullscreen mode
Output:

10152
Enter fullscreen mode Exit fullscreen mode

Understanding the Logic

  • A five-digit number starts from 10000.
  • Check whether the number is divisible by both 72 and 108.
  • If both conditions are true, display the number.
  • Use break to stop the loop because we need the smallest number.

4.Four Mobile Phones Ringing Together

Problem:There are four Mobile Phones in a house. At 5 a.m, all the four Mobile Phones will ring together. Thereafter, the first one rings every 15 minutes, the second one rings every 20 minutes, the third one rings every 25 minutes and the fourth one rings every 30 minutes. At what time, will the four Mobile Phones ring together again?

Mathematical Concept: LCM

We need to find the LCM of:

15, 20, 25 and 30.

LCM = 300 minutes.

300 minutes = 5 hours.

Starting time = 5:00 a.m.

Therefore:

5:00 a.m. + 5 hours = 10:00 a.m.

phone1 = 15
phone2 = 20
phone3 = 25
phone4 = 30

minutes = 1

while True:
    if (minutes % phone1 == 0 and
        minutes % phone2 == 0 and
        minutes % phone3 == 0 and
        minutes % phone4 == 0):

        print("All phones ring together after:", minutes, "minutes")
        break

    minutes += 1
Enter fullscreen mode Exit fullscreen mode

Output:

All phones ring together after: 300 minutes
Enter fullscreen mode Exit fullscreen mode

5.Book Seller – Dividing Books into Equal Boxes

Problem:A book seller has 175 English books, 245 Science books and 385 Mathematics books. He wants to sell the books in a box, subject-wise in equal numbers. What will be the greatest number of the boxes required? Also find the number of books for each subject in a box.

english = 175
science = 245
maths = 385

no = 1
gcd = 1

while no <= english and no <= science and no <= maths:
    if english % no == 0 and science % no == 0 and maths % no == 0:
        gcd = no

    no += 1

print("Number of Boxes:", gcd)
print("English Books per Box:", english // gcd)
print("Science Books per Box:", science // gcd)
print("Maths Books per Box:", maths // gcd)
Enter fullscreen mode Exit fullscreen mode
Output:

Number of Boxes: 35
English Books per Box: 5
Science Books per Box: 7
Maths Books per Box: 11
Enter fullscreen mode Exit fullscreen mode

Top comments (0)