DEV Community

Cover image for Python Looping #2: Factorial, Perfect Numbers, and Pattern Printing Explained
Ebenezer
Ebenezer

Posted on

Python Looping #2: Factorial, Perfect Numbers, and Pattern Printing Explained

Hey Folks! 👋

Good Day.

Welcome back to Day 2 of my Python Problem Solving journey.

A few days ago, our trainer assigned us 15 Python programming problems . Today, I solved another three problems from that list.

One thing I've started realizing is that programming is less about syntax and more about problem-solving. Once the logic becomes clear, writing the code becomes much easier.

In this article, I'll walk through three Python problems I solved and share the thinking process behind each solution.


Problem 1: Factorial of a Number

Understanding the Problem

A factorial is the product of all positive integers from a given number down to 1.

For example:

5! = 5 × 4 × 3 × 2 × 1
Enter fullscreen mode Exit fullscreen mode

Result:

120
Enter fullscreen mode Exit fullscreen mode

Before writing any code, I asked myself:

If I were solving this manually on paper, what exact steps would I follow?

The answer became the algorithm itself.

  1. Start with 1.
  2. Multiply by the current number.
  3. Move to the next smaller number.
  4. Repeat until reaching 1.

Python Solution Using While Loop

no = 5
factorial = 1

while no > 0:
    factorial = factorial * no
    no -= 1

print(factorial)
Enter fullscreen mode Exit fullscreen mode

Output

120
Enter fullscreen mode Exit fullscreen mode

Pseudocode

START

number = 5
factorial = 1

WHILE number > 0
    factorial = factorial × number
    number = number - 1
END WHILE

PRINT factorial

STOP
Enter fullscreen mode Exit fullscreen mode

Flowchart

Start
  |
  v
number = 5
factorial = 1
  |
  v
number > 0 ?
  |
 Yes
  |
factorial = factorial * number
  |
number = number - 1
  |
  +------> Repeat
  |
 No
  |
Print factorial
  |
 End
Enter fullscreen mode Exit fullscreen mode

Alternative Solution Using For Loop

no = 5
factorial = 1

for i in range(1, no + 1):
    factorial = factorial * i

print(factorial)
Enter fullscreen mode Exit fullscreen mode

Output

120
Enter fullscreen mode Exit fullscreen mode

Key Takeaway

When you already know how many iterations are required, a for loop is usually cleaner and easier to read.


Problem 2: Print Odd Numbers First and Then Even Numbers

Expected Output

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

At first glance, most people would solve this using two separate loops:

  • One loop for odd numbers
  • One loop for even numbers

I wanted to see whether I could achieve the same result using a single loop.


My Thought Process

I imagined the output as two separate queues.

Odd numbers:

1 → 3 → 5 → 7 → 9
Enter fullscreen mode Exit fullscreen mode

Even numbers:

2 → 4 → 6 → 8 → 10
Enter fullscreen mode Exit fullscreen mode

The challenge wasn't generating the numbers.

The challenge was switching from the first sequence to the second without creating another loop.


Python Solution

no = 1

while no <= 10:
    print(no, end=" ")
    no += 2

    if no == 11:
        no = 2
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 Logic Works

The loop starts at 1 and keeps incrementing by 2.

This generates:

1 3 5 7 9
Enter fullscreen mode Exit fullscreen mode

After printing 9:

no += 2
Enter fullscreen mode Exit fullscreen mode

changes the value to:

11
Enter fullscreen mode Exit fullscreen mode

At that point:

if no == 11:
    no = 2
Enter fullscreen mode Exit fullscreen mode

resets the sequence and begins printing even numbers.

A small condition completely changes the direction of the program.


Pseudocode

START

number = 1

WHILE number <= 10

    PRINT number

    number = number + 2

    IF number == 11
        number = 2

END WHILE

STOP
Enter fullscreen mode Exit fullscreen mode

Flowchart

Start
  |
  v
number = 1
  |
  v
number <= 10 ?
  |
 Yes
  |
Print number
  |
number = number + 2
  |
number == 11 ?
 / \
Yes No
 |
number = 2
 |
 +------> Repeat
 |
 No
 |
 End
Enter fullscreen mode Exit fullscreen mode

Key Takeaway

Sometimes a simple condition can eliminate the need for an additional loop.

Always ask yourself:

Can I simplify the solution without making it harder to understand?


Problem 3: Perfect Number Program in Python

What Is a Perfect Number?

A perfect number is a number whose proper divisors add up exactly to the original number.

Example:

6
Enter fullscreen mode Exit fullscreen mode

Divisors:

1, 2, 3
Enter fullscreen mode Exit fullscreen mode

Sum:

1 + 2 + 3 = 6
Enter fullscreen mode Exit fullscreen mode

Since the sum equals the original number, 6 is called a Perfect Number.


Real-World Analogy

Imagine a group of friends contributing money to buy a gift.

If the total contribution exactly matches the target amount, everything balances perfectly.

That's exactly how a perfect number works.

The sum of all valid contributors (divisors) equals the target amount (the number itself).


Python Solution

no = 496
sum = 0
i = 1

while i < no:

    if no % i == 0:
        sum += i

    i += 1

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

How the Logic Works

The program checks every number from 1 to n - 1.

If a number divides the original number without leaving a remainder:

no % i == 0
Enter fullscreen mode Exit fullscreen mode

it gets added to the running total.

After all checks are complete, we compare:

sum == no
Enter fullscreen mode Exit fullscreen mode

If both values match, the number is perfect.


Dry Run Using 6

Divisor Added? Running Sum
1 Yes 1
2 Yes 3
3 Yes 6
4 No 6
5 No 6

Final Check:

6 == 6
Enter fullscreen mode Exit fullscreen mode

Result:

Perfect Number
Enter fullscreen mode Exit fullscreen mode

Pseudocode

START

number = n
sum = 0

FOR each value from 1 to n - 1

    IF value divides number
        sum = sum + value

END FOR

IF sum equals number
    PRINT Perfect Number
ELSE
    PRINT Not Perfect Number

STOP
Enter fullscreen mode Exit fullscreen mode

Flowchart

Start
  |
  v
sum = 0
i = 1
  |
  v
i < number ?
  |
 Yes
  |
number % i == 0 ?
 / \
Yes No
 |
sum += i
 |
i += 1
 |
 +------> Repeat
 |
 No
 |
sum == number ?
 / \
Yes No
 |   |
Perfect
     |
Not Perfect
Enter fullscreen mode Exit fullscreen mode

What I Learned from These Problems

Although these programs are small, they helped me strengthen some important programming fundamentals:

  • Loops
  • Conditional Statements
  • Mathematical Logic
  • Pattern Generation
  • Problem Decomposition
  • Dry Running Code

* Converting Logic into Programs

My Current Problem-Solving Workflow

Problem
    ↓
Understand the Requirement
    ↓
Break It Into Smaller Steps
    ↓
Write Pseudocode
    ↓
Code
Enter fullscreen mode Exit fullscreen mode

This workflow has helped me reduce mistakes and understand programming concepts much more deeply.


If you're new to this series, check out Part 1:

https://dev.to/buddingdeveloper/python-for-beginners-how-i-solved-two-classic-problems-in-multiple-ways-with-flowcharts-30ie

Top comments (0)