CB100

Notas, resueltos y tps de la materia Algoritmos y Estructuras de Datos
Index Commits Files Refs README
commit 1a88bbaa106982a27a487bab3d18b6f59ab08917
parent 857b3f55eb1d22c88c5c4fe998eee0188a556956
Author: mjkloeckner <martinjkloeckner@gmail.com>
Date:   Fri, 15 Mar 2024 23:21:04 -0300

Add first Assignment solution

Diffstat:
Atps/1/Makefile | 15+++++++++++++++
Atps/1/main.cpp | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 75 insertions(+), 0 deletions(-)
diff --git a/tps/1/Makefile b/tps/1/Makefile
@@ -0,0 +1,15 @@
+CC := g++
+CFLAGS := -Wall -Wshadow -pedantic -ansi -std=c++98 -O3
+SRCS := $(wildcard *.cpp)
+
+TARGET := primos
+
+.PHONY: all clean
+
+all: $(TARGET)
+
+$(TARGET): $(SRCS)
+    $(CC) $(CLIBS) $(CFLAGS) -o $@ $^
+
+clean:
+    rm $(TARGET)
diff --git a/tps/1/main.cpp b/tps/1/main.cpp
@@ -0,0 +1,60 @@
+#include <iostream>
+#include <fstream>
+#include <ctime>
+#include <iomanip>
+#include <string>
+
+const unsigned int MAXIMO = 50000;
+
+bool isPrime (long n) {
+    if (n <= 1) {
+        return false;
+    }
+    
+    for (long i = 2; i < n; ++i) {
+        if (!(n % i)) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+int main (void) {
+    unsigned int ti, tf;
+    double tt;
+    std::ofstream fp;
+
+    fp.open("primos.txt");
+    if (!fp.is_open()) {
+        std::cerr << "Unable to open file";
+        return -1;
+    }
+
+    ti = clock();
+    unsigned long i, j;
+    for (i = j = 0; i < MAXIMO; ++i) {
+        if (isPrime(i)) {
+            fp << i << std::endl;
+            j++;
+        }
+    }
+    tf = clock();
+    tt = (double(tf - ti)) / CLOCKS_PER_SEC;
+    fp.close();
+    
+
+    std::string t_unit = "segundos";
+    if(tt < 1) {
+        tt *= 1000;
+        t_unit.assign("ms");
+    }
+
+    std::cout.precision(2);
+    std::cout << std::fixed
+              << "Se econtraron `" << j 
+              << "` numeros primos en `"
+              << tt << "` " << t_unit << std::endl;
+
+    return 0;
+}