9 Sitoutukset e2085a9b39 ... 8831a8b4ae

Tekijä SHA1 Viesti Päivämäärä
  Vinicius Teshima 8831a8b4ae [Haskell][1] Adding Solution 5 kuukautta sitten
  Vinicius Teshima 59d23669c5 [C][12] Commenting debug message 10 kuukautta sitten
  Vinicius Teshima 53f7546882 [C][12] Adding solution ultra bruteforce solution 10 kuukautta sitten
  Vinicius Teshima 88c548594b Adding .swp to .gitignore 10 kuukautta sitten
  Vinicius Teshima 042b4ae836 Reordering project 10 kuukautta sitten
  Vinicius Teshima 69b3da93a4 [C] Adding Brute Force Solution for Problem 0011 1 vuosi sitten
  Vinicius Teshima 7bde94a7c8 [C] Adding Solution for Problem 0010 1 vuosi sitten
  Vinicius Teshima 3f6d2a2dd8 [C] Adding Solution for Problem 0009 1 vuosi sitten
  Vinicius Teshima ba1c2ec1c2 [C] Adding Solution for Problem 0008 1 vuosi sitten
20 muutettua tiedostoa jossa 635 lisäystä ja 139 poistoa
  1. 7 1
      c/.gitignore
  2. 8 83
      c/Makefile
  3. 0 55
      c/Project.ede
  4. 0 0
      c/src/0001.c
  5. 0 0
      c/src/0002.c
  6. 0 0
      c/src/0003.c
  7. 0 0
      c/src/0004.c
  8. 0 0
      c/src/0005.c
  9. 0 0
      c/src/0006.c
  10. 0 0
      c/src/0007.c
  11. 89 0
      c/src/0008.c
  12. 32 0
      c/src/0009.c
  13. 64 0
      c/src/0010.c
  14. 124 0
      c/src/0011.c
  15. 123 0
      c/src/0012.c
  16. 86 0
      c/src/da.h
  17. 63 0
      c/src/primes.h
  18. 5 0
      haskell/Makefile
  19. 9 0
      haskell/build.sh
  20. 25 0
      haskell/src/0001.hs

+ 7 - 1
c/.gitignore

@@ -1,5 +1,6 @@
 .deps
 *.o
+*.swp
 
 # Executables
 0001
@@ -8,4 +9,9 @@
 0004
 0005
 0006
-0007
+0007
+0008
+0009
+0010
+0011
+0012

+ 8 - 83
c/Makefile

@@ -1,87 +1,12 @@
-# Automatically Generated Makefile by EDE.
-# For use with: make
-# Relative File Name: Makefile
-#
-# DO NOT MODIFY THIS FILE OR YOUR CHANGES MAY BE LOST.
-# EDE is the Emacs Development Environment.
-# https://cedet.sourceforge.net/ede.shtml
-#
-CFLAGS=-std=c89 -m64 -Og -ggdb -pedantic -Wall -Wextra -Wshadow -Wcast-align -Wunused -Wconversion -Wmisleading-indentation -Wdouble-promotion -Wformat=2 -Wbad-function-cast -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Werror $(shell pkg-config --libs ${LIBS}) -I src/external -fsanitize=address
+SRCS:=$(wildcard src/*.c)
+HDRS:=$(wildcard src/*.h)
+BINS:=$(subst src/,,$(patsubst %.c, %, ${SRCS}))
 
-top=
-ede_FILES=Project.ede Makefile
-
-0001_SOURCES=0001.c
-0001_OBJ= 0001.o
-CC=gcc
-C_COMPILE=$(CC) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)
-C_DEPENDENCIES=-Wp,-MD,.deps/$(*F).P
-C_LINK=$(CC) $(CFLAGS) $(LDFLAGS) -L.
-0002_SOURCES=0002.c
-0002_OBJ= 0002.o
-0003_SOURCES=0003.c
-0003_OBJ= 0003.o
-0004_SOURCES=0004.c
-0004_OBJ= 0004.o
-0005_SOURCES=0005.c
-0005_OBJ= 0005.o
-0006_SOURCES=0006.c
-0006_OBJ= 0006.o
-0007_SOURCES=0007.c
-0007_OBJ= 0007.o
-VERSION=1.0
-DISTDIR=$(top)ProjectEuler_C-$(VERSION)
-top_builddir = 
-
-DEP_FILES=.deps/0001.P .deps/0002.P .deps/0003.P .deps/0004.P .deps/0005.P .deps/0006.P .deps/0007.P
-
-all: 0001 0002 0003 0004 0005 0006 0007
-
-DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)
--include $(DEP_FILES)
-
-%.o: %.c
-	@echo '$(C_COMPILE) -c $<'; \
-	$(C_COMPILE) $(C_DEPENDENCIES) -o $@ -c $<
-
-0001: $(0001_OBJ)
-	$(C_LINK) -o $@ $^ $(LDDEPS)
-
-0002: $(0002_OBJ)
-	$(C_LINK) -o $@ $^ $(LDDEPS)
-
-0003: $(0003_OBJ)
-	$(C_LINK) -o $@ $^ $(LDDEPS)
-
-0004: $(0004_OBJ)
-	$(C_LINK) -o $@ $^ $(LDDEPS)
-
-0005: $(0005_OBJ)
-	$(C_LINK) -o $@ $^ $(LDDEPS)
-
-0006: $(0006_OBJ)
-	$(C_LINK) -o $@ $^ $(LDDEPS)
-
-0007: $(0007_OBJ)
-	$(C_LINK) -o $@ $^ $(LDDEPS)
-
-tags: 
+all: ${BINS}
 
+${BINS}: %: src/%.c ${HDRS}
+	cc -Wall -Werror -ansi -o $@ $<
 
+.PHONY: clean
 clean:
-	rm -f *.mod *.o *.obj .deps/*.P .lo
-
-.PHONY: dist
-
-dist:
-	mkdir $(DISTDIR)
-	cp $(0001_SOURCES) $(0002_SOURCES) $(0003_SOURCES) $(0004_SOURCES) $(0005_SOURCES) $(0006_SOURCES) $(0007_SOURCES) $(ede_FILES) $(DISTDIR)
-
-Makefile: Project.ede
-	@echo Makefile is out of date!  It needs to be regenerated by EDE.
-	@echo If you have not modified Project.ede, you can use ‘touch’ to update the Makefile time stamp.
-	@false
-
-
-
-# End of Makefile
+	rm -v ${BINS}

+ 0 - 55
c/Project.ede

@@ -1,55 +0,0 @@
-;; Object ProjectEuler_C
-;; EDE Project Files are auto generated: Do Not Edit
-(ede-proj-project "ProjectEuler_C"
-  :file "Project.ede"
-  :name "ProjectEuler_C"
-  :targets
-  (list
-    (ede-proj-target-makefile-program "0001"
-      :object-name "0001"
-      :name "0001"
-      :path ""
-      :source '("0001.c")
-      :configuration-variables '("debug" ("CFLAGS" . "-g") ("LDFLAGS" . "-g")))
-    (ede-proj-target-makefile-program "0002"
-      :object-name "0002"
-      :name "0002"
-      :path ""
-      :source '("0002.c")
-      :configuration-variables '("debug" ("CFLAGS" . "-g") ("LDFLAGS" . "-g")))
-    (ede-proj-target-makefile-program "0003"
-      :object-name "0003"
-      :name "0003"
-      :path ""
-      :source '("0003.c")
-      :configuration-variables '("debug" ("CFLAGS" . "-g") ("LDFLAGS" . "-g")))
-    (ede-proj-target-makefile-program "0004"
-      :object-name "0004"
-      :name "0004"
-      :path ""
-      :source '("0004.c")
-      :configuration-variables '("debug" ("CFLAGS" . "-g") ("LDFLAGS" . "-g")))
-    (ede-proj-target-makefile-program "0005"
-      :object-name "0005"
-      :name "0005"
-      :path ""
-      :source '("0005.c")
-      :configuration-variables '("debug" ("CFLAGS" . "-g") ("LDFLAGS" . "-g")))
-    (ede-proj-target-makefile-program "0006"
-      :object-name "0006"
-      :name "0006"
-      :path ""
-      :source '("0006.c")
-      :configuration-variables '("debug" ("CFLAGS" . "-g") ("LDFLAGS" . "-g")))
-    (ede-proj-target-makefile-program "0007"
-      :object-name "0007"
-      :name "0007"
-      :path ""
-      :source '("0007.c")
-      :configuration-variables '("debug" ("CFLAGS" . "-g") ("LDFLAGS" . "-g"))))
-  :configurations '("debug" "release")
-  :object-name "ProjectEuler_C"
-  :makefile-type Makefile
-  :variables '(("CFLAGS" . "-std=c89 -m64 -Og -ggdb -pedantic -Wall -Wextra -Wshadow -Wcast-align -Wunused -Wconversion -Wmisleading-indentation -Wdouble-promotion -Wformat=2 -Wbad-function-cast -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Werror $(shell pkg-config --libs ${LIBS}) -I src/external -fsanitize=address"))
-  :configuration-variables nil
-  :metasubproject t)

+ 0 - 0
c/0001.c → c/src/0001.c


+ 0 - 0
c/0002.c → c/src/0002.c


+ 0 - 0
c/0003.c → c/src/0003.c


+ 0 - 0
c/0004.c → c/src/0004.c


+ 0 - 0
c/0005.c → c/src/0005.c


+ 0 - 0
c/0006.c → c/src/0006.c


+ 0 - 0
c/0007.c → c/src/0007.c


+ 89 - 0
c/src/0008.c

@@ -0,0 +1,89 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "da.h"
+
+
+DA_DEF_STRUCT(char, str);
+DA_DEF_STRUCT(uint64_t, prods);
+
+void str_append(struct str *str, const char *cstr);
+uint8_t char_to_u8(char c);
+
+int
+main(int argc, const char **argv)
+{
+	uint64_t res = 0;
+	uint64_t i = 0, j = 0;
+	uint64_t window_size = 13;
+	struct str str = {NULL, 0, 1024};
+	struct prods prods = {0};
+	DA_CREATE(prods, sizeof(*prods.items));
+
+	str.items = calloc(str.cap, sizeof(*str.items));
+
+	str_append(&str, "73167176531330624919225119674426574742355349194934");
+	str_append(&str, "96983520312774506326239578318016984801869478851843");
+	str_append(&str, "85861560789112949495459501737958331952853208805511");
+	str_append(&str, "12540698747158523863050715693290963295227443043557");
+	str_append(&str, "66896648950445244523161731856403098711121722383113");
+	str_append(&str, "62229893423380308135336276614282806444486645238749");
+	str_append(&str, "30358907296290491560440772390713810515859307960866");
+	str_append(&str, "70172427121883998797908792274921901699720888093776");
+	str_append(&str, "65727333001053367881220235421809751254540594752243");
+	str_append(&str, "52584907711670556013604839586446706324415722155397");
+	str_append(&str, "53697817977846174064955149290862569321978468622482");
+	str_append(&str, "83972241375657056057490261407972968652414535100474");
+	str_append(&str, "82166370484403199890008895243450658541227588666881");
+	str_append(&str, "16427171479924442928230863465674813919123162824586");
+	str_append(&str, "17866458359124566529476545682848912883142607690042");
+	str_append(&str, "24219022671055626321111109370544217506941658960408");
+	str_append(&str, "07198403850962455444362981230987879927244284909188");
+	str_append(&str, "84580156166097919133875499200524063689912560717606");
+	str_append(&str, "05886116467109405077541002256983155200055935729725");
+	str_append(&str, "71636269561882670428252483600823257530420752963450");
+
+	for ( i = 0; i < str.size - window_size; ++i ) {
+		uint64_t t = 1;
+		for ( j = 0; j < window_size; ++j ) {
+			t *= (uint64_t) char_to_u8(*(str.items + i + j));
+		}
+		DA_APPEND(prods, t);
+	}
+
+	for ( i = 0; i < prods.size; ++i ) {
+		if ( prods.items[i] > res ) {
+			res = prods.items[i];
+		}
+	}
+
+	printf("Result = %ld!\n", res);
+	free(str.items);
+	free(prods.items);
+	(void) argc; (void) argv;
+	return 0;
+}
+
+void
+str_append(struct str *str, const char *cstr)
+{
+	uint64_t cstr_lenght = strlen(cstr);
+	if ( (str->size + cstr_lenght) >= str->cap ) {
+		str->cap *= 2;
+		str->items = realloc(str->items, str->cap);
+	}
+
+	while ( *cstr != '\0' ) {
+		str->items[str->size++] = *cstr;
+		++cstr;
+	}
+}
+
+uint8_t
+char_to_u8(char c)
+{
+	return (uint8_t)(c - '0');
+}
+

+ 32 - 0
c/src/0009.c

@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <stdint.h>
+
+int
+main(int argc, const char **argv)
+{
+	uint64_t res = 0;
+	uint64_t a = 1, b = 1, c = 1;
+	uint64_t upper_limit = 1000;
+
+	for ( a = 1; a < upper_limit; ++a ) {
+		for ( b = 1; b < upper_limit; ++b ) {
+			for ( c = 1; c < upper_limit; ++c ) {
+				if ( ((a*a) + (b*b)) != (c*c) ) {
+					continue;
+				}
+
+				if ( (a+b+c) != 1000 ) {
+					continue;
+				}
+
+				res = a*b*c;
+				goto end;
+			}
+		}
+	}
+
+end:
+	printf("Result = %ld!\n", res);
+	(void) argc; (void) argv;
+	return 0;
+}

+ 64 - 0
c/src/0010.c

@@ -0,0 +1,64 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+
+#define IMP
+#include "primes.h"
+#include "da.h"
+
+struct ll {
+	uint64_t d;
+	struct ll *n;
+};
+
+int
+main(int argc, const char **argv)
+{
+	uint64_t res = 0;
+	uint64_t i = 0;
+	struct ll primes = {0};
+	struct ll *tail = &primes;
+	struct ll *last = &primes;
+	struct ll *_tail = &primes;
+	/* void *tmp = NULL; */
+	uint64_t primes_size = 0;
+	uint64_t p = 0;
+	uint64_t target = 2000000;
+
+	for ( i = 2; i < target; ++i ) {
+		tail->d = i;
+		tail->n = calloc(1, sizeof(struct ll));
+		tail = tail->n;
+		++primes_size;
+	}
+	printf("primes_size = %ld\n", primes_size);
+
+	tail = &primes;
+	last = &primes;
+	while ( tail->n != NULL ) {
+		p = tail->d;
+		_tail = tail->n;
+
+		while ( _tail->n != NULL ) {
+			if ( _tail->d % p != 0 ) {
+				last = _tail;
+				_tail = _tail->n;
+				continue;
+			}
+			last->n = _tail->n;
+			/* tmp = _tail; */
+			_tail = _tail->n;
+			/* free(tmp); */
+		}
+
+		tail = tail->n;
+		res += p;
+	}
+
+	printf("Result = %ld!\n", res);
+
+	(void) argc; (void) argv;
+	return 0;
+}

+ 124 - 0
c/src/0011.c

@@ -0,0 +1,124 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+
+#define IMP
+#include "primes.h"
+#include "da.h"
+
+#define ROWS 20
+#define COLS 20
+
+struct point4 {
+	uint16_t i1;
+	uint16_t i2;
+	uint16_t i3;
+	uint16_t i4;
+};
+
+
+int
+main(int argc, const char **argv)
+{
+	uint64_t grid[ROWS * COLS] = {
+		 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, 81, 49, 31, 73, 55, 79, 14, 29,
+		93, 71, 40, 67, 53, 88, 30,  3, 49, 13, 36, 65, 52, 70, 95, 23,
+		 4, 60, 11, 42, 69, 24, 68, 56,  1, 32, 56, 71, 37,  2, 36, 91,
+		22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28,
+		66, 33, 13, 80, 24, 47, 32, 60, 99,  3, 45,  2, 44, 75, 33, 53,
+		78, 36, 84, 20, 35, 17, 12, 50, 32, 98, 81, 28, 64, 23, 67, 10,
+		26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70, 67, 26, 20, 68,
+		 2, 62, 12, 20, 95, 63, 94, 39, 63,  8, 40, 91, 66, 49, 94, 21,
+		24, 55, 58,  5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88,
+		34, 89, 63, 72, 21, 36, 23,  9, 75,  0, 76, 44, 20, 45, 35, 14,
+		 0, 61, 33, 97, 34, 31, 33, 95, 78, 17, 53, 28, 22, 75, 31, 67,
+		15, 94,  3, 80,  4, 62, 16, 14,  9, 53, 56, 92, 16, 39,  5, 42,
+		96, 35, 31, 47, 55, 58, 88, 24,  0, 17, 54, 24, 36, 29, 85, 57,
+		86, 56,  0, 48, 35, 71, 89,  7,  5, 44, 44, 37, 44, 60, 21, 58,
+		51, 54, 17, 58, 19, 80, 81, 68,  5, 94, 47, 69, 28, 73, 92, 13,
+		86, 52, 17, 77,  4, 89, 55, 40,  4, 52,  8, 83, 97, 35, 99, 16,
+		 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66, 88, 36, 68, 87,
+		57, 62, 20, 72,  3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69,
+		 4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18,  8, 46, 29, 32,
+		40, 62, 76, 36, 20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69,
+		82, 67, 59, 85, 74,  4, 36, 16, 20, 73, 35, 29, 78, 31, 90,  1,
+		74, 31, 49, 71, 48, 86, 81, 16, 23, 57,  5, 54,  1, 70, 54, 71,
+		83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52,  1, 89, 19, 67, 48,
+	};
+	uint64_t res = 0;
+	uint64_t i = 0, j = 0;
+	uint64_t sum = 0;
+
+#define T(i, j) ((j)+((i)*ROWS))
+	for ( i = 3 ; i < ROWS - 4; ++i ) {
+		for ( j = 3 ; j < COLS - 4; ++j ) {
+			sum = grid[T(i, j)]
+				* grid[T(i-1, j-1)]
+				* grid[T(i-2, j-2)]
+				* grid[T(i-3, j-3)];
+			if ( sum > res ) {
+				res = sum;
+			}
+			sum = grid[T(i, j)]
+				* grid[T(i-1, j+1)]
+				* grid[T(i-2, j+2)]
+				* grid[T(i-3, j+3)];
+			if ( sum > res ) {
+				res = sum;
+			}
+			sum = grid[T(i, j)]
+				* grid[T(i+1, j+1)]
+				* grid[T(i+2, j+2)]
+				* grid[T(i+3, j+3)];
+			if ( sum > res ) {
+				res = sum;
+			}
+			sum = grid[T(i, j)]
+				* grid[T(i+1, j-1)]
+				* grid[T(i+2, j-2)]
+				* grid[T(i+3, j-3)];
+			if ( sum > res ) {
+				res = sum;
+			}
+
+
+			sum = grid[T(i, j)]
+				* grid[T(i, j-1)]
+				* grid[T(i, j-2)]
+				* grid[T(i, j-3)];
+			if ( sum > res ) {
+				res = sum;
+			}
+			sum = grid[T(i, j)]
+				* grid[T(i-1, j)]
+				* grid[T(i-2, j)]
+				* grid[T(i-3, j)];
+			if ( sum > res ) {
+				res = sum;
+			}
+			sum = grid[T(i, j)]
+				* grid[T(i, j+1)]
+				* grid[T(i, j+2)]
+				* grid[T(i, j+3)];
+			if ( sum > res ) {
+				res = sum;
+			}
+			sum = grid[T(i, j)]
+				* grid[T(i+1, j)]
+				* grid[T(i+2, j)]
+				* grid[T(i+3, j)];
+			if ( sum > res ) {
+				res = sum;
+			}
+		}
+	}
+
+	printf("Result = %ld!\n", res);
+
+	(void) argc; (void) argv;
+	return 0;
+}

+ 123 - 0
c/src/0012.c

@@ -0,0 +1,123 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+#include <pthread.h>
+
+#define NUM_THREADS 28
+
+uint64_t num_of_div(uint64_t num);
+
+struct check_div {
+	uint64_t *nums;
+	uint64_t size;
+	uint64_t cap;
+};
+struct check_div_ret {
+	uint64_t num;
+	uint64_t div;
+};
+void *check_div_thread(void *data);
+
+int
+main(int argc, const char **argv)
+{
+	uint64_t res = UINT64_MAX;
+	uint64_t tri_num = 1;
+	uint64_t i = 2;
+	uint64_t j = 0;
+	pthread_t thrds[NUM_THREADS + 1] = {0};
+	uint64_t thrds_size = 0;
+
+	while ( res == UINT64_MAX ) {
+		struct check_div *data = NULL;
+		data = calloc(1, sizeof(*data));
+		data->cap = 100;
+		data->nums = calloc(data->cap + 2, sizeof(uint64_t));
+		data->size = 0;
+
+		while ( data->size < data->cap ) {
+			data->nums[data->size++] = tri_num;
+			tri_num += i++;
+		}
+
+		pthread_create(&thrds[thrds_size++], NULL,
+				check_div_thread, (void *)data);
+
+		if ( thrds_size < NUM_THREADS ) {
+			continue;
+		}
+
+		for ( j = 0; j < thrds_size; ++j ) {
+			struct check_div_ret *ret = NULL;
+			/* printf("Waiting for thread index: %ld\n", j); */
+			pthread_join(thrds[j], (void **)&ret);
+			if ( ret == NULL ) {
+				fprintf(stderr, "Thread did not return value");
+				exit(1);
+			}
+			/* printf("Got Result: %ld divs: %ld\n", ret->num, ret->div); */
+			if ( ret->div > 500 ) {
+				if ( ret->num < res ) {
+					res = ret->num;
+				}
+			}
+			free(ret);
+		}
+		
+		thrds_size = 0;
+	}
+	for ( ; j < thrds_size; ++j ) {
+		pthread_cancel(thrds[j]);
+	}
+
+	printf("Result = %ld!\n", res);
+
+	(void) argc; (void) argv;
+	return 0;
+}
+
+void *
+check_div_thread(void *data)
+{
+	struct check_div d = *((struct check_div *)data);
+	struct check_div_ret *ret = NULL;
+	uint64_t num = 0;
+	uint64_t i = 0;
+	uint64_t div = 0;
+	uint64_t max_div = 0;
+
+	for ( i = 0; i < d.size && max_div < 500; ++i ) {
+		num = d.nums[i];
+		div = num_of_div(num);
+		if ( div > max_div ) {
+			max_div = div;
+		}
+	}
+
+	free(((struct check_div *)data)->nums);
+	free(data);
+
+	ret = calloc(1, sizeof(struct check_div_ret));
+
+	ret->num = num;
+	ret->div = div;
+	/* printf("Returning num: %ld, div: %ld\n", num, div); */
+
+	return (void*) ret;
+}
+
+uint64_t
+num_of_div(uint64_t num)
+{
+	uint64_t nod = 0;
+	uint64_t i = 1;
+
+	for ( i = 1; i < num; ++i ) {
+		nod += (num % i == 0);
+	}
+
+	return nod;
+}
+

+ 86 - 0
c/src/da.h

@@ -0,0 +1,86 @@
+#ifndef DA_H
+#define DA_H
+
+#define CONCAT(a, b) CONCAT_INNER(a, b)
+#define CONCAT_INNER(a, b) a ## b
+
+#define UNIQUE_NAME(base) CONCAT(base, __COUNTER__)
+
+#include <stdlib.h>
+
+#define DA_DEF_STRUCT(type, name)		\
+	struct name {				\
+		type *items;			\
+		size_t size;			\
+		size_t cap;			\
+	}
+
+#define DA_DEF_STRUCT_ITEM(type, name)		\
+	struct {				\
+		type *items;			\
+		size_t size;			\
+		size_t cap;			\
+	} name
+
+#define DA_CREATE(da, item_size)				\
+	do {							\
+		(da).cap = 16;					\
+		(da).size = 0;					\
+		(da).items = calloc((da).cap, item_size);	\
+	} while(0)
+
+#define DA_DESTROY(da)					\
+	do {						\
+		free((da).items);			\
+	} while(0)
+
+#define DA_CLEAR(da)					\
+	do {						\
+		(da).size = 0;				\
+	} while(0)
+
+#define _DA_DELETE(da, index, i)					\
+	do {								\
+		if ( index > (da).size ) {				\
+			break;						\
+		}							\
+		memmove((da).items+index, (da).items+index+1,		\
+			(da).cap - (index+1));				\
+		--(da).size;						\
+	} while(0)
+
+#define DA_DELETE(da, index) \
+	_DA_DELETE(da, index, UNIQUE_NAME(_i))
+
+#define _DA_INSERT(da, item, index, i)					\
+	do {								\
+		if ( index > (da.size) ) {				\
+			break;						\
+		}							\
+		if ( (da).size+1 >= (da).cap ) {			\
+			(da).cap *= 2;					\
+			(da).items = realloc((da).items,		\
+					     (da).cap * sizeof(*(da).items)); \
+		}							\
+		memmove((da).items+index+1, (da).items+index,		\
+			(da).cap - index);				\
+		(da).items[index] = item;				\
+		(da).size++;						\
+	} while(0)
+
+#define DA_INSERT(da, item, index) \
+	_DA_INSERT(da, item, index, UNIQUE_NAME(_i))
+
+#define DA_APPEND(da, item)						\
+	do {								\
+		if ( (da).size+1 >= (da).cap ) {			\
+			(da).cap *= 2;					\
+			(da).items = realloc((da).items,		\
+					     (da).cap * sizeof(*(da).items)); \
+		}							\
+		(da).items[(da).size++] = item;			\
+	} while(0)
+
+#define DA_TAIL(da) (da).items[(da).size-1]
+
+#endif

+ 63 - 0
c/src/primes.h

@@ -0,0 +1,63 @@
+#ifdef _PRIMES_H
+#define _PRIMES_H
+
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "da.h"
+
+DA_STRUCT_DEF(uint64_t, primes);
+
+bool isprime(uint64_t n);
+void primes_fill(struct primes_da *primes);
+void primes_fill_with(struct primes *primes, uint64_t n);
+
+#if defined(IMP) || defined(PRIMES_IMP)
+
+void
+primes_fill_with(struct primes *primes, uint64_t n)
+{
+	uint64_t i = 0;
+	for ( ; primes->size < n; ++i ) {
+		if ( isprime(i) ) {
+			DA_APPEND(*primes, i);
+		}
+	}
+}
+
+bool
+isprime(uint64_t n)
+{
+	uint64_t i = 0;
+	if ( (n & 1) == 0 ) {
+		return ( n == 2 );
+	}
+
+	for ( i = 2; i < n; ++i ) {
+		if ( (n % i) == 0 ) {
+			return false;
+		}
+	}
+	return true;
+}
+
+void
+primes_fill(struct primes_da *primes)
+{
+	uint64_t i = primes->size;
+	if ( i == 0 ) {
+		i = 2;
+	}
+	for ( ; primes->size < primes->cap; ++i ) {
+		if ( isprime(i) ) {
+			primes->data[primes->size++] = i;
+		}
+	}
+}
+
+#endif /* defined(IMP) || defined(PRIMES_IMP) */
+
+
+
+#endif /* _PRIMES_H */

+ 5 - 0
haskell/Makefile

@@ -0,0 +1,5 @@
+
+
+all:
+	@echo 'Running build.sh'
+	@./build.sh

+ 9 - 0
haskell/build.sh

@@ -0,0 +1,9 @@
+#!/bin/sh
+
+find ./src -name '*.hs' -printf '%T@ %p\n' \
+	| sort -r | head -n1 | cut -d' ' -f2 |  while read file
+do
+	out_file="$(basename "$file" | cut -d'.' -f1)"
+	echo "Compiling file '${file}' into '${out_file}'"
+	ghc -o "$out_file" "$file"
+done

+ 25 - 0
haskell/src/0001.hs

@@ -0,0 +1,25 @@
+
+{-
+If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
+
+Find the sum of all the multiples of 3 or 5 below 1000.
+-}
+
+solution_is_div :: Int -> Int
+solution_is_div x = case (mod x 3) of
+  0 -> x
+  _ -> case (mod x 5) of
+        0 -> x
+        _ -> 0
+
+solution_inner :: Int -> Int
+solution_inner x = go' x 0 where
+  go' 1000 y = y
+  go' x y = go' (x+1) ((solution_is_div x) + y)
+
+solution :: Int
+solution =
+  solution_inner 0
+
+main :: IO ()
+main = putStrLn ("Solution: " ++ show solution)