1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <stdint.h>
uint64_t _pow(uint64_t n1, uint64_t n2) {
uint64_t result = 1;
for(int i = 0; i < n2; i++) {
result *= n1;
}
return result;
}
void _iterate_noops_helper(uint64_t n, int depth, uint64_t* a_num_invocations) {
*a_num_invocations += 1;
if(depth > 0) {
for(uint64_t i = 0; i < n; i++) {
_iterate_noops_helper(n, depth - 1, a_num_invocations);
}
}
}
int iterate_noops(uint64_t n) {
uint64_t num_invocations = 0;
_iterate_noops_helper(n, n, &num_invocations);
return num_invocations;
}
int main(int argc, char* argv[]) {
for(int n = 5; n < 10; n++) {
printf("___________________________\n");
printf("n == %d\n", n);
uint64_t num_invocations = iterate_noops(n);
printf("num_invocations == %lu\n", num_invocations);
printf("%d ^ %d == %lu\n", n, n, _pow(n, n));
printf("num_invocations / (%d ^ %d) == %.06f\n", n, n, (1.0 * num_invocations / _pow(n, n)));
}
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2023 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.