aboutsummaryrefslogtreecommitdiff
path: root/challenge-175
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-175')
-rw-r--r--challenge-175/walt-mankowski/c/Makefile18
-rw-r--r--challenge-175/walt-mankowski/c/ch-1.c36
-rw-r--r--challenge-175/walt-mankowski/c/ch-2.c38
3 files changed, 92 insertions, 0 deletions
diff --git a/challenge-175/walt-mankowski/c/Makefile b/challenge-175/walt-mankowski/c/Makefile
new file mode 100644
index 0000000000..d1cec8a239
--- /dev/null
+++ b/challenge-175/walt-mankowski/c/Makefile
@@ -0,0 +1,18 @@
+C = /usr/bin/cc
+CFLAGS = -Wall -O3
+
+all: ch-1 ch-2
+
+%.o: %.cpp
+ $(CPP) $(CPPFLAGS) -c $<
+
+ch-1: ch-1.o
+ $(C) -o $@ ch-1.o
+
+ch-2: ch-2.o
+ $(C) -o $@ ch-2.o
+
+clean:
+ rm -f *~
+ rm -f *.o
+ rm -f ch-1 ch-2
diff --git a/challenge-175/walt-mankowski/c/ch-1.c b/challenge-175/walt-mankowski/c/ch-1.c
new file mode 100644
index 0000000000..346c8fdb0b
--- /dev/null
+++ b/challenge-175/walt-mankowski/c/ch-1.c
@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+const bool is_leap_year(const int year) {
+ if (year % 4)
+ return false;
+ else if (year % 100)
+ return true;
+ else if (year % 400)
+ return false;
+ else
+ return true;
+}
+
+int main(int argc, char *argv[]) {
+ const int days_in_month[2][12] = {
+ { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
+ { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
+ };
+
+ const int year = atoi(argv[1]);
+ const int leap = is_leap_year(year);
+ for (int month = 0; month < 12; month++) {
+ struct tm ts;
+ memset(&ts, 0, sizeof(ts));
+ ts.tm_mon = month;
+ ts.tm_year = year - 1900;
+ ts.tm_mday = days_in_month[leap][month];
+ ts.tm_isdst = -1;
+ mktime(&ts);
+ printf("%d-%02d-%2d\n", ts.tm_year + 1900, month+1, ts.tm_mday - ts.tm_wday);
+ }
+}
diff --git a/challenge-175/walt-mankowski/c/ch-2.c b/challenge-175/walt-mankowski/c/ch-2.c
new file mode 100644
index 0000000000..81d217dc81
--- /dev/null
+++ b/challenge-175/walt-mankowski/c/ch-2.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+const int gcd(const int a, const int b) {
+ return b ? gcd(b, a % b) : a;
+}
+
+int main(int argc, char *argv[]) {
+ const int MAX = 6000;
+ int *totient = calloc(MAX, sizeof(int));
+
+ for (int n = 2; n <= MAX; n++)
+ for (int i = 1; i <= n-1; i++)
+ if (gcd(n, i) == 1)
+ totient[n]++;
+
+ int perfect[20];
+ int i = 2;
+ int n = 0;
+ while (n < 20 && i < 6000) {
+ int tot = totient[i];
+ int sum = tot;
+ while (tot != 1) {
+ if (sum > i)
+ break;
+ else {
+ tot = totient[tot];
+ sum += tot;
+ }
+ }
+ if (sum == i)
+ perfect[n++] = i;
+ i++;
+ }
+ for (int i = 0; i < 20; i++)
+ printf("%d ", perfect[i]);
+ printf("\n");
+}