How many ways to express a natural number N as sum of two or more consecutive natural numbers

Problem Statement

This problem is asking us to find out the number of ways a natural number N can be expressed as a sum of two or more consecutive natural numbers. For example:

  • 5 can be expressed as 2+3. This gives us the count as 1.
  • 9 can be expressed as 4+5 and 2+3+4. This gives us the count as 2.
  • 8 cannot be expressed as sum of consecutive numbers, so this gives us the count as 0.

Solution

Let us express N as a sum of n consecutive numbers as follows:

N = a + (a+1) + (a+2) + ... + (a+n-1)

N = (2a + n - 1) * n / 2

N = (2an + n2 - n ) / 2

a = (2N - n2 + n ) / 2n

Now, if a is a positive number then RHS will also be positive. Loop until RHS is positive as every positive natural number value of RHS is one possible answer.

Example Code

public class Sample{
    public static void main(String[] args) {

        double N = 7;
        double count = 0;
        double n = 2;
        for(double i=2; i<10; i++) {

            n = i;
            double val = (2*N - n*n + n ) / ( 2*n );
            if(val - (int)val == 0 && val > 0) {

                count++;
            }
        }

        System.out.println("count is: " + count);
    }
}

This code returns the following output:

count is: 2.0

21 thoughts on “How many ways to express a natural number N as sum of two or more consecutive natural numbers

  1. My brother recommended I might like this web site. He was totally right. This post actually made my day. You cann’t imagine just how much time I had spent for this information! Thanks!

  2. Thanks for another informative website. Where else could I get that type of information written in such a perfect way? I’ve a project that I’m just now working on, and I’ve been on the look out for such information.

  3. I absolutely love your blog and find almost all of your post’s to be just what I’m looking for. Does one offer guest writers to write content to suit your needs? I wouldn’t mind writing a post or elaborating on some of the subjects you write regarding here. Again, awesome site!

  4. I am now not sure where you’re getting your info, however good topic. I needs to spend some time learning much more or figuring out more. Thanks for magnificent information I was in search of this info for my mission.

  5. Terrific work! This is the type of info that should be shared around the net. Shame on the search engines for not positioning this post higher! Come on over and visit my site . Thanks =)

  6. I will right away grab your rss as I can not find your email subscription hyperlink or newsletter service. Do you have any? Please allow me understand in order that I could subscribe. Thanks.

  7. I precisely needed to appreciate you all over again. I’m not certain what I would have sorted out in the absence of the actual tactics discussed by you on that field. It was actually a very difficult concern in my opinion, nevertheless noticing this specialised way you dealt with it made me to cry for joy. I will be happier for this service and as well , sincerely hope you find out what a powerful job you were undertaking educating others using a site. Most probably you’ve never come across all of us.

  8. I simply could not leave your site before suggesting that I actually loved the standard info a person supply on your guests? Is going to be again steadily in order to investigate cross-check new posts

  9. Hey there! I’ve been reading your site for a while now and finally got the courage to go ahead and give you a shout out from Austin Tx! Just wanted to tell you keep up the excellent job!

  10. Just wish to say your article is as surprising. The clearness for your put up is simply nice and i could assume you are a professional on this subject. Fine with your permission let me to grab your feed to stay up to date with impending post. Thank you one million and please continue the rewarding work.

  11. It’s a shame you don’t have a donate button! I’d certainly donate to this brilliant blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to brand new updates and will share this blog with my Facebook group. Chat soon!

  12. Thanks for the auspicious writeup. It in truth was a amusement account it. Glance complicated to more brought agreeable from you! However, how can we communicate?

  13. We’re a group of volunteers and starting a brand new scheme in our community. Your site offered us with useful info to paintings on. You’ve performed a formidable job and our entire group might be thankful to you.

  14. My husband and i ended up being thrilled when Albert managed to conclude his investigations through the precious recommendations he had from your own blog. It’s not at all simplistic to simply continually be handing out helpful tips that many some people might have been making money from. And we also already know we need the writer to appreciate for that. The illustrations you made, the simple blog menu, the relationships your site give support to instill – it’s all awesome, and it’s helping our son in addition to us imagine that that concept is amusing, which is certainly really mandatory. Thank you for everything!

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top