Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
## How to test this
Run the test.py
32 changes: 32 additions & 0 deletions mathtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
62 changes: 0 additions & 62 deletions test.py

This file was deleted.