How to find the next number in the series 14, 25, 47, 91 with C++

2 Answers

0 votes
/*
25 = 14 * 2 - 3
47 = 25 * 2 - 3
91 = 47 * 2 - 3

91 * 2 - 3 = 179
*/

#include <iostream>
#include <vector>
#include <optional>

/*
    The next number in a sequence using a simple rule:
    next = previous * 2 - 3
*/
std::optional<int> nextInSeries(const std::vector<int>& seq) {
    if (seq.size() < 1) {
        return std::nullopt; // no data → no prediction
    }

    int last = seq.back();
    int next = last * 2 - 3; // inferred rule
    return next;
}

int main() {
    std::vector<int> series = {14, 25, 47, 91};

    if (auto next = nextInSeries(series)) {
        std::cout << "Next number is: " << *next << "\n";
    } else {
        std::cout << "Cannot determine next number.\n";
    }
}


/*
run:

Next number is: 179

*/

 



answered 6 days ago by avibootz
0 votes
/*
25 - 14 = 11
47 - 25 = 22
91 - 47 = 44

11 -> 22 -> 44 -> 88

91 + 88 = 179
*/

#include <iostream>
#include <vector>

/*
    Compute the next number in the series using the rule:
    Differences double each step:
        11 → 22 → 44 → 88 → ...
    So next = last + (last_difference * 2)
*/
int nextInSeries(const std::vector<int>& seq) {
    // Need at least two numbers to compute a difference
    if (seq.size() < 2) {
        throw std::runtime_error("Not enough data to infer next value.");
    }

    // Compute the last difference
    int lastDiff = seq.back() - seq[seq.size() - 2];

    // Apply the doubling rule
    int next = seq.back() + lastDiff * 2;

    return next;
}

int main() {
    std::vector<int> series = {14, 25, 47, 91};

    int next = nextInSeries(series);
    
    std::cout << "Next number is: " << next << "\n";
}



/*
run:

Next number is: 179

*/

 



answered 6 days ago by avibootz
...