Problem of the Day
A new programming or logic puzzle every Mon-Fri

Summation Calculator

Your goal today is to create a function that takes in a counter and a mathematical expression and outputs the summation from i=0 to i=counter-1. If it helps you can also pass in a function rather than a mathematical expression. Ex:

print summation(100, 'i') #or print summation(100, return_i)
>> 5050

Permalink: http://problemotd.com/problem/summation-calculator/

Comments:

  • Anonymous - 10 years, 4 months ago

    foldl1 haskell does some great work there

    run :: Int -> (Int -> Int -> Int) -> Int
    run x f = foldl1 f [1..x]
    

    reply permalink

  • Johnathan - 10 years, 4 months ago

    It got overly complex because my for loop was messing up, but this works.

    num = int(raw_input('Please enter a number and I will find the summation: '))
    x = 0
    i = 1
    
    while i <= num:
        x = x + int(i)
        i = i + 1
    
    print x
    raw_input('')
    

    reply permalink

  • Pufe - 10 years, 4 months ago

    Your example does not conform to your definition, if the range is exclusive (does not contain endpoints) then the sum should be 4950. My solution on emacs lisp uses an inclusive range to get the same answer as your example. It also evaluates the function at both endpoints (0 and i)

        (defun summation (i f)
          (if (< i 0)
              0
            (+ (funcall f i) (summation (- i 1) f))))
        (summation 100 'identity)
        ; >> 5050
    

    reply permalink

  • Nick Krichevsky - 10 years, 4 months ago

    Quick and dirty python solution

    i=100
    print sum(range(i))
    

    As noted by Pufe, your example does not show counter-1, but rather just counter. If you were to do just counter, the code would be

    i=100
    print sum(range(i+1))
    

    reply permalink

  • David - 10 years, 4 months ago

    Function Pointers are cool. I>o<I

    #include <stdlib.h>
    #include <stdio.h>
    
    int specialSumation (int counter, int (*mathExpr)(int) ) {
      int retVal = 0;
      for(int i = 0; i < counter; i++) {
        retVal += mathExpr(i);
      }
      return retVal;
    }
    
    int identity(int input) {
      return input;
    }
    
    int square(int input) {
      return input * input;
    }
    
    int history(int input) {
      static int past = 0;
      int retVal = input + (past % 3);
      past = input;
      return retVal;
    }
    
    //-------------------------------------------------------------------
    //--------------------------main loop--------------------------------
    //-------------------------------------------------------------------
    int main (int argc, char* argv[]) {
    
      int prnt = specialSumation(5, identity);
      printf("output: %i\n", prnt);
      prnt = specialSumation(5, square);
      printf("output: %i\n", prnt);
      prnt = specialSumation(5, history);
      printf("output: %i\n", prnt);
    
        return 0;
    }   //---------------------------------------------------------------
    
    
    

    I have included a couple use examples. The fact that you could pass in a mathematical expression seems to have been overlooked in the other answers. I opted to pass in a function, since I am not motivated to write a parser for math expressions.

    reply permalink

  • Anonymous - 10 years, 4 months ago

    I guess you should have used a more complicated mathematical expression in your example ...

    function printSummation(counter, expr) { var sum = 0; for (i=0; i<counter; i++) { var curExpr = expr.replace('i', i); sum += eval(curExpr); }

    return sum;
    

    }

    printSummation(2, 'i*2') 2 printSummation(3, 'i*2') 6 printSummation(3, 'i/2') 1.5

    I'm wondering, is this even possible in Java?

    reply permalink

  • Anonymous - 10 years, 4 months ago

    I think I'm the first person to post a O(1) solution - the rest have been O(n).

    def summation(to):
        n = to-1
        return n*(n+1) / 2
    
    >>> summation(100)
    4950.0
    

    This uses Gauss' method from the 1700s, using the observation that numbers can be paired (e.g. in summation(100) = 100+0 + 99+1 + 98+2...)

    reply permalink

Content curated by @MaxBurstein