72 lines
1.4 KiB
C
72 lines
1.4 KiB
C
//Ex 1:
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
|
|
|
|
int racineCarree(long long x) {
|
|
if (x < 0) return -1;
|
|
long long i = 0;
|
|
while (i * i <= x) {
|
|
if (i * i == x) return (int)i;
|
|
i++;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
|
|
int* racineCarreeTab(const long long *arr, size_t n) {
|
|
int *out = malloc(n * sizeof(int));
|
|
if (!out) return NULL;
|
|
for (size_t i = 0; i < n; i++) {
|
|
out[i] = racineCarree(arr[i]);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
static void print_int_tab(const int *a, size_t n) {
|
|
printf("[");
|
|
for (size_t i = 0; i < n; i++) {
|
|
printf("%d", a[i]);
|
|
if (i + 1 < n) printf(", ");
|
|
}
|
|
printf("]\n");
|
|
}
|
|
|
|
int main(void) {
|
|
{
|
|
long long x = 9;
|
|
printf("RacineCarree(9) ->", x);
|
|
|
|
long long tab1[] = {81,58,9};
|
|
int *r1 = racineCarreeTab(tab1, 3);
|
|
printf("racineCarreeTab([81,58,9]) -> ");
|
|
print_int_tab(r1, 3);
|
|
free(r1);
|
|
|
|
long long tab2[] = {25,48,2};
|
|
int *r2 = racineCarreeTab(tab2, 3);
|
|
printf("racineCarreeTab([25,48,2]) -> ");
|
|
print_int_tab(r2, 3);
|
|
free(r2);
|
|
|
|
size_t n = 10000;
|
|
long long *test = malloc(n * sizeof(long long));
|
|
if (!test) return 1;
|
|
|
|
srand(42);
|
|
for (size_t i = 0; i < n; i++) {
|
|
|
|
test[i] = 1000001LL + (rand() % 2000000);
|
|
}
|
|
int *racine_test = racineCarreeTab(test, n);
|
|
free(racine_test);
|
|
free(test);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|