DEV Community

Cover image for The Hidden Beauty of Numbers: Solving Perfect Numbers, Factorials, and More with Python
Hariharan S J
Hariharan S J

Posted on

The Hidden Beauty of Numbers: Solving Perfect Numbers, Factorials, and More with Python

1.Introduction

Did you know that some numbers are considered so special that mathematicians have been studying them for thousands of years?

Take the number 28, for example. At first glance, it looks like any ordinary number. But surprisingly, it belongs to a rare category known as Perfect Numbers. Similarly, concepts like factorials and number series play a crucial role in mathematics, probability, and computer science.

As programmers, understanding how to solve these problems not only improves our coding skills but also strengthens our logical thinking. One of the best tools for solving such problems in Python is the while loop, a simple yet powerful looping construct that allows us to repeat tasks efficiently.

In this article, we'll build and understand four practical Python programs using while loops. By the end, you'll have a better understanding of Perfect Numbers, Factorials, Number Patterns, and Summation Techniques while also improving your Python programming skills.

Let's dive in!

2.Perfect Number Program

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding itself. For instance, 28 is a perfect number because the sum of its divisors (1, 2, 4, 7, and 14) is 28.

  • Perfect numbers are also known as "Complete Numbers" in number theory.

  • Some of the first perfect numbers are 6, 28, 496, and 8128

  • As of 2025, a total of 52 perfect numbers have been discovered.

Other Examples

  • 6

  • 28

  • 496

  • 8128

  • 33550336 up to infinity.

The latest Perfect Number was discovered in 2024 and has 82,048,64 digits.

3.Why are Perfect Numbers Important?

Perfect Numbers have fascinated mathematicians for thousands of years and are an important topic in Number Theory. They help researchers study the relationships between numbers and understand mathematical patterns.

For programmers, perfect number problems are useful because they teach important concepts such as:

  • Loops

  • Conditional statements

  • Divisor calculation

  • Modulus operator (%)

  • Problem-solving skills

Perfect number questions are also common in coding interviews, programming assignments, and competitive programming contests.

Python Program

no = 28

divisor = 1
sum = 0

while divisor < no:
    if no % divisor == 0:
        sum = sum + divisor
    divisor = divisor + 1

if sum == no:
    print("Perfect Number")
else:
    print("Not a Perfect Number")
Enter fullscreen mode Exit fullscreen mode

Flowchart

 ┌───────┐
 │ Start │
 └───┬───┘
     │
     ▼
Initialize
no, divisor, sum
     │
     ▼
divisor < no ?
     │
 ┌───┴───┐
 │  Yes  │
 └───┬───┘
     │
     ▼
no % divisor == 0 ?
     │
 ┌───┴────┐
 │  Yes   │
 └───┬────┘
     │
     ▼
sum = sum + divisor
     │
     ▼
divisor++
     │
     └──────────┐
                │
                ▼
        divisor < no ?
                │
                ▼
       Loop Continues

After Loop

sum == no ?
     │
 ┌───┴───┐
 │       │
 ▼       ▼
Perfect  Not Perfect
Number   Number
Enter fullscreen mode Exit fullscreen mode

Output

Perfect Number
Enter fullscreen mode Exit fullscreen mode

How the Program Works

The program checks every number from 1 up to the given number. Whenever a number divides the given number completely, it is considered a divisor and added to the sum. Finally, if the sum of all divisors equals the original number, the program prints "Perfect Number".

4.Odd and Even Number Pattern in a Single Loop

Sometimes a problem can be solved in multiple ways. The following program generates odd numbers first and then even numbers using a single loop.

Python Program

no = 1

while no <= 10:
    if no <= 5:
        print((no * 2) - 1)
    else:
        print((no - 5) * 2)

    no = no + 1
Enter fullscreen mode Exit fullscreen mode

Flowchart

 ┌───────┐
 │ Start │
 └───┬───┘
     │
     ▼
 no = 1
     │
     ▼
 no <= 10 ?
     │
 ┌───┴───┐
 │  Yes  │
 └───┬───┘
     │
     ▼
 no <= 5 ?
     │
 ┌───┴─────┐
 │         │
 ▼         ▼
Print      Print
Odd        Even
Formula    Formula
     │
     ▼
 no++
     │
     └─────Loop
Enter fullscreen mode Exit fullscreen mode

Output

1
3
5
7
9
2
4
6
8
10
Enter fullscreen mode Exit fullscreen mode

How the Program Works

For the first five iterations, the formula (no * 2) - 1 generates odd numbers.

For the remaining iterations, the formula (no - 5) * 2 generates even numbers.

This approach demonstrates how mathematical expressions and conditions can be combined inside a single loop to produce a specific pattern.

5.Factorial of a Number

Factorial is one of the most commonly used mathematical operations in programming.

The factorial of a number is the product of all positive integers from 1 to that number.

For example:

5! = 5 × 4 × 3 × 2 × 1 = 120

Why is Factorial Used?

Factorials are widely used in:

  • Permutations and Combinations

  • Probability calculations

  • Mathematics

  • Algorithm design

  • Computer Science research

Python Program

no = 5

i = 1
fact = 1

while i <= no:
    fact = fact * i
    i = i + 1

print(fact)
Enter fullscreen mode Exit fullscreen mode

Flowchart

 ┌───────┐
 │ Start │
 └───┬───┘
     │
     ▼
Initialize
i = 1
fact = 1
     │
     ▼
 i <= no ?
     │
 ┌───┴───┐
 │  Yes  │
 └───┬───┘
     │
     ▼
fact = fact * i
     │
     ▼
i++
     │
     └────Loop

After Loop

     ▼
Print factorial
     │
     ▼
    End
Enter fullscreen mode Exit fullscreen mode

Output

120
Enter fullscreen mode Exit fullscreen mode

How the Program Works

The variable fact starts with the value 1. The loop multiplies it by every number from 1 to 5. Once the loop finishes, the final result stored in fact becomes 120.

6.Sum of First N Numbers

Finding the sum of a sequence is another common programming problem.

If we want to find the sum of the first 10 numbers:

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10

The result is:

55

Python Program

no = 10

i = 1
sum = 0

while i <= no:
    sum = sum + i
    i = i + 1

print(sum)
Enter fullscreen mode Exit fullscreen mode

Flowchart

 ┌───────┐
 │ Start │
 └───┬───┘
     │
     ▼
Initialize
i = 1
sum = 0
     │
     ▼
 i <= no ?
     │
 ┌───┴───┐
 │  Yes  │
 └───┬───┘
     │
     ▼
sum = sum + i
     │
     ▼
i++
     │
     └────Loop

After Loop

     ▼
Print sum
     │
     ▼
    End
Enter fullscreen mode Exit fullscreen mode

Output

55
Enter fullscreen mode Exit fullscreen mode

How the Program Works

The variable sum starts with 0. During each iteration, the current value of i is added to sum. After all numbers from 1 to 10 are processed, the final value of sum becomes 55.

Real-World Applications

This concept is useful in:

  • Data analysis

  • Mathematical series calculations

  • Statistical computations

  • Algorithm development

  • Financial calculations

7.Conclusion

The while loop is one of the most fundamental tools in Python programming. Although it appears simple, it can be used to solve a wide variety of problems.

In this article, we explored how to:

  • Identify a Perfect Number

  • Generate odd and even number patterns

  • Calculate the factorial of a number

  • Find the sum of the first N numbers

These programs help beginners strengthen their understanding of loops, conditions, arithmetic operations, and logical thinking. Once you master these concepts, you'll be better prepared to solve more advanced programming challenges in Python.

Happy Coding.

Reference

(https://www.geeksforgeeks.org/maths/perfect-numbers/)

Top comments (0)