Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7658977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:16:18+00:00 2026-05-31T13:16:18+00:00

We were given a simple task to come up with the most efficient way

  • 0

We were given a simple task to come up with the most efficient way we can to sum all the numbers between a start and end point (‘from’ and ‘to’) using recursion and iteration respectively, without using the obvious formula which would be O(1).

There is no application for this, I am simply curious and challenged to see if my solution can be improved / polished more than it already is:

/* recursion */
unsigned int sum1(unsigned int from, unsigned int to) {
    if (to - from < 2)
        return from + (from == to ? 0 : to);
    else
        return from + to + sum1(from + 1, to - 1);
}

/* iteration */
unsigned int sum2(unsigned int from, unsigned int to) {
    int p = to - from;
    if (p == 0) return from;
    int i, s, n = p / 2;
    if (p % 2 == 0) s = n + from;
    else {
        s = 0;
        n++;
    }
    for (i = 0; i < n; i++) {
        s += from++ + to--;
    }
    return s;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:16:19+00:00Added an answer on May 31, 2026 at 1:16 pm

    I tried improving the iterative version:

    unsigned int sum2_improved(unsigned int from, unsigned int to) {
        int p = to - from;
        if (p == 0) return from;
        int x = to + from;
        int s = 0;
        int i;
        for (i = p >> 1; i > 0; i--)
        {
            s += x;
        }
        s += (p % 2 == 0) ? x >> 1 : x;
        return s;
    }
    

    I tested your version with:

    for (i = 0; i < 9999999; i++) sum2(1,999);
    

    This is what I see:

    $ time ./addm
    real    0m18.315s
    user    0m18.220s
    sys     0m0.015s
    

    I tried my implementation with the same number of loops. Here’s how the improved function performed:

    $ time ./addm
    real    0m14.196s
    user    0m14.070s
    sys     0m0.015s
    

    UPDATE

    In my implementation x = to + from is the sum of the first and the last number in the sequence. If you consider any consecutive sequence of integers, and sum the first and last, the second and the penultimate, and so on … all these sum up to the same value. For example, in (1 ... 6), 1 + 6 = 2 + 5 = 3 + 4 = 7. However, with a sequence containing odd number of elements, you are left with the middle number which you will then have to add to the cumulative sum (that’s what the assignment following the for loop was doing.

    Also, note that this is still O(n). I realized after I initially posted my answer that my approach can actually be done in constant time. Here’s the updated code:

    unsigned int sum0(unsigned int from, unsigned int to) {
        int p = to - from;
        if (p == 0) return from;
        int x = to + from;
        int s = 0;
    
        s += (x * (p >> 1));
    
        s += (p % 2 == 0) ? x >> 1 : x;
    
        return s;
    }
    

    I ran this with the same number of loops as the earlier tests. Here’s what I saw:

    $ time ./addm
    
    real    0m0.158s
    user    0m0.093s
    sys     0m0.047s
    

    I’m not sure if this can be considered a variation of the formula for your purposes. In any case, it was an interesting exercise for me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a simple namespaced route map.namespace :api do |api| api.resources :genres end how can
It's a simple task, but I can't make it work. Given the following XML:
Simple task: given that an article has many comments, be able to display in
I've been given the task to create a simple Silverlight chat box for two
I've been given a seemingly simple task. When a given URL is requested the
I've been given a task to convert a simple spreadsheet to a HTML form
I Have been given this simple task , I have this list where i
I have been given a task that can be simplified to this scenario: Customers
i Have been given this simple task , I want to connect to facebook
It's a well-known task, simple to describe: Given a text file foo.txt, and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.