diff options
| author | deadmarshal <adeadmarshal@gmail.com> | 2023-01-04 09:44:53 +0330 |
|---|---|---|
| committer | deadmarshal <adeadmarshal@gmail.com> | 2023-01-04 09:44:53 +0330 |
| commit | dc6e6dc4252027b60ebd935abb1a53ce46145989 (patch) | |
| tree | dba808345b6296ac45e7e53f7ee7046d503965d4 /challenge-198/deadmarshal/c | |
| parent | ee249218f373166edca2b95144a9b0b59e200e05 (diff) | |
| download | perlweeklychallenge-club-dc6e6dc4252027b60ebd935abb1a53ce46145989.tar.gz perlweeklychallenge-club-dc6e6dc4252027b60ebd935abb1a53ce46145989.tar.bz2 perlweeklychallenge-club-dc6e6dc4252027b60ebd935abb1a53ce46145989.zip | |
TWC198
Diffstat (limited to 'challenge-198/deadmarshal/c')
| -rw-r--r-- | challenge-198/deadmarshal/c/ch-1.c | 37 | ||||
| -rw-r--r-- | challenge-198/deadmarshal/c/ch-2.c | 30 |
2 files changed, 67 insertions, 0 deletions
diff --git a/challenge-198/deadmarshal/c/ch-1.c b/challenge-198/deadmarshal/c/ch-1.c new file mode 100644 index 0000000000..4b6678f4bc --- /dev/null +++ b/challenge-198/deadmarshal/c/ch-1.c @@ -0,0 +1,37 @@ +#include<stdio.h> +#include<stdlib.h> + +int compare(const void *a, const void *b) +{ + int i1 = *(int*)a; + int i2 = *(int*)b; + return i1 - i2; +} + +int max_gap(int *arr, size_t sz) +{ + if(sz < 2) return 0; + qsort(arr,sz,sizeof(int),compare); + size_t i = 0; + int max = 0, count = 0, temp = 0; + while(i < sz) + { + temp = abs(arr[i] - arr[i+1]); + if(temp > max) max = temp; + i += 2; + } + for(i = 0; i < sz-1; ++i) + if(abs(arr[i] - arr[i+1]) == max) count++; + return count; +} + +int main(void) +{ + int a1[] = {2,5,8,1}; + int a2[] = {3}; + size_t sz1 = 4, sz2 = 1; + printf("%d\n",max_gap(a1,sz1)); + printf("%d\n",max_gap(a2,sz2)); + return 0; +} + diff --git a/challenge-198/deadmarshal/c/ch-2.c b/challenge-198/deadmarshal/c/ch-2.c new file mode 100644 index 0000000000..98b1245243 --- /dev/null +++ b/challenge-198/deadmarshal/c/ch-2.c @@ -0,0 +1,30 @@ +#include<stdio.h> + +int is_prime(int n) +{ + int i = 5; + if((n == 2) || (n == 3)) return 1; + if((n <= 1) || (n % 2 == 0) || (n % 3 == 0)) return 0; + while(i * i <= n) + { + if((n % i == 0) || (n % (i+2) == 0)) return 0; + i += 6; + } + return 1; +} + +int prime_count(int n) +{ + int count = 0; + for(int i = 1; i < n; ++i) if(is_prime(i)) count++; + return count; +} + +int main(void) +{ + printf("%d\n", prime_count(10)); + printf("%d\n", prime_count(15)); + printf("%d\n", prime_count(1)); + printf("%d\n", prime_count(25)); +} + |
