There are several questions on here how to round a value to a known multiple. For example there is this question but that shows how to round a value to a specified multiple (e.g. round 9 to multiple of 5 will yield 10). I want to round a value up to the nearest factor of a given number. For example lets says I want to round up a value to the closest factor of 48.
Factors of 48: 1, 2, 3, 4, 6, 8, 12, 16, 24, 48
If my value is 9 I would want to round up to 12. The only way I know to do this is with brute force:
class Program {
static void Main(string[] args) {
const int clock = 48;
int value = 9;
while( value < clock && (clock % value) != 0) {
value++;
}
Console.WriteLine(value);
}
}
This works fine but it's not clever or efficient, at least I suspect that to be the case. Is there a better way to round a number up to a factor of a base number other than brute force?