# -*- coding: utf-8 -*-

"""
Multiples of 3 and 5
Problem 1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.
"""

import sys
import argparse

def imperative_sum(limit):
    def _divisible_by_3_or_5(num):
        return num % 3 == 0 or num % 5 == 0

    total = 0
    for number in xrange(limit):
        if _divisible_by_3_or_5(number):
            total += number

    return total

def functional_sum(limit):
    return sum([ number for number in xrange(limit) \
                 if number % 3 == 0 or number % 5 == 0 ])

def main(argv):
    description = "Calculate the sum of all numbers that are divisible by " \
            "either 3 or 5 up to a limit"
    parser = argparse.ArgumentParser(description=description)
    group = parser.add_mutually_exclusive_group()
    group.add_argument("-i", "--imperative",
            help="Use the imperative method",
            action="store_true")
    group.add_argument("-f", "--functional",
            help="Use the functional method",
            action="store_true")
    parser.add_argument("-l", "--limit",
            help="Indicate the upper limit of the list; defaults to 1000",
            type=int, default=1000)
    parser.add_argument("-v", "--verbose",
            help="Verbose mode",
            action="store_true")

    args = parser.parse_args()

    f = functional_sum if args.functional else imperative_sum
    msg = "The total is {}" if args.verbose else "{}"

    print msg.format( f(args.limit) )


if __name__ == "__main__":
    main(sys.argv)

@@ -296,24 +296,87 @@
 sum(limit):%0A
+    ''' Straightforward sum calculation via loop and check '''%0A
     def _div
@@ -398,16 +398,16 @@
 5(num):%0A
-
         
@@ -613,115 +613,476 @@
     
-return sum(%5B number for number in x
+''' Using functional programming concepts '''%0A    return sum( set( range(0, limit, 3) + 
 range(
+0, 
 limit
-) %5C%0A                 if number %25 3 == 0 or number %25 5 == 0 %5D)
+, 5) ))%0A%0Adef algebraic_sum(limit):%0A    ''' Calculates the sum by exploiting the fact that the sum of consecutive%0A        integers 1 ... n equals 0.5 * n(n+1) '''%0A    def _sum_1_to_n(n):%0A        return (0.5) * n * (n+1)%0A%0A    total = ( 3 * _sum_1_to_n(limit/3)) + %5C%0A            ( 5 * _sum_1_to_n(limit/5)) - %5C%0A            (15 * _sum_1_to_n(limit/15))%0A%0A    return total
 %0A%0Ade
@@ -1496,16 +1496,16 @@
 ional%22,%0A
-
         
@@ -1571,24 +1571,146 @@
 tore_true%22)%0A
+    group.add_argument(%22-a%22, %22--algebraic%22,%0A            help=%22Use the algebraic method%22,%0A            action=%22store_true%22)%0A
     parser.a

Add algebraic method, and rewrite functional method using range and sets