Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementation for nth_term in SML. #92

Merged
merged 2 commits into from
Oct 4, 2017
Merged
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/bin/

# OS X
*.DS_Store

33 changes: 33 additions & 0 deletions algebra/arithmetic_progression/sml/arithmetic_progression.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(*
* Brief: Calculates the nth term in an arithmetic sequence.
*
* Note, we start the sequence from n = 0 (i.e. the first number in the
* sequence is when n = 0).
*
* Requires: n >= 0
*
* Param a: starting value
* Param d: difference between each term
* Param n: number in the sequence we want to get
*
* Return: The nth number in an arithmetic sequence.
*)
fun nth_term (a: int) (d: int) (n: int): int =
if n = 0
then a
else d + nth_term a d (n - 1)

(*
* Brief: Calculates the sum of the first n numbers in an arithemetic sequence.
*
* Requires: n >= 0
*
* Param a: starting value
* Param d: difference between each term
* Param n: number of terms we want to add
*
* Return: The sum of the first n numbers in an arithmetic sequence.
*)
fun sum_of_first_n (a: int) (d: int) (n: int): int =
(n * (2 * a + (n - 1) * d)) div 2