浏览代码

[C] Adding Solution for Problem 0007

Vinicius Teshima 1 年之前
父节点
当前提交
e2085a9b39
共有 4 个文件被更改,包括 81 次插入4 次删除
  1. 2 1
      c/.gitignore
  2. 65 0
      c/0007.c
  3. 8 3
      c/Makefile
  4. 6 0
      c/Project.ede

+ 2 - 1
c/.gitignore

@@ -7,4 +7,5 @@
 0003
 0003
 0004
 0004
 0005
 0005
-0006
+0006
+0007

+ 65 - 0
c/0007.c

@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+struct primes_da {
+	uint64_t *data;
+	uint64_t size;
+	uint64_t cap;
+};
+
+
+bool isprime(uint64_t n);
+void primes_fill(struct primes_da *primes);
+
+int
+main(int argc, const char **argv)
+{
+	uint64_t res = 0;
+	uint64_t target = 10001;
+	struct primes_da primes = {0};
+
+	primes.cap = target + 1;
+	primes.data = calloc(primes.cap, sizeof(uint64_t));
+	primes.size = 0;
+
+	primes_fill(&primes);
+	res = primes.data[target-1];
+
+	printf("Result = %ld!\n", res);
+
+	free(primes.data);
+	(void) argc; (void) argv;
+	return 0;
+}
+
+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;
+		}
+	}
+}

+ 8 - 3
c/Makefile

@@ -27,13 +27,15 @@ C_LINK=$(CC) $(CFLAGS) $(LDFLAGS) -L.
 0005_OBJ= 0005.o
 0005_OBJ= 0005.o
 0006_SOURCES=0006.c
 0006_SOURCES=0006.c
 0006_OBJ= 0006.o
 0006_OBJ= 0006.o
+0007_SOURCES=0007.c
+0007_OBJ= 0007.o
 VERSION=1.0
 VERSION=1.0
 DISTDIR=$(top)ProjectEuler_C-$(VERSION)
 DISTDIR=$(top)ProjectEuler_C-$(VERSION)
 top_builddir = 
 top_builddir = 
 
 
-DEP_FILES=.deps/0001.P .deps/0002.P .deps/0003.P .deps/0004.P .deps/0005.P .deps/0006.P
+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
+all: 0001 0002 0003 0004 0005 0006 0007
 
 
 DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)
 DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)
 -include $(DEP_FILES)
 -include $(DEP_FILES)
@@ -60,6 +62,9 @@ DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)
 0006: $(0006_OBJ)
 0006: $(0006_OBJ)
 	$(C_LINK) -o $@ $^ $(LDDEPS)
 	$(C_LINK) -o $@ $^ $(LDDEPS)
 
 
+0007: $(0007_OBJ)
+	$(C_LINK) -o $@ $^ $(LDDEPS)
+
 tags: 
 tags: 
 
 
 
 
@@ -70,7 +75,7 @@ clean:
 
 
 dist:
 dist:
 	mkdir $(DISTDIR)
 	mkdir $(DISTDIR)
-	cp $(0001_SOURCES) $(0002_SOURCES) $(0003_SOURCES) $(0004_SOURCES) $(0005_SOURCES) $(0006_SOURCES) $(ede_FILES) $(DISTDIR)
+	cp $(0001_SOURCES) $(0002_SOURCES) $(0003_SOURCES) $(0004_SOURCES) $(0005_SOURCES) $(0006_SOURCES) $(0007_SOURCES) $(ede_FILES) $(DISTDIR)
 
 
 Makefile: Project.ede
 Makefile: Project.ede
 	@echo Makefile is out of date!  It needs to be regenerated by EDE.
 	@echo Makefile is out of date!  It needs to be regenerated by EDE.

+ 6 - 0
c/Project.ede

@@ -40,6 +40,12 @@
       :name "0006"
       :name "0006"
       :path ""
       :path ""
       :source '("0006.c")
       :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"))))
       :configuration-variables '("debug" ("CFLAGS" . "-g") ("LDFLAGS" . "-g"))))
   :configurations '("debug" "release")
   :configurations '("debug" "release")
   :object-name "ProjectEuler_C"
   :object-name "ProjectEuler_C"