Python Topics
- Python Intro
- Python String Methods
- Python Interview Programs
- Abundant Number
- Amicable Number
- Armstrong Number
- Average of N Numbers
- Automorphic Number
- Biggest of three numbers
- Binary to Decimal
- Common Divisors
- Composite Number
- Condense a Number
- Cube Number
- Decimal to Binary
- Decimal to Octal
- Disarium Number
- Even Number
- Evil Number
- Factorial of a Number
- Fibonacci Series
- GCD
- Happy Number
- Harshad Number
- LCM
- Leap Year
- Magic Number
- Matrix Addition
- Matrix Division
- Matrix Multiplication
- Matrix Subtraction
- Matrix Transpose
- Maximum Value of an Array
- Minimum Value of an Array
- Multiplication Table
- Natural Number
- Number Combination
- Odd Number
- Palindrome Number
- Pascalβs Triangle
- Perfect Number
- Perfect Square
- Power of 2
- Power of 3
- Pronic Number
- Prime Factor
- Prime Number
- Smith Number
- Strong Number
- Sum of Array
- Sum of Digits
- Swap Two Numbers
- Triangular Number
- Python Star Pattern
- Python Number Pattern
- Python Alphabet Pattern
Python Program to Check Pronic Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, mathematical patterns and properties often become interesting challenges. One such intriguing concept is the "Pronic Number."
A pronic number, also known as a rectangular number, oblong number, or heteromecic number, is the product of two consecutive integers.
In this tutorial, we will dive into a Python program designed to check whether a given number is a pronic number or not.
π Example
Now, let's explore the Python code that checks whether a given number is a pronic number or not.
import math
# Function to check if a number is a pronic number
def is_pronic(num):
sqrt_num = int(math.sqrt(num))
return sqrt_num * (sqrt_num + 1) == num
# Driver program
if __name__ == "__main__":
# Replace this value with your desired number
number = 12
# Check if the number is a pronic number
if is_pronic(number):
print(f"{number} is a pronic number.")
else:
print(f"{number} is not a pronic number.")
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the if __name__ == "__main__": block.
12 is a pronic number.
Run the script to check if the given number is a pronic number.
π§ How the Program Works
- The program defines a function is_pronic that takes a number as input and returns True if the number is a pronic number and False otherwise.
- Inside the function, it calculates the integer square root of the input number.
- It checks whether the product of the square root and the square root incremented by 1 equals the input number. If yes, the number is a pronic number.
- The driver program tests the function with a sample number and prints the result.
π Between the Given Range
Let's take a look at the python code that checks for pronic numbers in the range from 1 to 20.
# Function to check if a number is Pronic
def is_pronic(number):
for i in range(1, int(number**0.5) + 1):
if i * (i + 1) == number:
return True
return False
# Range to check for Pronic numbers
start_range = 1
end_range = 20
# Display Pronic numbers in the specified range
pronic_numbers = [num for num in range(start_range, end_range + 1) if is_pronic(num)]
print(f"Pronic Numbers in the range {start_range} to {end_range}:\n{' '.join(map(str, pronic_numbers))}")
π» Testing the Program
Pronic Numbers in the range 1 to 20: 2 6 12 20
Simply run the Python script to see the Pronic numbers in the range from 1 to 20.
π§ How the Program Works
- The program defines a function is_pronic to check if a given number is a Pronic number.
- Inside the function, it iterates through each number up to the square root of the input number, checking if nΓ(n+1) is equal to the given number.
- The main section then defines the range from 1 to 20 and identifies Pronic numbers within that range.
- The result is displayed in the specified output format.
π§ Understanding the Concept of Pronic Numbers
Before delving into the code, let's take a moment to understand the concept of pronic numbers.
A pronic number is a product of two consecutive integers, and it has applications in various mathematical and geometric contexts.
A pronic number, denoted by Pn, is a number of the form Pn=n Γ (n+1) where n is a non-negative integer. The sequence of pronic numbers starts as 0, 2, 6, 12, 20, and so on.
For example, let's take n = 3. The corresponding pronic number would be P3 = 3x(3+1) = 12.
π’ Optimizing the Program
While the provided program is effective, consider exploring ways to optimize the computation of pronic numbers for larger inputs.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Python Program to Check Pronic Number), please comment here. I will help you immediately.