Add sqrt search example

This commit is contained in:
baz 2024-02-28 17:05:53 +00:00
parent 159c720e49
commit 9da6d99c5c
1 changed files with 35 additions and 0 deletions

35
sqrt/main.c Normal file
View File

@ -0,0 +1,35 @@
#include "stdbool.h"
#include <stdio.h>
#include "math.h"
int main(int argc, char *argv[])
{
bool arr[] = { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true };
double size = sizeof(arr) / sizeof(arr[0]);
double sq = sqrt(size);
sq = floor(sq);
int i = 0;
for (; i < (int)size; i += (int)sq) // Find the first breakage
{
if (arr[i])
{
i -= sq;
break;
}
}
int j = i + (int)sq;
for (; i < (int)size && i <= j; i++)
{
if (arr[i])
{
printf("%d", i);
break;
}
}
return 0;
}