1
0

4 Revīzijas 69b3da93a4 ... 59d23669c5

Autors SHA1 Ziņojums Datums
  Vinicius Teshima 59d23669c5 [C][12] Commenting debug message 10 mēneši atpakaļ
  Vinicius Teshima 53f7546882 [C][12] Adding solution ultra bruteforce solution 10 mēneši atpakaļ
  Vinicius Teshima 88c548594b Adding .swp to .gitignore 10 mēneši atpakaļ
  Vinicius Teshima 042b4ae836 Reordering project 10 mēneši atpakaļ
17 mainītis faili ar 385 papildinājumiem un 183 dzēšanām
  1. 3 1
      c/.gitignore
  2. 8 103
      c/Makefile
  3. 0 79
      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. 0 0
      c/src/0008.c
  12. 0 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. 0 0
      c/src/da.h
  17. 63 0
      c/src/primes.h

+ 3 - 1
c/.gitignore

@@ -1,5 +1,6 @@
 .deps
 *.o
+*.swp
 
 # Executables
 0001
@@ -12,4 +13,5 @@
 0008
 0009
 0010
-0011
+0011
+0012

+ 8 - 103
c/Makefile

@@ -1,107 +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 ./ -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
-0008_SOURCES=0008.c
-0008_OBJ= 0008.o
-0009_SOURCES=0009.c
-0009_OBJ= 0009.o
-0010_SOURCES=0010.c
-0010_OBJ= 0010.o
-0011_SOURCES=0011.c
-0011_OBJ= 0011.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 .deps/0008.P .deps/0009.P .deps/0010.P .deps/0011.P
-
-all: 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011
-
-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)
-
-0008: $(0008_OBJ)
-	$(C_LINK) -o $@ $^ $(LDDEPS)
-
-0009: $(0009_OBJ)
-	$(C_LINK) -o $@ $^ $(LDDEPS)
-
-0010: $(0010_OBJ)
-	$(C_LINK) -o $@ $^ $(LDDEPS)
-
-0011: $(0011_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) $(0008_SOURCES) $(0009_SOURCES) $(0010_SOURCES) $(0011_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 - 79
c/Project.ede

@@ -1,79 +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")))
-    (ede-proj-target-makefile-program "0008"
-      :object-name "0008"
-      :name "0008"
-      :path ""
-      :source '("0008.c")
-      :configuration-variables '("debug" ("CFLAGS" . "-g") ("LDFLAGS" . "-g")))
-    (ede-proj-target-makefile-program "0009"
-      :object-name "0009"
-      :name "0009"
-      :path ""
-      :source '("0009.c")
-      :configuration-variables '("debug" ("CFLAGS" . "-g") ("LDFLAGS" . "-g")))
-    (ede-proj-target-makefile-program "0010"
-      :object-name "0010"
-      :name "0010"
-      :path ""
-      :source '("0010.c")
-      :configuration-variables '("debug" ("CFLAGS" . "-g") ("LDFLAGS" . "-g")))
-    (ede-proj-target-makefile-program "0011"
-      :object-name "0011"
-      :name "0011"
-      :path ""
-      :source '("0011.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 ./ -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


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


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


+ 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;
+}
+

+ 0 - 0
c/da.h → c/src/da.h


+ 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 */