1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/// Returns the common difference between two terms of an arithmetic progression
///
/// # Arguments
///
/// * `term` - Any term of the AP
/// * `previous_term` - The term before `term`
///
/// # Examples
///
/// ```
/// use ariprog;
/// let common_diff = ariprog::get_common_difference(10.0, 5.0); // Should return 5
/// let cd = ariprog::get_common_difference(12.0, 2.0); // should return 10
/// ```
pub fn get_common_difference(term: f32, previous_term: f32) -> f32 {
    term - previous_term
}

/// Returns the nth term of an arithmetic progression
///
/// # Arguments
///
/// * `first_term` - The first term of the AP
/// * `common_difference` - The common difference of the AP
/// * `nth_term_position` - The nth term position (nth) of the AP (e.g.: the twentieth term is in position 20)
///
/// # Examples
///
/// ```
/// use ariprog;
/// let nth_term = ariprog::get_nth_term(1.0, 2.0, 20.0); // Should return 39
/// ```
pub fn get_nth_term(first_term: f32, common_difference: f32, nth_term_position: f32) -> f32 {
    let nth_term = first_term + (nth_term_position - 1.0) * common_difference;

    nth_term
}

/// Returns the first term of an arithmetic progression
///
/// # Arguments
///
/// * `nth_term` - The nth term of the AP
/// * `common_difference` - The common difference of the AP
/// * `nth_term_position` - The nth term position (nth) of the AP (e.g.: the twentieth term is in position 20)
///
/// # Examples
///
/// ```
/// use ariprog;
/// let first_term = ariprog::get_first_term(-103.0, -2.0, 50.0); // Should return -5
/// ```
pub fn get_first_term(nth_term: f32, common_difference: f32, nth_term_position: f32) -> f32 {
    // an = a + (n - 1)d --> a = an + [(n - 1) * d * -1]
    let first_term = nth_term + ((nth_term_position - 1.0) * common_difference * -1.0);

    first_term
}

/// Returns an arithmetic progression with the first term + arithmetic means + nth_term, as a vector
///
/// # Arguments
///
/// * `how_many_in_between` - How many terms go between the first term and the nth term
/// * `first_term` - The first term of the AP
/// * `nth_term` - The nth term of the AP
///
/// # Examples
///
/// ```
/// use ariprog;
/// let ap = ariprog::insert_arithmetic_means(3, 1.0, 13.0); // Should return [1.0, 4.0, 7.0, 10.0, 13.0]
/// ```
pub fn insert_arithmetic_means(how_many_in_between: i32, first_term: f32, nth_term: f32) -> Vec<f32> {
    let how_many_terms = how_many_in_between as f32 + 2.0;
    let first_calc = nth_term - first_term;

    let common_difference = first_calc / (how_many_terms - 1.0);

    let mut counter = 0.0;
    let mut arithmetic_means = vec![];

    while counter < how_many_terms {
        arithmetic_means.push(first_term + (counter * common_difference));
        counter += 1.0;
    }

    arithmetic_means
}

/// Returns the number of terms in an AP
///
/// # Arguments
///
/// * `first_term` - The first term of the AP
/// * `nth_term` - The nth term of the AP
/// * `common_difference` - The common difference of the AP
///
/// # Examples
///
/// ```
/// use ariprog;
/// let n_of_terms = ariprog::get_how_many_terms(0.0, 100.0, 2.0); // Should return 51
/// ```
pub fn get_how_many_terms(first_term: f32, nth_term: f32, common_difference: f32) -> i32 {
    let how_many_terms = (nth_term - first_term + common_difference) / common_difference;

    how_many_terms as i32
}