Python Program to Print All Pronic Numbers Between 1 and 100
Pronic numbers are also called oblong numbers or rectangular numbers. They are produced by multiplying two successive integers. Mathematically, it can be written as n * (n + 1), where n is a positive integer.
For instance, 6 is a pronic number because it equals 2 * (2 + 1). Similarly, when you multiply 3 by 4 it equals; hence,12 is another pronic number as it is equal to 3 * (3 + 1)
Example:
Input: n1 = 1, n2 = 100 Output: 2, 6, 12, 20, 30, 42, 56, 72, 90
Explanation: These are the pronic number between the range of 1 to 100
Python Program to Print All Pronic Numbers Between 1 and 100
Below are some of the ways by which we can print all pronic numbers between 1 and 100 in Python:
- Using a Loop
- Using List Comprehension
Approach 1: Using a Loop
In this approach, a single loop from one to the given limit(n) iterates over simple values. It examines all i and checks if their products with i+1 fall within the specified limits. If so then i*(i+1) makes a pronic number which is displayed immediately.
def solve(n):
for i in range(1, n + 1):
if i * (i + 1) <= n:
print(i * (i + 1), end=" ")
print("Pronic Numbers Between 1 and 100:")
solve(100)
Output
Pronic Numbers Between 1 and 100: 2 6 12 20 30 42 56 72 90
Approach 2: Using List Comprehension
List comprehension offers an elegant way of generating list of pronic numbers. As such it goes through range of numbers computing product between each number and its next consecutive integer and stores them on list .
def solve(n):
pronic_nums = [i * (i + 1) for i in range(1, int(n ** 0.5) + 1)]
print("Pronic Numbers Between 1 and 100:")
print(*pronic_nums)
solve(100)
Output
Pronic Numbers Between 1 and 100: 2 6 12 20 30 42 56 72 90 110