Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: pbevin/euler
base: c3d3cec6c5
...
head fork: pbevin/euler
compare: e0a80c18ea
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 105 additions and 0 deletions.
  1. +105 −0 euler.c
View
105 euler.c
@@ -309,6 +309,18 @@ int divisors(int n) {
return count;
}
+/* Add up the divisors of n, not counting n itself.
+ * Same trick as divisors().
+ */
+int sumdivisors(int n) {
+ int sum = 1, i;
+ for (i = 2; i * i < n; i++) {
+ if (n % i == 0) sum += i + n/i;
+ }
+ if (i * i == n) sum += i;
+ return sum;
+}
+
void eu012(char *ans) {
int n = 0;
@@ -346,6 +358,16 @@ void add(char *dst, char *src, int n) {
}
}
+void mul1(char *dst, int a, int n) {
+ int c = 0;
+ for (int i = 0; i < n; i++) {
+ int d = dst[i] * a + c;
+ dst[i] = d % 10;
+ c = d / 10;
+ }
+}
+
+
void eu013(char *ans) {
FILE *fp = fopen("eu013.txt", "r");
char sum[100];
@@ -528,6 +550,86 @@ void eu018(char *ans) {
sprintf(ans, "%d", max);
}
+/*
+ * Jan 1, 1900 was a Monday
+ * Find Sundays on the first of the month from
+ * Jan 1, 1901 to
+ * Dec 31, 2000
+ *
+ * We use Sun => 0, Mon => 1, ..., Sat => 7.
+ */
+int countsun(int *days_in_month, int day, int *newday) {
+ // Return the number of Sundays in the year, and
+ // modify newday to be the day of the week on Jan 1
+ // of the next year.
+ int count = 0;
+ for (int i = 0; i < 12; i++) {
+ if (day == 0) count++;
+ day += days_in_month[i];
+ day %= 7;
+ }
+ *newday = day;
+ return count;
+}
+
+void eu019(char *ans) {
+ int day;
+ int count;
+ int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
+ int daysly[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
+
+ /* 1900 was NOT a leap year, but 2000 was. */
+ countsun(days, 1, &day);
+ count = 0; /* Not counting 1900 */
+ for (int yr = 1901; yr <= 2000; yr++) {
+ if (yr % 4 == 0) {
+ count += countsun(daysly, day, &day);
+ } else {
+ count += countsun(days, day, &day);
+ }
+ }
+
+ sprintf(ans, "%d", count);
+}
+
+/* Sum of digits of 1000! */
+void eu020(char *ans) {
+ const int N = 100;
+ char fact[1000];
+
+ memset(fact, 0, sizeof(fact));
+ fact[0] = 1;
+
+ for (int i = 2; i < N; i++) {
+ mul1(fact, i, 1000);
+ }
+
+ int t = 0;
+ for (int i = 0; i < 1000; i++) {
+ t += fact[i];
+ }
+ sprintf(ans, "%d", t);
+}
+
+/* Add up all amicable numbers under 10,000 */
+void eu021(char *ans) {
+ const int N = 10000;
+ int ds[N];
+
+ for (int i = 1; i < N; i++) {
+ ds[i] = sumdivisors(i);
+ }
+
+ int sum = 0;
+ for (int i = 1; i < N; i++) {
+ if (ds[i] < N && i != ds[i] && ds[ds[i]] == i) {
+ sum += i;
+ }
+ }
+
+ sprintf(ans, "%d", sum);
+}
+
typedef void (solver)(char *ans);
struct puzzle {
const char *name;
@@ -554,6 +656,9 @@ struct puzzle puzzles[] = {
{ "016", &eu016, "1366" },
{ "017", &eu017, "21124" },
{ "018", &eu018, "1074" },
+ { "019", &eu019, "171" },
+ { "020", &eu020, "648" },
+ { "021", &eu021, "31626" },
};
#define NPUZZLES (sizeof puzzles / sizeof(puzzles[0]))

No commit comments for this range

Something went wrong with that request. Please try again.