#include <stdio.h>
#include <math.h> // for cbrt() and round()
/*
This program finds the next number in a sequence of perfect cubes.
The input series is: 1, 8, 27, 64, 125, 216
These values correspond to:
1^3, 2^3, 3^3, 4^3, 5^3, 6^3
The next value is 7^3 = 343.
The approach:
- Verify each number is a perfect cube.
- Extract the cube root of the last number.
- Compute (root + 1)^3 to get the next number.
*/
// Check whether a number is a perfect cube.
// Uses cbrt() and rounding to verify the cube root.
int isPerfectCube(int value) {
double root = cbrt((double)value);
int rounded = (int)round(root);
return rounded * rounded * rounded == value;
}
// Compute the next cube in the series.
int nextCubeInSeries(const int *series, int length) {
int lastValue = series[length - 1];
// Compute the integer cube root of the last value.
int lastRoot = (int)round(cbrt((double)lastValue));
// The next number is (lastRoot + 1)^3.
int next = (lastRoot + 1) * (lastRoot + 1) * (lastRoot + 1);
return next;
}
int main(void) {
int series[] = {1, 8, 27, 64, 125, 216};
int length = sizeof(series) / sizeof(series[0]);
// Validate the pattern before computing the next value.
for (int i = 0; i < length; ++i) {
if (!isPerfectCube(series[i])) {
printf("Series contains a non-cube value.\n");
return 1;
}
}
int nextValue = nextCubeInSeries(series, length);
printf("Next number in the series: %d\n", nextValue);
return 0;
}
/*
run:
Next number in the series: 343
*/