slovo/benchmarks/math-loop/c/math_loop.c
2026-05-22 08:38:43 +02:00

33 lines
679 B
C

#include <stdint.h>
#include <stdio.h>
#define LOOP_COUNT 1000000
#define EXPECTED_CHECKSUM 5000001
static int32_t math_loop(int32_t limit) {
int32_t i = 0;
int32_t acc = 1;
while (i < limit) {
acc = acc + ((i + 3) * 2);
acc = acc > 1000000000 ? acc - 1000000000 : acc;
i = i + 1;
}
return acc;
}
static int32_t configured_loop_count(void) {
int32_t value = LOOP_COUNT;
if (scanf("%d", &value) != 1 || value <= 0) {
return LOOP_COUNT;
}
return value;
}
int main(void) {
int32_t result = math_loop(configured_loop_count());
printf("%d\n", result);
return result == EXPECTED_CHECKSUM ? 0 : 1;
}