Skip to content

Commit bfa0a7f

Browse files
authored
Merge pull request #10 from DoctorLai/integer-factorization
Integer factorization
2 parents a73c525 + 4ac7375 commit bfa0a7f

File tree

13 files changed

+193
-11
lines changed

13 files changed

+193
-11
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ run:
2626
@for dir in $(SUBDIRS); do \
2727
echo "=== Running $$dir ==="; \
2828
(cd $$dir && $(MAKE) SANITIZE=$(SANITIZE) run) || exit 1; \
29+
## Run tests if present and fail the test if tests fail \
30+
if [ -f "$$dir/tests.sh" ]; then \
31+
echo "=== Running tests in $$dir ==="; \
32+
(cd $$dir && ./tests.sh) || exit 1; \
33+
fi; \
2934
done
3035

3136
# --------------------------

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Examples include (and will expand to):
2626
* Parallelism
2727
* [parallel-transform](./parallel-transform/)
2828
* Performance‑oriented C++ idioms
29+
* Algorithms
30+
* [integer-factorization](./integer-factorization/)
2931

3032
---
3133

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

176178
---
177179

fold-left-fold-right/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ include ../common.mk
55
# CXXFLAGS += -pthread
66

77
TARGET := $(notdir $(CURDIR))
8-
SRCS := main.cpp
8+
SRCS := $(wildcard *.cpp)
99
OBJS := $(SRCS:.cpp=.o)
1010

1111
all: $(TARGET)

integer-factorization/Makefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# pull in shared compiler settings
2+
include ../common.mk
3+
4+
# per-example flags
5+
# CXXFLAGS += -pthread
6+
7+
## get it from the folder name
8+
TARGET := $(notdir $(CURDIR))
9+
## all *.cpp files in this folder
10+
SRCS := $(wildcard *.cpp)
11+
OBJS := $(SRCS:.cpp=.o)
12+
13+
all: $(TARGET)
14+
15+
$(TARGET): $(OBJS)
16+
$(CXX) $(CXXFLAGS) -o $@ $^
17+
18+
%.o: %.cpp
19+
$(CXX) $(CXXFLAGS) -c $< -o $@
20+
21+
run: $(TARGET)
22+
./$(TARGET) $(ARGS)
23+
24+
clean:
25+
rm -f $(OBJS) $(TARGET)
26+
27+
# Delegates to top-level Makefile
28+
check-format:
29+
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)
30+
31+
.PHONY: all clean run check-format
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "factorization_algorithms.hpp"
2+
3+
std::vector<long long>
4+
trial_division(long long n)
5+
{
6+
std::vector<long long> factors;
7+
for (long long i = 2; i * i <= n; ++i) {
8+
while (n % i == 0) {
9+
factors.push_back(i);
10+
n /= i;
11+
}
12+
}
13+
if (n > 1) {
14+
factors.push_back(n);
15+
}
16+
return factors;
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* This header file declares the function prototypes for various integer factorization algorithms.
3+
*/
4+
5+
#ifndef FACTORIZATION_ALGORITHMS_H
6+
#define FACTORIZATION_ALGORITHMS_H
7+
8+
#include <vector>
9+
10+
std::vector<long long>
11+
trial_division(long long n);
12+
13+
#endif // FACTORIZATION_ALGORITHMS_H

integer-factorization/main.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* This program implements various algorithms for integer factorization,
3+
* It takes integer(s) from the command line or a file and outputs their prime
4+
* factorizations.
5+
*
6+
* Usage:
7+
* ./integer-factorization [options] [integers...]
8+
* Options:
9+
* -h, --help Show this help message and exit
10+
* -f, --file FILE Read integers from FILE, one per line
11+
* -o, --output FILE Write output to FILE instead of stdout
12+
*/
13+
14+
#include <iostream>
15+
#include <fstream>
16+
#include <vector>
17+
#include <string>
18+
#include <getopt.h>
19+
#include "factorization_algorithms.hpp"
20+
21+
void
22+
print_help()
23+
{
24+
std::cout << "Usage: ./integer-factorization [options] [integers...]\n"
25+
<< "Options:\n"
26+
<< " -h, --help Show this help message and exit\n"
27+
<< " -f, --file FILE Read integers from FILE, one per line\n"
28+
<< " -o, --output FILE Write output to FILE instead of stdout\n";
29+
}
30+
31+
int
32+
main(int argc, char* argv[])
33+
{
34+
std::string input_file;
35+
std::string output_file;
36+
std::vector<long long> numbers;
37+
38+
static struct option long_options[] = {
39+
{"help", no_argument, 0, 'h'},
40+
{"file", required_argument, 0, 'f'},
41+
{"output", required_argument, 0, 'o'},
42+
{0, 0, 0, 0}};
43+
44+
int opt;
45+
while ((opt = getopt_long(argc, argv, "hf:o:", long_options, NULL)) != -1) {
46+
switch (opt) {
47+
case 'h':
48+
print_help();
49+
return 0;
50+
case 'f':
51+
input_file = optarg;
52+
break;
53+
case 'o':
54+
output_file = optarg;
55+
break;
56+
default:
57+
print_help();
58+
return 1;
59+
}
60+
}
61+
62+
// Read numbers from file if specified
63+
if (!input_file.empty()) {
64+
std::ifstream infile(input_file);
65+
long long num;
66+
while (infile >> num) {
67+
numbers.push_back(num);
68+
}
69+
}
70+
71+
// Read numbers from command line arguments
72+
for (int index = optind; index < argc; index++) {
73+
numbers.push_back(std::stoll(argv[index]));
74+
}
75+
76+
// Prepare output stream
77+
std::ostream* out_stream = &std::cout;
78+
std::ofstream outfile;
79+
if (!output_file.empty()) {
80+
outfile.open(output_file);
81+
out_stream = &outfile;
82+
}
83+
84+
// Factor each number and output the result
85+
for (const auto& number : numbers) {
86+
std::vector<long long> factors;
87+
factors = trial_division(number);
88+
*out_stream << "Factors of " << number << ": ";
89+
for (const auto& factor : factors) {
90+
*out_stream << factor << " ";
91+
}
92+
*out_stream << "\n";
93+
}
94+
if (outfile.is_open()) {
95+
outfile.close();
96+
}
97+
return 0;
98+
}

integer-factorization/tests.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -ex
4+
5+
./integer-factorization -h
6+
7+
./integer-factorization 1234 45678

parallel-transform/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ include ../common.mk
66

77
## get it from the folder name
88
TARGET := $(notdir $(CURDIR))
9-
SRCS := main.cpp
9+
SRCS := $(wildcard *.cpp)
1010
OBJS := $(SRCS:.cpp=.o)
1111

1212
all: $(TARGET)

smart-ptr/Makefile

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,27 @@ include ../common.mk
44
# per-example flags
55
# CXXFLAGS += -pthread
66

7+
## get it from the folder name
78
TARGET := $(notdir $(CURDIR))
8-
SRCS = main.cpp
9+
SRCS := $(wildcard *.cpp)
10+
OBJS := $(SRCS:.cpp=.o)
911

1012
all: $(TARGET)
1113

12-
$(TARGET): $(SRCS)
13-
$(CXX) $(CXXFLAGS) -o $(TARGET) $(SRCS)
14+
$(TARGET): $(OBJS)
15+
$(CXX) $(CXXFLAGS) -o $@ $^
16+
17+
%.o: %.cpp
18+
$(CXX) $(CXXFLAGS) -c $< -o $@
1419

1520
run: $(TARGET)
1621
./$(TARGET) $(ARGS)
1722

1823
clean:
19-
rm -f $(TARGET)
24+
rm -f $(OBJS) $(TARGET)
25+
26+
# Delegates to top-level Makefile
27+
check-format:
28+
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)
2029

21-
.PHONY: all clean
30+
.PHONY: all clean run check-format

0 commit comments

Comments
 (0)