The DS Man

Learning data structures and algorithms are sometimes hard to grasp and sometimes are very boring…

Follow publication

Recursion problem — Sum of N natural numbers

Source: imgflip.com

Natural numbers are those numbers which start with 1. So, if we say the sum of 10 natural numbers, it means that we need to find the sum from 1 to 10.

If you’re not aware, this is the sixth article of the recursion series. Previous articles were about the concept of recursion and some snippet output exercises. Get the complete list of the series here: https://medium.com/the-ds-man/a-series-on-recursion-478aa3d3d88

Problem

For a given input N, write a recursive function which will compute the sum of N natural numbers.

Don’t move onto solution until you try this yourself. I would suggest to draw diagrams like we did in previous articles. It will help to think clearly.

Solution

This problem can be solved via a simple formula with O(1) complexity. But the purpose of introducing this problem is to solve it via recursion to make the topic more clear.

Think of a base case. Since the sum we have to find is of natural numbers, so if the number reaches 1, we can return 1. And then we can add the values together.

Go solution

Beep Boop. Boop Beep. Sorry to interrupt. But we are building a very large developer community on Discord with thousands of developers from all around the world with all levels. We are just started out and hoping to make it big.

This Discord server will have all the knowledge you will ever need to become a great programmer. You will learn about technologies you have never heard of. There will be projects for all levels, jobs posting, conferences, events and a lot more. We will also post daily problems on it, so that you can stay on the top on problem solving.

There will be lots of tutorials, live coding etc. I would request to kindly join and make yourself the part of biggest developer community. It’s a start and it will eventually grow. It’s a request not to leave it after joining if there’s not much engaging content at that point of time. But, we are working round the clock to make it happen.

Please join here: https://discord.gg/kzE8kBvCyG

Java solution

That’s all for this problem. If you have done it yourself, it’s great. If not, don’t worry. We will do a lot more.

Read further

This was the sixth article of recursion series. You can find the list of complete recursion series here: https://medium.com/the-ds-man/a-series-on-recursion-478aa3d3d88

That’s all Folks !!!

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

The DS Man
The DS Man

Published in The DS Man

Learning data structures and algorithms are sometimes hard to grasp and sometimes are very boring. Learn all of them easily with articles written specially for humans and are very easy to understand.

Tejaswi Kasat
Tejaswi Kasat

Written by Tejaswi Kasat

Love programming, music and coffee.

No responses yet

Write a response