Introduction
Hey Folks! π
Hope you're doing well.
It's been about a week since I last published a technical blog. Over the past few days, I've been spending more time learning, practicing, and solving programming problems, which is why I haven't posted recently.
A few days ago, our trainer assigned us a set of 15 programming problems and asked us to solve them, push the solutions to GitHub, and share our learning journey through a blog. I found this task particularly interesting because it encouraged not only coding but also documenting the thought process behind each solution.
In this blog series, I'll be covering three of those fifteen problems. Rather than simply presenting the final code, I want to walk through the complete problem-solving approach that I followed.
One habit I've been developing while learning Python is to avoid jumping directly into coding. Before writing a single line of code, I try to understand the problem, think about the logic, sketch a simple flowchart, and perform a few manual calculations. Once the logic is clear, I start implementing the solution in Python.
.
However, my learning process doesn't stop after the code works.
After solving a problem successfully, I ask myself a few important questions:
Can this solution be written more clearly?
Can the number of iterations be reduced?
Is there a more efficient approach?
What is the time complexity of my current solution?
How would an experienced developer solve the same problem?
These questions help me move beyond simply writing working code and focus on writing better code.
Throughout this article, you'll notice that some problems are solved in multiple ways. I intentionally included both the beginner-friendly approaches and the optimized versions because I believe understanding the evolution of a solution is just as valuable as understanding the final answer.
So, let's dive into the first three problems and explore the logic, flowcharts, implementations, and optimizations step by step.
Recently, I worked on two beginner-friendly problems:
- Finding the sum of the first N natural numbers.
- Finding the sum of prime numbers up to N.
At first, I wrote simple solutions using loops. Later, I tried optimizing them to understand how programmers make code more efficient.
In this article, I'll share both approaches, the flowcharts behind them, and the lessons I learned while improving the solutions.
Problem 1: Sum of First N Natural Numbers
Suppose:
N = 5
1 + 2 + 3 + 4 + 5 = 15
Our goal is to calculate this sum.
Approach 1: Using a While Loop
This was my first thought.
Start from 1, keep adding numbers until N is reached.
Code
N = 5
total_sum = 0
num = 1
while num <= N:
total_sum += num
num += 1
print(total_sum)
Flowchart
Start
β
βΌ
N = 5
sum = 0
num = 1
β
βΌ
num <= N ?
β
ββ΄β
βYes
βΌ
sum = sum + num
num = num + 1
β
βββββββββββββββ
β
βΌ
num <= N ?
β
No
β
βΌ
Print Sum
β
βΌ
End
Time Complexity
O(N)
If N becomes larger, the loop must run more times.
For example:
N = 10 β 10 iterations
N = 100 β 100 iterations
N = 100000 β 100000 iterations
Approach 2: Using a For Loop
After learning for loops, I realized the same logic could be written more cleanly.
Code
N = 5
total_sum = 0
for i in range(1, N + 1):
total_sum += i
print(total_sum)
Flowchart
Start
β
βΌ
sum = 0
β
βΌ
for i = 1 to N
β
βΌ
sum = sum + i
β
βΌ
Next i
β
βΌ
Print Sum
β
βΌ
End
What Changed?
The logic is exactly the same.
The only difference is readability.
The for loop automatically handles incrementing the counter.
Time Complexity
O(N)
Still the same.
Approach 3: Using a Mathematical Formula
After writing both loop-based solutions, I asked myself:
"Do I really need to visit every number?"
Then I discovered a famous formula:
Sum = N Γ (N + 1) / 2
Code
N = 5.
total_sum = N * (N + 1) // 2
print(total_sum)
Flowchart
Start
β
βΌ
Read N
β
βΌ
Apply Formula
β
βΌ
Print Sum
β
βΌ
End
Time Complexity
O(1)
This is constant time.
Whether N is:
10
100
1000
1000000
the computer performs the same number of operations.
Why Did I Move to This Approach?
My while loop worked.
My for loop worked.
But both had one limitation:
More numbers = More iterations
The formula completely removes the loop.
This was my first lesson in optimization:
The fastest loop is often the one you don't need to execute.
Problem 2: Sum of Prime Numbers Up To N
Now things became more interesting.
Suppose:
N = 11
Prime numbers are:
2 3 5 7 11
Their sum is:
28
First Attempt: Basic Prime Number Logic
My first idea was simple.
For every number:
- Check if it is prime.
- If prime, add it to the sum.
Code
N = 11
prime_sum = 0
num = 2
while num <= N:
is_prime = True
i = 2
while i * i <= num:
if num % i == 0:
is_prime = False
break
i += 1
if is_prime:
print(num)
prime_sum += num
num += 1
print(prime_sum)
Flowchart
Take a Number
β
βΌ
Check if Prime
β
βΌ
Prime?
β β
Yes No
β β
βΌ β
Add to Sum
β
βΌ
Next Number
Complexity
Approximately:
O(NβN)
What Problem Did I Notice?
While debugging and tracing the program, I noticed something.
For every number:
2
3
4
5
6
7
8
9
10
11
I was checking many numbers that obviously cannot be prime.
For example:
4
6
8
10
All even numbers.
Why waste time checking them?
This led me to think differently.
Optimized Prime Number Program
I made three improvements.
Improvement 1
Skip all even numbers.
Instead of:
2,3,4,5,6,7,8,9,10,11
I check:
3,5,7,9,11
Improvement 2
Skip even divisors.
Instead of:
2,3,4,5,6,7
I check:
3,5,7
Optimized Code
N = 11
prime_sum = 2
num = 3
while num <= N:
is_prime = True
i = 3
while i * i <= num:
if num % i == 0:
is_prime = False
break
i += 2
if is_prime:
print(num)
prime_sum += num
num += 2
print(prime_sum)
Flowchart
Start
β
βΌ
prime_sum = 2
num = 3
β
βΌ
num <= N ?
β
βΌ
Check Prime
β
βΌ
Prime?
β
ββ΄β
βYes
βΌ
Add to Sum
β
βΌ
num = num + 2
β
βΌ
Repeat
What I Learned
The biggest lesson wasn't Python syntax.
The biggest lesson was learning how programmers think.
My journey looked like this:
Working Code
β
βΌ
Cleaner Code
β
βΌ
Faster Code
β
βΌ
Smarter Logic
At first, I focused only on getting the correct answer.
Then I started asking:
- Can I reduce the number of loops?
- Can I skip unnecessary checks?
- Can I solve the problem mathematically?
- Can I make the code easier to read?
Those questions helped me understand optimization much better than simply memorizing syntax.
If you're a beginner, don't worry about writing the perfect solution immediately.
First make it work.
Then make it better.
That's where real learning begins.
Let's see tomorrow in another problem with solutions ...
Top comments (0)