Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ run:
@for dir in $(SUBDIRS); do \
echo "=== Running $$dir ==="; \
(cd $$dir && $(MAKE) SANITIZE=$(SANITIZE) run) || exit 1; \
## Run tests if present and fail the test if tests fail \
if [ -f "$$dir/tests.sh" ]; then \
echo "=== Running tests in $$dir ==="; \
(cd $$dir && ./tests.sh) || exit 1; \
fi; \
done

# --------------------------
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Examples include (and will expand to):
* Parallelism
* [parallel-transform](./parallel-transform/)
* Performance‑oriented C++ idioms
* Algorithms
* [integer-factorization](./integer-factorization/)

---

Expand Down Expand Up @@ -171,7 +173,7 @@ The CI setup requires **no updates** when new example folders are added.
The CI will perform:
1. `./clang-check.sh *.cpp *.hpp`
2. `make SANITIZE=[address, thread, undefined]`
3. `make run`
3. `make run` which will run `make run` for each project and `./tests.sh` if it is present.

---

Expand Down
2 changes: 1 addition & 1 deletion fold-left-fold-right/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include ../common.mk
# CXXFLAGS += -pthread

TARGET := $(notdir $(CURDIR))
SRCS := main.cpp
SRCS := $(wildcard *.cpp)
OBJS := $(SRCS:.cpp=.o)

all: $(TARGET)
Expand Down
31 changes: 31 additions & 0 deletions integer-factorization/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# pull in shared compiler settings
include ../common.mk

# per-example flags
# CXXFLAGS += -pthread

## get it from the folder name
TARGET := $(notdir $(CURDIR))
## all *.cpp files in this folder
SRCS := $(wildcard *.cpp)
OBJS := $(SRCS:.cpp=.o)

all: $(TARGET)

$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) -o $@ $^

%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@

run: $(TARGET)
./$(TARGET) $(ARGS)

clean:
rm -f $(OBJS) $(TARGET)

# Delegates to top-level Makefile
check-format:
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)

.PHONY: all clean run check-format
17 changes: 17 additions & 0 deletions integer-factorization/factorization_algorithms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "factorization_algorithms.hpp"

std::vector<long long>
trial_division(long long n)
{
std::vector<long long> factors;
for (long long i = 2; i * i <= n; ++i) {
while (n % i == 0) {
factors.push_back(i);
n /= i;
}
}
if (n > 1) {
factors.push_back(n);
}
return factors;
}
13 changes: 13 additions & 0 deletions integer-factorization/factorization_algorithms.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* This header file declares the function prototypes for various integer factorization algorithms.
*/

#ifndef FACTORIZATION_ALGORITHMS_H
#define FACTORIZATION_ALGORITHMS_H

#include <vector>

std::vector<long long>
trial_division(long long n);

#endif // FACTORIZATION_ALGORITHMS_H
98 changes: 98 additions & 0 deletions integer-factorization/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* This program implements various algorithms for integer factorization,
* It takes integer(s) from the command line or a file and outputs their prime
* factorizations.
*
* Usage:
* ./integer-factorization [options] [integers...]
* Options:
* -h, --help Show this help message and exit
* -f, --file FILE Read integers from FILE, one per line
* -o, --output FILE Write output to FILE instead of stdout
*/

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <getopt.h>
#include "factorization_algorithms.hpp"

void
print_help()
{
std::cout << "Usage: ./integer-factorization [options] [integers...]\n"
<< "Options:\n"
<< " -h, --help Show this help message and exit\n"
<< " -f, --file FILE Read integers from FILE, one per line\n"
<< " -o, --output FILE Write output to FILE instead of stdout\n";
}

int
main(int argc, char* argv[])
{
std::string input_file;
std::string output_file;
std::vector<long long> numbers;

static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"file", required_argument, 0, 'f'},
{"output", required_argument, 0, 'o'},
{0, 0, 0, 0}};

int opt;
while ((opt = getopt_long(argc, argv, "hf:o:", long_options, NULL)) != -1) {
switch (opt) {
case 'h':
print_help();
return 0;
case 'f':
input_file = optarg;
break;
case 'o':
output_file = optarg;
break;
default:
print_help();
return 1;
}
}

// Read numbers from file if specified
if (!input_file.empty()) {
std::ifstream infile(input_file);
long long num;
while (infile >> num) {
numbers.push_back(num);
}
}

// Read numbers from command line arguments
for (int index = optind; index < argc; index++) {
numbers.push_back(std::stoll(argv[index]));
}

// Prepare output stream
std::ostream* out_stream = &std::cout;
std::ofstream outfile;
if (!output_file.empty()) {
outfile.open(output_file);
out_stream = &outfile;
}

// Factor each number and output the result
for (const auto& number : numbers) {
std::vector<long long> factors;
factors = trial_division(number);
*out_stream << "Factors of " << number << ": ";
for (const auto& factor : factors) {
*out_stream << factor << " ";
}
*out_stream << "\n";
}
if (outfile.is_open()) {
outfile.close();
}
return 0;
}
7 changes: 7 additions & 0 deletions integer-factorization/tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

set -ex

./integer-factorization -h

./integer-factorization 1234 45678
2 changes: 1 addition & 1 deletion parallel-transform/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include ../common.mk

## get it from the folder name
TARGET := $(notdir $(CURDIR))
SRCS := main.cpp
SRCS := $(wildcard *.cpp)
OBJS := $(SRCS:.cpp=.o)

all: $(TARGET)
Expand Down
19 changes: 14 additions & 5 deletions smart-ptr/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ include ../common.mk
# per-example flags
# CXXFLAGS += -pthread

## get it from the folder name
TARGET := $(notdir $(CURDIR))
SRCS = main.cpp
SRCS := $(wildcard *.cpp)
OBJS := $(SRCS:.cpp=.o)

all: $(TARGET)

$(TARGET): $(SRCS)
$(CXX) $(CXXFLAGS) -o $(TARGET) $(SRCS)
$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) -o $@ $^

%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@

run: $(TARGET)
./$(TARGET) $(ARGS)

clean:
rm -f $(TARGET)
rm -f $(OBJS) $(TARGET)

# Delegates to top-level Makefile
check-format:
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)

.PHONY: all clean
.PHONY: all clean run check-format
2 changes: 1 addition & 1 deletion thread-safe-queue/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include ../common.mk
CXXFLAGS += -pthread

TARGET := $(notdir $(CURDIR))
SRCS := main.cpp
SRCS := $(wildcard *.cpp)
OBJS := $(SRCS:.cpp=.o)

all: $(TARGET)
Expand Down
2 changes: 1 addition & 1 deletion unique-ptr-basics/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include ../common.mk
# CXXFLAGS += -pthread

TARGET := $(notdir $(CURDIR))
SRCS := main.cpp
SRCS := $(wildcard *.cpp)
OBJS := $(SRCS:.cpp=.o)

all: $(TARGET)
Expand Down
2 changes: 1 addition & 1 deletion views-zip-enumerate/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include ../common.mk
# CXXFLAGS += -pthread

TARGET := $(notdir $(CURDIR))
SRCS := main.cpp
SRCS := $(wildcard *.cpp)
OBJS := $(SRCS:.cpp=.o)

all: $(TARGET)
Expand Down