commit 980b3ea1698525864e5ec01accb338725cd6ff16
parent 0e6d2918b84297c32a629dd5bbdd44b7b2ea7aad
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date: Wed, 3 Apr 2024 12:40:02 -0300
move `main.cpp` to `primos.cpp`
Diffstat:
3 files changed, 67 insertions(+), 76 deletions(-)
diff --git a/tps/1/informe/main.tex b/tps/1/informe/main.tex
@@ -456,7 +456,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{main.cpp}. Luego,
+Compruebe que este en el mismo directorio que el archivo \code{primos.cpp}. Luego,
compile el código con el programa \code{make}
\begin{fullgrayverb}
@@ -487,7 +487,7 @@ herramienta \code{make}, para eso ejecute el siguiente comando:
\newcommand{\myvariable}{\textbf{Hello}}
\begin{fullgrayverb}
-$ g++ -Wall -Wshadow -ansi -std=c++98 -O3 $\fileName$ -o primos
+$ g++ -Wall -Wshadow -ansi -std=c++98 -O3 primos.cpp -o primos
\end{fullgrayverb}$
diff --git a/tps/1/main.cpp b/tps/1/main.cpp
@@ -1,74 +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>
-#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
@@ -0,0 +1,65 @@
+// 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;
+}