DEV Community

Cover image for Python Looping #3: Divisors, Multiples, and Number Patterns Explained
Ebenezer
Ebenezer

Posted on

Python Looping #3: Divisors, Multiples, and Number Patterns Explained

Hey Folks! 👋

Good Day.

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

Over the past few days, our trainer assigned us 15 beginner-level Python problems to strengthen our logical thinking and programming fundamentals.

In my previous blogs, I covered factorials, perfect numbers, and custom number patterns. Today, I completed the remaining problems from the assignment using simple logic and basic Python concepts.

Whenever I face a new problem, I try to answer three questions before writing code:

  1. What is the expected output?
  2. What pattern do I see?
  3. How would I solve it manually on paper?

Once those answers become clear, the code usually becomes straightforward.

In this Blog, I'll share the solutions along with the way I approached each problem mentally.


Problem 1: Finding Divisors of a Number

Let's take the number 10.

The divisors of 10 are:

1 2 5 10
Enter fullscreen mode Exit fullscreen mode

A divisor is simply a number that divides another number without leaving a remainder.

Before coding, I thought:

If I wanted to find all divisors manually, I would check every number from 1 to 10 and see whether it divides 10 perfectly.

That thought became the algorithm.

Python Solution

no = 10
i = 1
count = 0

while i <= no:
    if no % i == 0:
        print(i)
        count += 1

    i += 1

print(count)
Enter fullscreen mode Exit fullscreen mode

Pseudocode

START

number = 10

FOR every value from 1 to number

    IF number is divisible by current value

        PRINT current value

END FOR

PRINT total divisor count

STOP
Enter fullscreen mode Exit fullscreen mode

What I Learned

The modulus operator (%) is one of the most useful tools in problem solving.

Whenever I need to check:

  • divisors
  • multiples
  • odd numbers
  • even numbers

I almost always end up using %.


Problem 2: Multiples of 3 and 5

The goal was to print numbers that are divisible by both 3 and 5 between 1 and 100.

Before coding, I asked:

What makes a number a multiple of both 3 and 5?

Answer:

number % 3 == 0
AND
number % 5 == 0
Enter fullscreen mode Exit fullscreen mode

Python Solution

no = 1

while no <= 100:

    if no % 3 == 0 and no % 5 == 0:
        print(no)

    no += 1
Enter fullscreen mode Exit fullscreen mode

Pseudocode

START

number = 1

WHILE number <= 100

    IF divisible by 3 AND divisible by 5

        PRINT number

    number = number + 1

END WHILE

STOP
Enter fullscreen mode Exit fullscreen mode

Key Learning

Many beginners confuse:

and
Enter fullscreen mode Exit fullscreen mode

with

or
Enter fullscreen mode Exit fullscreen mode

Remember:

  • and → both conditions must be true
  • or → at least one condition must be true

Problem 3: Simple Number Patterns

Most beginners see pattern problems as difficult.

I used to think the same.

Now I try a different approach.

Instead of looking at the whole pattern, I ask:

What changes between one number and the next?

Once I find that relationship, the pattern becomes easy.


Pattern 1

Output:

1 1 1 1 1
Enter fullscreen mode Exit fullscreen mode

Observation

Nothing changes.

The same value is printed every time.

no = 1

while no <= 5:
    print("1", end=" ")
    no += 1
Enter fullscreen mode Exit fullscreen mode

Pattern 2

Output:

1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode

Observation

Increase by 1.

no = 1

while no <= 5:
    print(no, end=" ")
    no += 1
Enter fullscreen mode Exit fullscreen mode

Pattern 3

Output:

1 3 5 7 9
Enter fullscreen mode Exit fullscreen mode

Observation

Increase by 2.

no = 1

while no <= 9:
    print(no, end=" ")
    no += 2
Enter fullscreen mode Exit fullscreen mode

Pattern 4

Output:

3 6 9 12 15
Enter fullscreen mode Exit fullscreen mode

Observation

Multiples of 3.

no = 1

while no <= 5:
    print(no * 3, end=" ")
    no += 1
Enter fullscreen mode Exit fullscreen mode

Pattern 5

Output:

15 12 9 6 3
Enter fullscreen mode Exit fullscreen mode

Observation

Reverse multiples of 3.

no = 5

while no >= 1:
    print(no * 3, end=" ")
    no -= 1
Enter fullscreen mode Exit fullscreen mode

Pattern 6

Output:

10 8 6 4 2
Enter fullscreen mode Exit fullscreen mode

Observation

Reverse multiples of 2.

no = 5

while no >= 1:
    print(no * 2, end=" ")
    no -= 1
Enter fullscreen mode Exit fullscreen mode

Pattern 7

Output:

9 7 5 3 1
Enter fullscreen mode Exit fullscreen mode

Observation

Reverse odd numbers.

no = 9

while no >= 1:
    print(no, end=" ")
    no -= 2
Enter fullscreen mode Exit fullscreen mode

The Biggest Lesson From These 15 Problems

After solving all 15 problems, I realized something interesting.

The challenge was never Python.

The challenge was understanding the pattern.

Once the logic became clear, the code became simple.


What's Next?

With these beginner practice problems completed, I'll be moving to a new phase from tomorrow.

Instead of simple exercises, I'll start solving interview-style and job-market coding questions in detail.

The focus will be on:

  • Problem-solving approaches
  • Multiple solutions
  • Time complexity discussions
  • Common interview questions
  • Real-world coding challenges

Let's see what tomorrow brings.

Top comments (0)