diff --git a/README.md b/README.md index 6c8e7f1..e776217 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,17 @@ -#Math Tools +# Math Tools -##Overview +## Overview This project builds some basic math functions for the calculus of series. -##How to use it +## How to use it Just import the mathtools module. Each function is described in the code comments. -###Functions to be implemented: +### Functions to be implemented: * `isPrime`: tells if a number is prime. * `factorial`: Calculates the factorial of a number. * `fib`: Calculates the n value of the fibonacci sequence. * `geometric`: Calculates the sum of a geometric serie. * `arithmetic`: Calculates the sum of an arithmetic serie. -##How to test this -Run the test.py \ No newline at end of file +## How to test this +Run the test.py diff --git a/mathtools.py b/mathtools.py index 58081fa..54e6d70 100644 --- a/mathtools.py +++ b/mathtools.py @@ -10,3 +10,35 @@ def isPrime(n): if n % i == 0: return False return True +def factorial(n): + '''Returns the factorial of a number''' + if n == 0: + return 1 + else: + return n * factorial(n-1) +def arithmetic(a, difference, n): + '''Calculates the sum of a arithmetic serie of n elements. + An arithmetic sequence is of the form: a, a+d, a+2d, a+3d,... + n is the number of elements in the sequence.''' + #Get the arithmetic sequence + sequence = [a+difference*x for x in range(n)] + #Calculates its sum + return sum(sequence) +def fib(n): + ''' Calculates the n value of the fibonacci sequence''' + if n == 0: + return 0 + elif n == 1: + return 1 + else: + return fib(n-1)+fib(n-2) +def geometric(a, ratio, n): + '''Calculates the sum of a geometric serie of n elements. + A geometric sequence is of the form: a, a*r, a*r*r, a*r*r*r,... + n is the number of elements in the sequence.''' + #Use the sum formula: + return a*(1-ratio**n)/(1-ratio) + #Get the geometric sequence + sequence = [a*(ratio**x) for x in range(n)] + #Calculates its sum + return sum(sequence) diff --git a/test.py b/test.py deleted file mode 100644 index ff90060..0000000 --- a/test.py +++ /dev/null @@ -1,62 +0,0 @@ -import mathtools - -def test_isPrime(): - fails = 0 - print "Testing isPrime" - primes = [2, 3, 5, 7, 983, 991, 997] - not_primes = [4, 6, 8, 9, 993, 994, 995, 999] - for prime in primes: - if mathtools.isPrime(prime): - print '+ ', # Pass the test - else: - fails += 1 - print '- ', - for not_prime in not_primes: - if mathtools.isPrime(not_prime): - fails += 1 - print '- ', - else: - print '+ ', # Pass the test - - if not fails: - print "TEST OK" - else: - print ("FOUND ", fails, " ERRORS") - - -def test_factorial(): - print "Testing factorial " - fails = 0 - factorials = [(5, 120), (10, 3628800), (7, 5040), (12, 479001600)] - for f in factorials: - if mathtools.factorial(f[0]) == f[1]: - print '+ ', # Pass the test - else: - fails += 1 - print '- ', - if not fails: - print "TEST OK" - else: - print "FOUND ", fails, " ERRORS" - -def test_fib(): - print "Testing fib " - fails = 0 - fib = [(1, 1), (0, 0), (3, 2), (4, 3), (6, 8), (7, 13), (20, 6765)] - for f in fib: - if mathtools.fib(f[0]) == f[1]: - print '+ ', # Pass the test - else: - fails += 1 - print '- ', - if not fails: - print "TEST OK" - else: - print "FOUND ", fails, " ERRORS" - -functions = ('isPrime', 'factorial', 'fib') -for f in functions: - if hasattr(mathtools, f): - globals()['test_'+f]() - else: - print "*", f, "IS NOT DEFINED" \ No newline at end of file