Commits

Anonymous committed 6ba23c7

added euler problems from laptop

Comments (0)

Files changed (4)

euler/012-trianglenumfactors.c

+#include <math.h>
+#include <stdio.h>
+
+#define MAX 100000
+
+long long numfactors(long long n) {
+	long long a = 2;
+	long long i, b;
+	for (i = 2; i < (n/2)+1; i++) {
+		if(n % i == 0) {
+			b = n / i;
+			if (b < i)
+				break;
+			if (b == i) {
+				a += 1;
+			} else {
+				a += 2;
+			}
+		}
+	}
+	return a;
+}
+
+int main() {
+	long long a = 0;
+	long long i, b;
+	for(i = 1; i < MAX; i++) {
+		a += i;
+		b = numfactors(a);
+		if (b > 500) {
+			printf("%lli has %lli factors\n", a, b);
+			return 0;
+		}
+	}
+	return 1;
+}

euler/014-sequencesteps.c

+#/usr/bin/python
+
+# works but is very slow, a c translation
+# would probably be usable
+
+def seq(n):
+	if n % 2 == 0:
+		return n / 2
+	else:
+		return 3 * n + 1
+
+def numsteps(n):
+	i = seq(n)
+	steps = 1
+	while i != 1:
+		steps += 1
+		i = seq(i)
+	return steps
+
+def prob14():
+	max = 0
+	maxi = 0
+	for i in range(1,1000000):
+		a = numsteps(i)
+		#print repr(i) + ":" + repr(a)
+		if a > max:
+			max = a
+			maxi = i
+	print repr(maxi) + " has " + repr(max) + "steps"
+
+prob14()

euler/020-factdigits.c

+#include <ctype.h>
+#include <gmp.h>
+#include <stdio.h>
+
+int main() {
+	mpz_t a;
+	size_t len;
+	char *sp, *ip;
+	int i, acc;
+
+	mpz_init(a);
+	mpz_set_ui(a, 1);
+	for(i = 2; i <= 100; i++)
+		mpz_mul_ui(a, a, i);
+
+	gmp_asprintf(&sp, "%Zd", a);
+	for(ip = sp, acc = 0; isdigit(*ip); ip++)
+		acc += *ip - '0';
+
+	printf("%i\n", acc);
+
+	return 0;
+}

euler/023-abundant.c

+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int isabundant(int n) {
+	int i, sum = 1;
+	//printf("%5i: 1 ", n);
+	for(i=2; i <= sqrt(n); i++) {
+		if(n % i == 0) {
+	//		printf("%i ", i);
+			sum += i;
+			if(n/i != i) {
+	//			printf("%i ", n / i);
+				sum += n/i;
+			}
+		}
+	}
+	//printf("= %i\n",sum);
+	//if(sum > n) printf("###### %6i ######\n", n);
+	return (sum > n) ? 1 : 0;
+}
+
+
+int *agenabundants(int max) {
+	int i, found = 1;
+	int *tab = malloc(sizeof(int));
+	tab[0] = 12;
+	for(i=14; i<=max; i++) {
+		if(isabundant(i)) {
+			found++;
+			tab = realloc(tab, found*sizeof(int));
+			tab[found-1] = i;
+		}
+	}
+	tab = realloc(tab, (found+1)*sizeof(int));
+	tab[found] = 0;
+	return tab;
+}
+
+int main() {
+	int i;
+	int *atab;
+	int *jp, *kp;
+
+	atab = agenabundants(28123);
+	return 0;
+}