diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8326e08 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +build/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6ad58cb --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ + +CC = gcc +CFLAGS = -Wall -pedantic -ansi +LDFLAGS = -lm -lgmp +BUILDDIR = build +LIBDIR = lib + +STRIP = strip +RMDIR = rm -fr +RM = rm -f + +p% : p%.c + mkdir -p $(BUILDDIR) + $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $(BUILDDIR)/$@ + $(STRIP) $(BUILDDIR)/$@ + +p7 : p7.c $(LIBDIR)/prime.o +p10 : p10.c $(LIBDIR)/prime.o +p12 : p12.c $(LIBDIR)/prime.o + +$(LIBDIR)/%.o : $(LIBDIR)/%.c + $(CC) $(CFLAGS) -c $< -o $@ + +clean : + $(RMDIR) $(BUILDDIR) + $(RM) $(LIBDIR)/*.o diff --git a/p10.c b/p10.c index e2ee6fd..a853fe6 100644 --- a/p10.c +++ b/p10.c @@ -19,7 +19,7 @@ int main() { if (is_prime(i)) s += i; } - printf("%lli\n", s); /* this format throws som warnings but works so whatever */ + printf("%li\n", s); return 0; } diff --git a/p11.c b/p11.c index 9f27085..6f9d49f 100644 --- a/p11.c +++ b/p11.c @@ -20,6 +20,7 @@ /* macro for checking all directions of a square x,y */ #define check_square(x, y) \ do { \ + int p; \ if (x < 17) \ setprod(px(x, y)); \ if (y < 17) \ @@ -30,6 +31,13 @@ setprod(prd(x, y)); \ } while(0) +#define setprod(x) \ + do { \ + p = x; \ + if (p > result) \ + result = p; \ + } while(0) + int grid[20][20] = { { 8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8 }, { 49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0 }, @@ -55,12 +63,6 @@ int grid[20][20] = { unsigned int result = 0; -void inline setprod(int p) { - - if (p > result) - result = p; -} - int main() { int x, y; diff --git a/p2.c b/p2.c index 878e86e..f3bd99d 100644 --- a/p2.c +++ b/p2.c @@ -11,16 +11,18 @@ int main() { - int c = 0, a = 1, b = 1, r = 0; + int a = 1, b = 1, r = 0; - while(c < 4000000) { + for(;;) { - c = a + b; + int c = a + b; a = b; b = c; - - if (!(c & 1)) + + if (~c & 1) r += c; + if (c >= 4000000) + break; } printf("%i\n", r); diff --git a/p4.c b/p4.c index 19fbc09..62a4d9f 100644 --- a/p4.c +++ b/p4.c @@ -50,7 +50,7 @@ int main() { if (!ispalindrom(r) || !(a = factor(r))) continue; - printf("%i * %i = %i\n", r/a, a, r); + printf("%i\n", r); break; } diff --git a/p6.c b/p6.c index 8a9532e..8c8120f 100644 --- a/p6.c +++ b/p6.c @@ -19,4 +19,6 @@ int main() { } printf("%i\n", s2*s2 - s1); + + return 0; }