DEV Community

Cover image for Sum of Natural Numbers - A First Step into Algorithms in Go
Ruben Alvarado
Ruben Alvarado

Posted on

Sum of Natural Numbers - A First Step into Algorithms in Go

Throughout my technical interview journey, I've observed a shortage of Go-based Data Structures and Algorithms (DSA) examples. As Go has become increasingly in-demand and I've been studying it extensively, I've decided to transition my DSA series from JavaScript/TypeScript to Go. The elegant syntax of Go makes this transition exciting—it feels like exploring a whole new world.

I'll start with a super basic algorithm to warm up: The Sum of Natural Numbers. Join me as we explore this simple yet effective algorithm.

Disclaimer: This post assumes you have basic knowledge of Go and are preparing for job interviews.

Problem Statement

Given a positive integer n return the sum of all natural numbers from 1 to n .

Breaking down the problem statement, we have: an input (a positive integer), the required logic (sum all natural numbers), and an output (the resulting integer from the sum). Let's use this structure to define our algorithm.

How to Solve it

If you've read my previous posts about DSA, you know I prefer a straightforward approach: explaining the problem statement first, then the algorithm to solve it, and finally the implementation.

Now, let's continue. The first step is to handle the input. We need to validate if it's a positive number since this is a constraint in the problem statement. This is a basic validation: if the input number (n) is less than 0, it means it's not positive, so we return 0.

if n < 0 { return 0 } 
Enter fullscreen mode Exit fullscreen mode

Now, declare a sum variable where we'll accumulate the value of every number.

sum := 0 
Enter fullscreen mode Exit fullscreen mode

Thanks to the elegance of Go, we can create a loop using the range syntax. When we declare a variable i that iterates through the range of n, we're creating a loop that runs from 0 to n-1.

for i := range n {} 
Enter fullscreen mode Exit fullscreen mode

Go is such a beautiful language. It's important to note that this syntax only works with Go 1.22 or newer. With older versions, you'll need to use a conventional for loop instead.

for i := 0; i < n; i++ {} 
Enter fullscreen mode Exit fullscreen mode

Next, add the value of the current iteration plus 1 to our sum variable, since our loop starts from 0.

sum += i + 1 
Enter fullscreen mode Exit fullscreen mode

Once the loop concludes, you simply return the accumulated sum.

return sum; 
Enter fullscreen mode Exit fullscreen mode

Complete Implementation

The complete implementation of our solution for summing natural numbers is presented below. When reviewing this implementation, notice how concise yet readable the Go syntax is:

func sumNaturalNumbers(n int) int { if n < 0 { return 0 } sum := 0 for i := range n { sum += i + 1 } return sum } 
Enter fullscreen mode Exit fullscreen mode

And voila! You've just completed your first technical interview question using Go. While we haven't covered any data structures yet, this example serves as an excellent starting point for your Go DSA journey.

Final Thoughts

While this algorithm is simple, it effectively demonstrates Go's capabilities as a programming language. The elegant syntax and performance characteristics make Go particularly attractive for technical interviews and real-world applications.

There's a saying in the arts: "you need to know the rules to break them." Similarly, to excel in technical interviews, you need strong fundamentals. The best approach combines a clear starting point (like this basic algorithm), a solid plan for tackling more complex problems, and consistent discipline in practice. I hope this example helps you in your next technical interview and assists you in landing your desired job.

You can find the code for this and other algorithms in my GitHub repository: https://github.com/RubenOAlvarado/algorithms

Keep learning, and I'll see you in the next one!

Photo belongs to Susan Holt Simpson in Unsplash

Top comments (0)