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

1 Answer

0 votes
public class NextInSeries {

    /**
    25 - 14 = 11
    47 - 25 = 22
    91 - 47 = 44
    
    11 -> 22 -> 44 -> 88
    
    91 + 88 = 179
    */

    /**
        Compute the next number in the series using the rule:
        - Differences double each step.
        - For the sequence 14, 25, 47, 91:
              Differences: 11, 22, 44
              Next difference = 44 * 2 = 88
              Next value = 91 + 88 = 179
    */
    public static int nextInSeries(int[] arr) {
        if (arr.length < 2) {
            throw new IllegalArgumentException("Not enough data to compute next value.");
        }

        // Last difference
        int lastDiff = arr[arr.length - 1] - arr[arr.length - 2];

        // Apply doubling rule
        return arr[arr.length - 1] + lastDiff * 2;
    }

    public static void main(String[] args) {
        int[] series = {14, 25, 47, 91};

        int next = nextInSeries(series);
        System.out.println("Next number in the series: " + next);
    }
}



/*
run:

Next number in the series: 179

*/

 



answered 6 days ago by avibootz
...