Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add problem3[0-9]

  • Loading branch information...
commit 893224cf72f7cd180bf4f357a67fb2ad092580d4 1 parent 810601a
@eagletmt authored
View
31 30-39/problem30.c
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2009, eagletmt
+ * Released under the MIT License <http://opensource.org/licenses/mit-license.php>
+ */
+
+#include <stdio.h>
+#include <math.h>
+
+#define M 1000000
+#define E 5
+
+int main(void)
+{
+ char buf[16];
+ int i;
+ int ttl = 0;
+ for (i = 10; i < M; i++) {
+ int sum = 0, j;
+
+ snprintf(buf, sizeof buf, "%d", i);
+ for (j = 0; buf[j]; j++) {
+ sum += pow(buf[j]-'0', E);
+ }
+ if (i == sum) {
+ ttl += i;
+ }
+ }
+ printf("%d\n", ttl);
+ return 0;
+}
+
View
27 30-39/problem31.c
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2009, eagletmt
+ * Released under the MIT License <http://opensource.org/licenses/mit-license.php>
+ */
+
+#include <stdio.h>
+
+int main(void)
+{
+ int z = 0;
+ int n = 200;
+
+ int a, b, c, d, e, f, g;
+ for (a = 0; a <= n; a += 200)
+ for (b = 0; b <= n; b += 100)
+ for (c = 0; c <= n; c += 50)
+ for (d = 0; d <= n; d += 20)
+ for (e = 0; e <= n; e += 10)
+ for (f = 0; f <= n; f += 5)
+ for (g = 0; g <= n; g += 2)
+ if (a+b+c+d+e+f+g <= 200)
+ z++;
+ printf("%d\n", z);
+
+ return 0;
+}
+
View
54 30-39/problem32.c
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2009, eagletmt
+ * Released under the MIT License <http://opensource.org/licenses/mit-license.php>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <string.h>
+
+static int compare(const void *a, const void *b);
+static int is_pandigital(char *buf);
+#define DIGITS(x) (lround(floor(log10((double)x))))
+
+int main(void)
+{
+ int product, multiplier, multiplicand, remaindar;
+ int sum = 0;
+ char buf[10];
+
+ for (product = 1111; product < 100000; product++) {
+ for (multiplicand = 2; multiplicand*multiplicand < product; multiplicand++) {
+ remaindar = product % multiplicand;
+ if (remaindar != 0) {
+ continue;
+ }
+ multiplier = product / multiplicand;
+ if (DIGITS(multiplicand) + DIGITS(multiplier) + DIGITS(product) != 6) {
+ continue;
+ }
+
+ snprintf(buf, sizeof buf, "%d%d%d", multiplicand, multiplier, product);
+ if (is_pandigital(buf)) {
+ sum += product;
+ break;
+ }
+ }
+ }
+ printf("%d\n", sum);
+
+ return 0;
+}
+
+static int compare(const void *a, const void *b)
+{
+ return *(const char *)a - *(const char *)b;
+}
+
+static int is_pandigital(char *buf)
+{
+ qsort(buf, 9, 1, compare);
+ return memcmp(buf, "123456789", 9) == 0;
+}
+
View
94 30-39/problem33.c
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2009, eagletmt
+ * Released under the MIT License <http://opensource.org/licenses/mit-license.php>
+ */
+
+#include <stdio.h>
+#include <math.h>
+
+static unsigned gcd(unsigned a, unsigned b);
+static int cancel(unsigned n1, unsigned d1, unsigned *n2, unsigned *d2);
+
+int main(void)
+{
+ unsigned ans_n = 1, ans_d = 1;
+ unsigned i;
+ for (i = 11; i < 100; i++) {
+ unsigned j;
+
+ if (i % 10 == 0) {
+ continue;
+ }
+
+ for (j = 11; j < i; j++) {
+ unsigned g, n, d;
+ unsigned n2, d2;
+
+ if (j % 10 == 0) {
+ continue;
+ }
+
+ if ((g = gcd(i, j)) == 1) {
+ continue;
+ }
+ /* cancel numerically */
+ n = j/g;
+ d = i/g;
+
+ /* cancel symbolically */
+ if (cancel(j, i, &n2, &d2)) {
+ unsigned g2 = gcd(n2, d2);
+ n2 /= g2; d2 /= g2;
+ if (n == n2 && d == d2) {
+ ans_n *= n;
+ ans_d *= d;
+ }
+ }
+ }
+ }
+ printf("%u\n", ans_d / gcd(ans_n, ans_d));
+ return 0;
+}
+
+unsigned gcd(unsigned a, unsigned b)
+{
+ unsigned r;
+ while ((r = a % b) != 0) {
+ a = b; b = r;
+ }
+ return b;
+}
+
+int cancel(unsigned n1, unsigned d1, unsigned *n2, unsigned *d2)
+{
+ if (n1%10 == d1%10) {
+ /* e.g. 24 and 34 */
+ *n2 = n1/10;
+ *d2 = d1/10;
+ return 1;
+ }
+ else if (n1%10 == d1/10) {
+ /* e.g. 24 and 41 */
+ *n2 = n1/10;
+ *d2 = d1%10;
+ return 1;
+ }
+ else if (n1/10 == d1%10) {
+ /* e.g. 24 and 32 */
+ *n2 = n1%10;
+ *d2 = d1/10;
+ return 1;
+ }
+ else if (n1/10 == d1/10) {
+ /* e.g. 24 and 21 */
+ *n2 = n1%10;
+ *d2 = d1%10;
+ return 1;
+ }
+ else {
+ *n2 = n1;
+ *d2 = d1;
+ return 0;
+ }
+}
+
View
39 30-39/problem34.c
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2009, eagletmt
+ * Released under the MIT License <http://opensource.org/licenses/mit-license.php>
+ */
+
+#include <stdio.h>
+
+unsigned fact(unsigned n);
+
+int main(void)
+{
+ unsigned i, sum = 0;
+ char buf[16];
+
+ for (i = 3; i < 100000; i++) {
+ unsigned j, s = 0;
+
+ snprintf(buf, sizeof buf, "%u", i);
+ for (j = 0; buf[j] != 0; j++) {
+ s += fact(buf[j]-'0');
+ }
+ if (i == s) {
+ sum += s;
+ }
+ }
+ printf("%u\n", sum);
+
+ return 0;
+}
+
+unsigned fact(unsigned n)
+{
+ unsigned f = 1;
+ while (n != 0) {
+ f *= n--;
+ }
+ return f;
+}
+
View
63 30-39/problem35.c
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2009, eagletmt
+ * Released under the MIT License <http://opensource.org/licenses/mit-license.php>
+ */
+
+#include <stdio.h>
+#include <math.h>
+
+#define N 1000000
+
+static int is_prime(unsigned n);
+
+int main(void)
+{
+ unsigned i, count = 0;
+
+ for (i = 2; i < N; i++) {
+ unsigned digits;
+ unsigned k;
+ unsigned j;
+
+ if (!is_prime(i)) {
+ continue;
+ }
+ digits = log10(i);
+ k = i;
+ for (j = 0; j < digits; j++) {
+ unsigned l = pow(10, digits);
+ unsigned t = k % l;
+ k /= l;
+ k += t*10;
+ if (!is_prime(k)) {
+ goto NEXT;
+ }
+ }
+ count++;
+NEXT:
+ ;
+ }
+ printf("%u\n", count);
+
+ return 0;
+}
+
+int is_prime(unsigned n)
+{
+ if (n <= 1) {
+ return 0;
+ }
+ else if (n == 2) {
+ return 1;
+ }
+ else {
+ unsigned i;
+ for (i = 2; i*i <= n; i++) {
+ if (n % i == 0) {
+ return 0;
+ }
+ }
+ return 1;
+ }
+}
+
View
38 30-39/problem36.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2009, eagletmt
+ * Released under the MIT License <http://opensource.org/licenses/mit-license.php>
+ */
+
+#include <stdio.h>
+
+#define N 1000000
+
+static int palindromic_base(int n, int base);
+
+int main(void)
+{
+ int i;
+ int sum = 0;
+
+ for (i = 1; i < N; i++) {
+ if (palindromic_base(i, 10) && palindromic_base(i, 2)) {
+ sum += i;
+ }
+ }
+ printf("%d\n", sum);
+
+ return 0;
+}
+
+int palindromic_base(int n, int base)
+{
+ int reversed = 0;
+ const int orig = n;
+
+ while (n) {
+ reversed = base*reversed + n%base;
+ n /= base;
+ }
+ return orig == reversed;
+}
+
View
67 30-39/problem37.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2009, eagletmt
+ * Released under the MIT License <http://opensource.org/licenses/mit-license.php>
+ */
+
+#include <stdio.h>
+#include <math.h>
+
+static int is_prime(unsigned n);
+
+int main(void)
+{
+ unsigned i;
+ unsigned count = 0, sum = 0;
+
+ for (i = 10; count < 11; i++) {
+ /* truncate from right to left */
+ unsigned k, d;
+
+ k = i;
+ while (k != 0) {
+ if (!is_prime(k)) {
+ goto NEXT;
+ }
+ k /= 10;
+ }
+
+ /* truncate from left to right */
+ k = i;
+ d = log10(k);
+ d = pow(10, d);
+ while (d != 1) {
+ if (!is_prime(k % d)) {
+ goto NEXT;
+ }
+ d /= 10;
+ }
+ sum += i;
+ count++;
+
+NEXT:
+ ;
+ }
+ printf("%u\n", sum);
+
+ return 0;
+}
+
+int is_prime(unsigned n)
+{
+ if (n <= 1) {
+ return 0;
+ }
+ else if (n == 2) {
+ return 1;
+ }
+ else {
+ unsigned i;
+ for (i = 2; i*i <= n; i++) {
+ if (n % i == 0) {
+ return 0;
+ }
+ }
+ return 1;
+ }
+}
+
View
70 30-39/problem38.c
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2009, eagletmt
+ * Released under the MIT License <http://opensource.org/licenses/mit-license.php>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define N 10000
+
+static int is_pandigital(const char *s);
+static int compare(const void *a, const void *b);
+
+int main(void)
+{
+ unsigned max = 0;
+ unsigned i;
+
+ for (i = 1; i < N; i++) {
+ char s[10] = {0};
+ unsigned j;
+
+ snprintf(s, sizeof s, "%u", i);
+ for (j = 2; ; j++) {
+ char t[10] = {0};
+
+ snprintf(t, sizeof t, "%u", i*j);
+ if (strlen(s) + strlen(t) > 9) {
+ break;
+ }
+ strncat(s, t, sizeof s - strlen(s));
+ }
+ if (is_pandigital(s)) {
+ unsigned l = strtoul(s, NULL, 10);
+ if (l > max) {
+ max = l;
+ }
+ }
+ }
+ printf("%u\n", max);
+
+ return 0;
+}
+
+int compare(const void *a, const void *b)
+{
+ return *(const char *)a - *(const char *)b;
+}
+
+int is_pandigital(const char *s)
+{
+ size_t l;
+ char t[10];
+ size_t i;
+
+ l = strlen(s);
+ if (l != 9) {
+ return 0;
+ }
+ strncpy(t, s, sizeof t);
+ qsort(t, l, sizeof *t, compare);
+ for (i = 0; i < l; i++) {
+ if (t[i] != i+'1') {
+ return 0;
+ }
+ }
+ return 1;
+}
+
View
32 30-39/problem39.c
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2009, eagletmt
+ * Released under the MIT License <http://opensource.org/licenses/mit-license.php>
+ */
+
+#include <stdio.h>
+
+int main(void)
+{
+ unsigned a, b, c, p;
+ unsigned cnt, max = 0, maxp = 0;
+
+ for (p = 1; p <= 1000; p++) {
+ cnt = 0;
+ for (a = 1; a < p/2; a++) {
+ for (b = a; b < p/2; b++) {
+ c = p - a - b;
+ if (a*a + b*b == c*c) {
+ cnt++;
+ }
+ }
+ }
+ if (cnt > max) {
+ max = cnt;
+ maxp = p;
+ }
+ }
+ printf("%u\n", maxp);
+
+ return 0;
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.