Home > Python > Basic Python Programs

Ch. 3.3 Finding Prime Number Greater or Lesser Than a Given Value

Chapters Index
Program 1

Finding Prime Number Greater or Lesser Than a Given Value

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

Input

Given number (e.g., 53).

Call next_prime(given_value)

Increment num by 1.

Check if num is Prime:

Start loop: Check divisibility from 2 to num - 1.

  • If num is divisible by any i, return False.
  • If no divisors are found, return True.
  • If not prime, increment num and repeat the check.
  • Once a prime is found, return that prime number.

Store the result in next_prime_greater.

Call previous_prime(given_value)

Decrement num by 1.

Check if num is Prime:

Start loop: Check divisibility from 2 to num - 1.

  • If num is divisible by any i, return False.
  • If no divisors are found, return True.
  • If not prime, decrement num and repeat the check.
  • Once a prime is found, return that prime number.

Store the result in previous_prime_lesser.

Program 2

Finding Prime Number Greater or Lesser Than a Given Value

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.