commit 72073ecd39afe9a4c0d368c4ada855797aef972d
parent 3081925558f61f165963e1490590a36744e62e2a
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date: Wed, 3 Apr 2024 12:56:01 -0300
Revert "move `main.cpp` to `primos.cpp`"
This reverts commit 980b3ea1698525864e5ec01accb338725cd6ff16.
Diffstat:
3 files changed, 76 insertions(+), 67 deletions(-)
diff --git a/tps/1/informe/informe.tex b/tps/1/informe/informe.tex
@@ -458,7 +458,7 @@ Github, tendrá que navegar al directorio \code{tps/1}
\subsection{Sistemas basados en UNIX}
-Compruebe que este en el mismo directorio que el archivo \code{primos.cpp}. Luego,
+Compruebe que este en el mismo directorio que el archivo \code{main.cpp}. Luego,
compile el código con el programa \code{make}
\begin{fullgrayverb}
@@ -489,7 +489,7 @@ herramienta \code{make}, para eso ejecute el siguiente comando:
\newcommand{\myvariable}{\textbf{Hello}}
\begin{fullgrayverb}
-$ g++ -Wall -Wshadow -ansi -std=c++98 -O3 primos.cpp -o primos
+$ g++ -Wall -Wshadow -ansi -std=c++98 -O3 $\fileName$ -o primos
\end{fullgrayverb}$
diff --git a/tps/1/main.cpp b/tps/1/main.cpp
@@ -0,0 +1,74 @@
+// El algoritmo utilizado para verificar que un numero es primo
+// es la Criba de Eratóstenes
+// https://es.wikipedia.org/wiki/Criba_de_Erat%C3%B3stenes
+
+#include <iostream>
+#include <fstream>
+#include <iomanip>
+#include <vector>
+#include <cmath>
+
+#define OUTPUT_FILE_PATH "primos.txt"
+const unsigned int MAXIMO = 100000000;
+
+void vectorDiscardNonPrimes(std::vector<bool>& v) {
+ v[0] = v[1] = false; // 0 y 1 no son primos
+ for (size_t i = 2; i < std::sqrt(MAXIMO); ++i) {
+ if(v[i]) {
+ for (size_t j = i; j <= (MAXIMO/i); ++j) {
+ v[i*j] = false;
+ }
+ }
+ }
+}
+
+void vectorExportToFilePath(
+ const std::vector<bool> v,
+ std::ofstream& fp,
+ unsigned int &primesWritten) {
+
+ primesWritten = 0;
+ for (size_t i = 2; i < v.size(); ++i) {
+ if(v[i]) {
+ fp << i << std::endl;
+ primesWritten++;
+ }
+ }
+}
+
+int main (void) {
+ unsigned int ti, primesFound;
+ double tt; // total time
+ std::ofstream fp;
+ std::vector<bool> numeros(MAXIMO, true);
+
+ ti = clock();
+
+ vectorDiscardNonPrimes(numeros);
+
+ fp.open(OUTPUT_FILE_PATH);
+ if (!fp.is_open()) {
+ std::cerr << "ERROR: No se pudo abrir `" OUTPUT_FILE_PATH "`\n";
+ return -1;
+ }
+
+ vectorExportToFilePath(numeros, fp, primesFound);
+ fp.close();
+
+ tt = (double(clock() - ti)) / CLOCKS_PER_SEC;
+
+ std::cout.precision(2);
+ std::string tUnit = "segundos";
+ if(tt < 1) {
+ tt *= 1000;
+ tUnit.assign("ms");
+ std::cout.precision(0);
+ }
+
+ std::cout << std::fixed
+ << "Se encontraron `" << primesFound
+ << "` numeros primos en `"
+ << tt << "` " << tUnit << std::endl;
+
+ return 0;
+}
diff --git a/tps/1/primos.cpp b/tps/1/primos.cpp
@@ -1,65 +0,0 @@
-// El algoritmo utilizado para verificar que un numero es primo
-// es la Criba de Eratóstenes
-// https://es.wikipedia.org/wiki/Criba_de_Erat%C3%B3stenes
-
-#include <iostream>
-#include <fstream>
-#include <iomanip>
-#include <vector>
-
-#define OUTPUT_FILE_PATH "primos.txt"
-const unsigned int MAXIMO = 100000000;
-
-void vectorDiscardNonPrimes(std::vector<bool>& v) {
- v[0] = v[1] = false; // 0 y 1 no son primos
- for (size_t i = 2; i*i < MAXIMO; ++i) {
- if(v[i]) {
- for (size_t j = i; j <= (MAXIMO/i); ++j) {
- v[i*j] = false;
- }
- }
- }
-}
-
-void vectorExportToFilePath(
- const std::vector<bool> v,
- std::ofstream& fp,
- unsigned int &primesWritten) {
-
- primesWritten = 0;
- for (size_t i = 2; i < v.size(); ++i) {
- if(v[i]) {
- fp << i << std::endl;
- primesWritten++;
- }
- }
-}
-
-int main (void) {
- clock_t t;
- unsigned int primesFound;
- std::ofstream fp;
- std::vector<bool> numeros(MAXIMO, true);
-
- t = clock();
- vectorDiscardNonPrimes(numeros);
-
- fp.open(OUTPUT_FILE_PATH);
- if (!fp.is_open()) {
- std::cerr << "ERROR: No se pudo abrir `" OUTPUT_FILE_PATH "`\n";
- return -1;
- }
-
- vectorExportToFilePath(numeros, fp, primesFound);
- fp.close();
-
- t = (clock() - t) / CLOCKS_PER_SEC;
- std::cout << std::fixed
- << std::setprecision(3)
- << "Se encontrarón `" << primesFound
- << "` numeros primos en `"
- << (double)(clock() - t) / CLOCKS_PER_SEC
- << "` " << " segundos\n";
-
- return 0;
-}