Solutions for the first 10 problems
Signed-off-by: erick-alcachofa <erick@artichoke.dev>
This commit is contained in:
commit
4989ed8a4d
15
.gitignore
vendored
Normal file
15
.gitignore
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
# Ignore all
|
||||
*
|
||||
|
||||
# Unignore all with extensions
|
||||
!*.*
|
||||
|
||||
# Unignore all dirs
|
||||
!*/
|
||||
|
||||
**/.template/**
|
||||
|
||||
**/build/**
|
||||
|
||||
**/CMakeUserPresets.json
|
||||
**/compile_commands.json
|
||||
74
CMakeLists.txt
Normal file
74
CMakeLists.txt
Normal file
@ -0,0 +1,74 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
project(
|
||||
ProjectEuler
|
||||
LANGUAGES CXX
|
||||
)
|
||||
|
||||
find_package(GTest CONFIG REQUIRED)
|
||||
|
||||
file(GLOB SOURCES "problem-*/")
|
||||
|
||||
foreach(FILE IN LISTS SOURCES)
|
||||
cmake_path(GET FILE STEM PROBLEM)
|
||||
|
||||
file(READ "${FILE}/data/input" input)
|
||||
file(READ "${FILE}/data/example" example)
|
||||
|
||||
set(TEST_PATH "${CMAKE_CURRENT_BINARY_DIR}/${PROBLEM}/src/test.cpp")
|
||||
set(SOLUTION_PATH "${CMAKE_CURRENT_BINARY_DIR}/${PROBLEM}/src/solution.cpp")
|
||||
|
||||
configure_file(
|
||||
"${FILE}/src/test.cpp"
|
||||
"${TEST_PATH}" ESCAPE_QUOTES
|
||||
)
|
||||
|
||||
configure_file(
|
||||
"${FILE}/src/solution.cpp"
|
||||
"${SOLUTION_PATH}" ESCAPE_QUOTES
|
||||
)
|
||||
|
||||
add_executable("${PROBLEM}" "${SOLUTION_PATH}")
|
||||
add_executable("test-${PROBLEM}" "${TEST_PATH}")
|
||||
|
||||
target_link_libraries(
|
||||
"${PROBLEM}" PRIVATE
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
"test-${PROBLEM}" PRIVATE
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
"${PROBLEM}" PRIVATE
|
||||
"${FILE}/src"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/common"
|
||||
)
|
||||
|
||||
|
||||
target_include_directories(
|
||||
"test-${PROBLEM}" PRIVATE
|
||||
"${FILE}/src"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/common"
|
||||
)
|
||||
|
||||
set_target_properties(
|
||||
"${PROBLEM}" PROPERTIES
|
||||
RUNTIME_OUTPUT_DIRECTORY "${FILE}/bin"
|
||||
RUNTIME_OUTPUT_NAME "solution"
|
||||
SUFFIX ""
|
||||
)
|
||||
|
||||
set_target_properties(
|
||||
"test-${PROBLEM}" PROPERTIES
|
||||
RUNTIME_OUTPUT_DIRECTORY "${FILE}/bin"
|
||||
RUNTIME_OUTPUT_NAME "test"
|
||||
SUFFIX ""
|
||||
)
|
||||
endforeach()
|
||||
|
||||
75
common/sieve.hpp
Normal file
75
common/sieve.hpp
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Project Euler Solutions - Shared Utilities Module
|
||||
* -----------------------------------------------
|
||||
* File : sieve.hpp
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* - Utility Eratosthenes sieve class
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
template <std::integral T>
|
||||
struct Sieve {
|
||||
Sieve(T MaxPrime)
|
||||
: MaxPrime(MaxPrime)
|
||||
, isPrime(MaxPrime + 1, true) { }
|
||||
|
||||
~Sieve() = default;
|
||||
|
||||
void Fill() {
|
||||
isPrime[0] = false;
|
||||
isPrime[1] = false;
|
||||
|
||||
for (T p = 2; p <= MaxPrime; ++p) {
|
||||
if (isPrime[p]) {
|
||||
primes.push_back(p);
|
||||
|
||||
for (T i = p * p; i <= MaxPrime; i += p) {
|
||||
isPrime[i] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t Count() const {
|
||||
return primes.size();
|
||||
}
|
||||
|
||||
T Prime(size_t idx) const {
|
||||
return primes.at(idx);
|
||||
}
|
||||
|
||||
bool IsPrime(T p) {
|
||||
return isPrime.at(p);
|
||||
}
|
||||
|
||||
auto begin() {
|
||||
return primes.begin();
|
||||
}
|
||||
|
||||
auto end() {
|
||||
return primes.end();
|
||||
}
|
||||
|
||||
auto rbegin() {
|
||||
return primes.rbegin();
|
||||
}
|
||||
|
||||
auto rend() {
|
||||
return primes.rend();
|
||||
}
|
||||
|
||||
private:
|
||||
T MaxPrime;
|
||||
std::vector<T> primes;
|
||||
std::vector<bool> isPrime;
|
||||
};
|
||||
36
common/sqrt.hpp
Normal file
36
common/sqrt.hpp
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Project Euler Solutions - Shared Utilities Module
|
||||
* -----------------------------------------------
|
||||
* File : sqrt.hpp
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* - constexpr sqrt function
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
template <typename T>
|
||||
constexpr T sqrt_helper(T x, T lo, T hi) {
|
||||
if (lo == hi) {
|
||||
return lo;
|
||||
}
|
||||
|
||||
const T mid = (lo + hi + 1) / 2;
|
||||
|
||||
if (x / mid < mid) {
|
||||
return sqrt_helper<T>(x, lo, mid - 1);
|
||||
} else {
|
||||
return sqrt_helper(x, mid, hi);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr T ct_sqrt(T x) {
|
||||
return sqrt_helper<T>(x, 0, x / 2 + 1);
|
||||
}
|
||||
56
common/timer.hpp
Normal file
56
common/timer.hpp
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Project Euler Solutions - Shared Utilities Module
|
||||
* -----------------------------------------------
|
||||
* File : timer.hpp
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* - Utility class to measure execution time of the implementation
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <print>
|
||||
|
||||
using Nanoseconds = std::chrono::nanoseconds;
|
||||
using Microseconds = std::chrono::microseconds;
|
||||
using Milliseconds = std::chrono::milliseconds;
|
||||
using Seconds = std::chrono::seconds;
|
||||
|
||||
template <typename DefaultUnit = std::chrono::nanoseconds,
|
||||
bool LogAtEndOfScope = true>
|
||||
struct Timer {
|
||||
using Clock = std::chrono::high_resolution_clock;
|
||||
using Timepoint = std::chrono::time_point<Clock>;
|
||||
|
||||
Timer() : begin(Clock::now()) {}
|
||||
~Timer() {
|
||||
if constexpr (LogAtEndOfScope) {
|
||||
LogElapsed();
|
||||
}
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
begin = Clock::now();
|
||||
}
|
||||
|
||||
template <typename Unit = DefaultUnit>
|
||||
auto Elapsed() const {
|
||||
auto end = Clock::now();
|
||||
return std::chrono::duration_cast<Unit>(end - begin);
|
||||
}
|
||||
|
||||
template <typename Unit = DefaultUnit>
|
||||
void LogElapsed() const {
|
||||
std::println(stderr, "{}", this->Elapsed());
|
||||
}
|
||||
|
||||
private:
|
||||
Timepoint begin;
|
||||
};
|
||||
45
problem-1/src/impl.hpp
Normal file
45
problem-1/src/impl.hpp
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Project Euler Solutions - Core Implementation
|
||||
* ---------------------------------------------
|
||||
* Problem : Multiples of 3 or 5
|
||||
* URL : https://projecteuler.net/problem=1
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
|
||||
static int64_t Solve(std::stringstream in) {
|
||||
int64_t n = 0;
|
||||
|
||||
in >> n;
|
||||
|
||||
auto NumberOfMultiples =
|
||||
[](int64_t n, int64_t m) -> int64_t {
|
||||
return (n / m) - (n % m == 0);
|
||||
};
|
||||
|
||||
auto SumUpTo =
|
||||
[](int64_t n) -> int64_t {
|
||||
return (n * (n + 1)) / 2;
|
||||
};
|
||||
|
||||
auto countMults3 = NumberOfMultiples(n, 3);
|
||||
auto countMults5 = NumberOfMultiples(n, 5);
|
||||
auto countMults15 = NumberOfMultiples(n, 15);
|
||||
|
||||
return (
|
||||
( 3 * SumUpTo(countMults3)) +
|
||||
( 5 * SumUpTo(countMults5)) -
|
||||
(15 * SumUpTo(countMults15))
|
||||
);
|
||||
}
|
||||
40
problem-1/src/solution.cpp
Normal file
40
problem-1/src/solution.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Project Euler Solutions - Main Translation Unit
|
||||
* ------------------------------------------
|
||||
* Problem : Multiples of 3 or 5
|
||||
* URL : https://projecteuler.net/problem=1
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "impl.hpp"
|
||||
|
||||
int main(int, char *[]) {
|
||||
std::ios_base::sync_with_stdio(false),
|
||||
std::cin.tie(nullptr),
|
||||
std::cout.tie(nullptr);
|
||||
|
||||
std::string input = R"(@input@)";
|
||||
|
||||
decltype(Solve({})) ans = {};
|
||||
|
||||
{
|
||||
Timer<Microseconds> timer;
|
||||
ans = Solve(std::stringstream{input});
|
||||
}
|
||||
|
||||
std::println("{}", ans);
|
||||
}
|
||||
28
problem-1/src/test.cpp
Normal file
28
problem-1/src/test.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Project Euler Solutions - Test File
|
||||
* ------------------------------------
|
||||
* Problem : Multiples of 3 or 5
|
||||
* URL : https://projecteuler.net/problem=1
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "impl.hpp"
|
||||
|
||||
const char *input = R"(@example@)";
|
||||
|
||||
TEST(Problem1, test) {
|
||||
const auto ans = Solve(std::stringstream{input});
|
||||
EXPECT_EQ(ans, 23);
|
||||
}
|
||||
38
problem-10/src/impl.hpp
Normal file
38
problem-10/src/impl.hpp
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Project Euler Solutions - Core Implementation
|
||||
* ---------------------------------------------
|
||||
* Problem : Summation of Primes
|
||||
* URL : https://projecteuler.net/problem=10
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Tuesday, August 05 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
|
||||
#include "sieve.hpp"
|
||||
|
||||
static int64_t Solve(std::stringstream in) {
|
||||
int64_t n = 0;
|
||||
|
||||
in >> n;
|
||||
|
||||
Sieve sieve(n);
|
||||
sieve.Fill();
|
||||
|
||||
int64_t sum = 0;
|
||||
|
||||
for (auto p : sieve) {
|
||||
sum += p;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
40
problem-10/src/solution.cpp
Normal file
40
problem-10/src/solution.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Project Euler Solutions - Main Translation Unit
|
||||
* ------------------------------------------
|
||||
* Problem : Summation of Primes
|
||||
* URL : https://projecteuler.net/problem=10
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Tuesday, August 05 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "impl.hpp"
|
||||
|
||||
int main(int, char *[]) {
|
||||
std::ios_base::sync_with_stdio(false),
|
||||
std::cin.tie(nullptr),
|
||||
std::cout.tie(nullptr);
|
||||
|
||||
std::string input = R"(@input@)";
|
||||
|
||||
decltype(Solve({})) ans = {};
|
||||
|
||||
{
|
||||
Timer<Milliseconds> timer;
|
||||
ans = Solve(std::stringstream{input});
|
||||
}
|
||||
|
||||
std::println("{}", ans);
|
||||
}
|
||||
28
problem-10/src/test.cpp
Normal file
28
problem-10/src/test.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Project Euler Solutions - Test File
|
||||
* ------------------------------------
|
||||
* Problem : Summation of Primes
|
||||
* URL : https://projecteuler.net/problem=10
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Tuesday, August 05 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "impl.hpp"
|
||||
|
||||
const char *input = R"(@example@)";
|
||||
|
||||
TEST(Problem10, test) {
|
||||
const auto ans = Solve(std::stringstream{input});
|
||||
EXPECT_EQ(ans, 17);
|
||||
}
|
||||
41
problem-2/src/impl.hpp
Normal file
41
problem-2/src/impl.hpp
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Project Euler Solutions - Core Implementation
|
||||
* ---------------------------------------------
|
||||
* Problem : Even Fibonacci Numbers
|
||||
* URL : https://projecteuler.net/problem=2
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
|
||||
static int64_t Solve(std::stringstream in) {
|
||||
int64_t n = 0;
|
||||
|
||||
in >> n;
|
||||
|
||||
int64_t sum = 0;
|
||||
|
||||
int64_t prev = 1;
|
||||
int64_t curr = 2;
|
||||
|
||||
while (curr < n) {
|
||||
if ((curr & 1) == 0) {
|
||||
sum += curr;
|
||||
}
|
||||
|
||||
prev = std::exchange(curr, prev + curr);
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
40
problem-2/src/solution.cpp
Normal file
40
problem-2/src/solution.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Project Euler Solutions - Main Translation Unit
|
||||
* ------------------------------------------
|
||||
* Problem : Even Fibonacci Numbers
|
||||
* URL : https://projecteuler.net/problem=2
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "impl.hpp"
|
||||
|
||||
int main(int, char *[]) {
|
||||
std::ios_base::sync_with_stdio(false),
|
||||
std::cin.tie(nullptr),
|
||||
std::cout.tie(nullptr);
|
||||
|
||||
std::string input = R"(@input@)";
|
||||
|
||||
decltype(Solve({})) ans = {};
|
||||
|
||||
{
|
||||
Timer<Microseconds> timer;
|
||||
ans = Solve(std::stringstream{input});
|
||||
}
|
||||
|
||||
std::println("{}", ans);
|
||||
}
|
||||
28
problem-2/src/test.cpp
Normal file
28
problem-2/src/test.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Project Euler Solutions - Test File
|
||||
* ------------------------------------
|
||||
* Problem : Even Fibonacci Numbers
|
||||
* URL : https://projecteuler.net/problem=2
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "impl.hpp"
|
||||
|
||||
const char *input = R"(@example@)";
|
||||
|
||||
TEST(Problem2, test) {
|
||||
const auto ans = Solve(std::stringstream{input});
|
||||
EXPECT_EQ(ans, (2 + 8 + 34));
|
||||
}
|
||||
46
problem-3/src/impl.hpp
Normal file
46
problem-3/src/impl.hpp
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Project Euler Solutions - Core Implementation
|
||||
* ---------------------------------------------
|
||||
* Problem : Largest Prime Factor
|
||||
* URL : https://projecteuler.net/problem=3
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
|
||||
#include "sieve.hpp"
|
||||
#include "sqrt.hpp"
|
||||
|
||||
static int64_t Solve(std::stringstream in) {
|
||||
int64_t n = 0;
|
||||
|
||||
in >> n;
|
||||
|
||||
Sieve sieve(ct_sqrt(n));
|
||||
|
||||
sieve.Fill();
|
||||
|
||||
for (auto p : sieve) {
|
||||
if (n % p == 0) {
|
||||
do {
|
||||
n /= p;
|
||||
} while (n % p == 0);
|
||||
|
||||
if (n == 1) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
40
problem-3/src/solution.cpp
Normal file
40
problem-3/src/solution.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Project Euler Solutions - Main Translation Unit
|
||||
* ------------------------------------------
|
||||
* Problem : Largest Prime Factor
|
||||
* URL : https://projecteuler.net/problem=3
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "impl.hpp"
|
||||
|
||||
int main(int, char *[]) {
|
||||
std::ios_base::sync_with_stdio(false),
|
||||
std::cin.tie(nullptr),
|
||||
std::cout.tie(nullptr);
|
||||
|
||||
std::string input = R"(@input@)";
|
||||
|
||||
decltype(Solve({})) ans = {};
|
||||
|
||||
{
|
||||
Timer<Milliseconds> timer;
|
||||
ans = Solve(std::stringstream{input});
|
||||
}
|
||||
|
||||
std::println("{}", ans);
|
||||
}
|
||||
28
problem-3/src/test.cpp
Normal file
28
problem-3/src/test.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Project Euler Solutions - Test File
|
||||
* ------------------------------------
|
||||
* Problem : Largest Prime Factor
|
||||
* URL : https://projecteuler.net/problem=3
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "impl.hpp"
|
||||
|
||||
const char *input = R"(@example@)";
|
||||
|
||||
TEST(Problem3, test) {
|
||||
const auto ans = Solve(std::stringstream{input});
|
||||
EXPECT_EQ(ans, 29);
|
||||
}
|
||||
90
problem-4/src/impl.hpp
Normal file
90
problem-4/src/impl.hpp
Normal file
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Project Euler Solutions - Core Implementation
|
||||
* ---------------------------------------------
|
||||
* Problem : Largest Palindrome Product
|
||||
* URL : https://projecteuler.net/problem=4
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <print>
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
#include <generator>
|
||||
|
||||
#include "sqrt.hpp"
|
||||
|
||||
static std::generator<int64_t> GeneratePalindromes(int64_t NDigits) {
|
||||
int64_t factor = 1;
|
||||
int64_t absoluteMaxValue = 0;
|
||||
int64_t cValue = 0;
|
||||
|
||||
for (int64_t i = 0; i < (NDigits / 2); ++i) {
|
||||
factor *= 10;
|
||||
cValue *= 10;
|
||||
cValue += 9;
|
||||
}
|
||||
|
||||
cValue = absoluteMaxValue = (cValue * cValue);
|
||||
cValue /= factor;
|
||||
|
||||
while (cValue >= (factor / 10)) {
|
||||
int64_t tValue = cValue;
|
||||
int64_t tFactor = factor / 10;
|
||||
int64_t palindrome = cValue * factor;
|
||||
|
||||
for (int64_t i = 0; i < (NDigits / 2); ++i) {
|
||||
palindrome += ((tValue % 10) * tFactor);
|
||||
|
||||
tValue /= 10;
|
||||
tFactor /= 10;
|
||||
}
|
||||
|
||||
cValue -= 1;
|
||||
|
||||
if (palindrome > absoluteMaxValue) {
|
||||
continue;
|
||||
}
|
||||
|
||||
co_yield palindrome;
|
||||
}
|
||||
}
|
||||
|
||||
static int64_t Solve(std::stringstream in) {
|
||||
int64_t n = 0;
|
||||
|
||||
in >> n;
|
||||
|
||||
int64_t minVal = 1;
|
||||
int64_t maxVal = 10;
|
||||
|
||||
for (int64_t i = 1; i < n; ++i) {
|
||||
minVal *= 10;
|
||||
maxVal *= 10;
|
||||
}
|
||||
|
||||
minVal -= 1;
|
||||
|
||||
for (auto p : GeneratePalindromes(n * 2)) {
|
||||
for (int64_t f = ct_sqrt(p); f > minVal; --f) {
|
||||
if (p % f == 0) {
|
||||
auto d = p / f;
|
||||
|
||||
if ((d > minVal && d < maxVal) &&
|
||||
(f > minVal && f < maxVal)) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
40
problem-4/src/solution.cpp
Normal file
40
problem-4/src/solution.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Project Euler Solutions - Main Translation Unit
|
||||
* ------------------------------------------
|
||||
* Problem : Largest Palindrome Product
|
||||
* URL : https://projecteuler.net/problem=4
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "impl.hpp"
|
||||
|
||||
int main(int, char *[]) {
|
||||
std::ios_base::sync_with_stdio(false),
|
||||
std::cin.tie(nullptr),
|
||||
std::cout.tie(nullptr);
|
||||
|
||||
std::string input = R"(@input@)";
|
||||
|
||||
decltype(Solve({})) ans = {};
|
||||
|
||||
{
|
||||
Timer<Microseconds> timer;
|
||||
ans = Solve(std::stringstream{input});
|
||||
}
|
||||
|
||||
std::println("{}", ans);
|
||||
}
|
||||
28
problem-4/src/test.cpp
Normal file
28
problem-4/src/test.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Project Euler Solutions - Test File
|
||||
* ------------------------------------
|
||||
* Problem : Largest Palindrome Product
|
||||
* URL : https://projecteuler.net/problem=4
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "impl.hpp"
|
||||
|
||||
const char *input = R"(@example@)";
|
||||
|
||||
TEST(Problem4, test) {
|
||||
const auto ans = Solve(std::stringstream{input});
|
||||
EXPECT_EQ(ans, 9009);
|
||||
}
|
||||
69
problem-5/src/impl.hpp
Normal file
69
problem-5/src/impl.hpp
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Project Euler Solutions - Core Implementation
|
||||
* ---------------------------------------------
|
||||
* Problem : Smallest Multiple
|
||||
* URL : https://projecteuler.net/problem=5
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
|
||||
#include "sieve.hpp"
|
||||
|
||||
static int64_t Solve(std::stringstream in) {
|
||||
int64_t n = 0;
|
||||
|
||||
in >> n;
|
||||
|
||||
Sieve sieve(n);
|
||||
|
||||
sieve.Fill();
|
||||
|
||||
std::map<int64_t, int64_t> factors;
|
||||
|
||||
for (int64_t i = 1; i < n; ++i) {
|
||||
if (sieve.IsPrime(i)) {
|
||||
factors[i] = std::max(factors[i], 1l);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (auto p : sieve) {
|
||||
if (p > i) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (i % p == 0) {
|
||||
auto v = i;
|
||||
auto c = 0l;
|
||||
|
||||
do {
|
||||
c += 1;
|
||||
v /= p;
|
||||
} while (v % p == 0);
|
||||
|
||||
factors[p] = std::max(factors[p], c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int64_t mul = 1;
|
||||
|
||||
for (auto [p, e] : factors) {
|
||||
for (int64_t i = 0; i < e; ++i) {
|
||||
mul *= p;
|
||||
}
|
||||
}
|
||||
|
||||
return mul;
|
||||
}
|
||||
40
problem-5/src/solution.cpp
Normal file
40
problem-5/src/solution.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Project Euler Solutions - Main Translation Unit
|
||||
* ------------------------------------------
|
||||
* Problem : Smallest Multiple
|
||||
* URL : https://projecteuler.net/problem=5
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "impl.hpp"
|
||||
|
||||
int main(int, char *[]) {
|
||||
std::ios_base::sync_with_stdio(false),
|
||||
std::cin.tie(nullptr),
|
||||
std::cout.tie(nullptr);
|
||||
|
||||
std::string input = R"(@input@)";
|
||||
|
||||
decltype(Solve({})) ans = {};
|
||||
|
||||
{
|
||||
Timer<Microseconds> timer;
|
||||
ans = Solve(std::stringstream{input});
|
||||
}
|
||||
|
||||
std::println("{}", ans);
|
||||
}
|
||||
28
problem-5/src/test.cpp
Normal file
28
problem-5/src/test.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Project Euler Solutions - Test File
|
||||
* ------------------------------------
|
||||
* Problem : Smallest Multiple
|
||||
* URL : https://projecteuler.net/problem=5
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "impl.hpp"
|
||||
|
||||
const char *input = R"(@example@)";
|
||||
|
||||
TEST(Problem5, test) {
|
||||
const auto ans = Solve(std::stringstream{input});
|
||||
EXPECT_EQ(ans, 2520);
|
||||
}
|
||||
32
problem-6/src/impl.hpp
Normal file
32
problem-6/src/impl.hpp
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Project Euler Solutions - Core Implementation
|
||||
* ---------------------------------------------
|
||||
* Problem : Sum Square Difference
|
||||
* URL : https://projecteuler.net/problem=6
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
|
||||
static int64_t Solve(std::stringstream in) {
|
||||
int64_t n = 0;
|
||||
|
||||
in >> n;
|
||||
|
||||
auto squareOfSum = (n * (n + 1)) / 2;
|
||||
squareOfSum *= squareOfSum;
|
||||
|
||||
auto sumOfSquares = (n * (n + 1) * ((2 * n) + 1)) / 6;
|
||||
|
||||
return squareOfSum - sumOfSquares;
|
||||
}
|
||||
40
problem-6/src/solution.cpp
Normal file
40
problem-6/src/solution.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Project Euler Solutions - Main Translation Unit
|
||||
* ------------------------------------------
|
||||
* Problem : Sum Square Difference
|
||||
* URL : https://projecteuler.net/problem=6
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "impl.hpp"
|
||||
|
||||
int main(int, char *[]) {
|
||||
std::ios_base::sync_with_stdio(false),
|
||||
std::cin.tie(nullptr),
|
||||
std::cout.tie(nullptr);
|
||||
|
||||
std::string input = R"(@input@)";
|
||||
|
||||
decltype(Solve({})) ans = {};
|
||||
|
||||
{
|
||||
Timer<Microseconds> timer;
|
||||
ans = Solve(std::stringstream{input});
|
||||
}
|
||||
|
||||
std::println("{}", ans);
|
||||
}
|
||||
28
problem-6/src/test.cpp
Normal file
28
problem-6/src/test.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Project Euler Solutions - Test File
|
||||
* ------------------------------------
|
||||
* Problem : Sum Square Difference
|
||||
* URL : https://projecteuler.net/problem=6
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "impl.hpp"
|
||||
|
||||
const char *input = R"(@example@)";
|
||||
|
||||
TEST(Problem6, test) {
|
||||
const auto ans = Solve(std::stringstream{input});
|
||||
EXPECT_EQ(ans, 2640);
|
||||
}
|
||||
39
problem-7/src/impl.hpp
Normal file
39
problem-7/src/impl.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Project Euler Solutions - Core Implementation
|
||||
* ---------------------------------------------
|
||||
* Problem : 70001st Prime
|
||||
* URL : https://projecteuler.net/problem=7
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
|
||||
#include "sieve.hpp"
|
||||
|
||||
constexpr int64_t MaxPrime = 1000007;
|
||||
|
||||
static int64_t Solve(std::stringstream in) {
|
||||
int64_t n = 0;
|
||||
|
||||
in >> n;
|
||||
|
||||
Sieve sieve(MaxPrime);
|
||||
|
||||
sieve.Fill();
|
||||
|
||||
if (int64_t(sieve.Count()) < n) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return sieve.Prime(size_t(n - 1));
|
||||
}
|
||||
40
problem-7/src/solution.cpp
Normal file
40
problem-7/src/solution.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Project Euler Solutions - Main Translation Unit
|
||||
* ------------------------------------------
|
||||
* Problem : 70001st Prime
|
||||
* URL : https://projecteuler.net/problem=7
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "impl.hpp"
|
||||
|
||||
int main(int, char *[]) {
|
||||
std::ios_base::sync_with_stdio(false),
|
||||
std::cin.tie(nullptr),
|
||||
std::cout.tie(nullptr);
|
||||
|
||||
std::string input = R"(@input@)";
|
||||
|
||||
decltype(Solve({})) ans = {};
|
||||
|
||||
{
|
||||
Timer<Milliseconds> timer;
|
||||
ans = Solve(std::stringstream{input});
|
||||
}
|
||||
|
||||
std::println("{}", ans);
|
||||
}
|
||||
28
problem-7/src/test.cpp
Normal file
28
problem-7/src/test.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Project Euler Solutions - Test File
|
||||
* ------------------------------------
|
||||
* Problem : 70001st Prime
|
||||
* URL : https://projecteuler.net/problem=7
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Monday, August 04 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "impl.hpp"
|
||||
|
||||
const char *input = R"(@example@)";
|
||||
|
||||
TEST(Problem7, test) {
|
||||
const auto ans = Solve(std::stringstream{input});
|
||||
EXPECT_EQ(ans, 13);
|
||||
}
|
||||
41
problem-8/src/data.hpp
Normal file
41
problem-8/src/data.hpp
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Project Euler Solutions - Static Data
|
||||
* ---------------------------------------------
|
||||
* Problem : Largest Product in a Series
|
||||
* URL : https://projecteuler.net/problem=8
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Tuesday, August 05 2025
|
||||
*
|
||||
* Notes:
|
||||
* - Fixed number data from the problem
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
constexpr std::string_view number =
|
||||
"73167176531330624919225119674426574742355349194934"
|
||||
"96983520312774506326239578318016984801869478851843"
|
||||
"85861560789112949495459501737958331952853208805511"
|
||||
"12540698747158523863050715693290963295227443043557"
|
||||
"66896648950445244523161731856403098711121722383113"
|
||||
"62229893423380308135336276614282806444486645238749"
|
||||
"30358907296290491560440772390713810515859307960866"
|
||||
"70172427121883998797908792274921901699720888093776"
|
||||
"65727333001053367881220235421809751254540594752243"
|
||||
"52584907711670556013604839586446706324415722155397"
|
||||
"53697817977846174064955149290862569321978468622482"
|
||||
"83972241375657056057490261407972968652414535100474"
|
||||
"82166370484403199890008895243450658541227588666881"
|
||||
"16427171479924442928230863465674813919123162824586"
|
||||
"17866458359124566529476545682848912883142607690042"
|
||||
"24219022671055626321111109370544217506941658960408"
|
||||
"07198403850962455444362981230987879927244284909188"
|
||||
"84580156166097919133875499200524063689912560717606"
|
||||
"05886116467109405077541002256983155200055935729725"
|
||||
"71636269561882670428252483600823257530420752963450";
|
||||
42
problem-8/src/impl.hpp
Normal file
42
problem-8/src/impl.hpp
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Project Euler Solutions - Core Implementation
|
||||
* ---------------------------------------------
|
||||
* Problem : Largest Product in a Series
|
||||
* URL : https://projecteuler.net/problem=8
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Tuesday, August 05 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <print>
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
|
||||
#include "data.hpp"
|
||||
|
||||
static int64_t Solve(std::stringstream in) {
|
||||
int64_t n = 0;
|
||||
|
||||
in >> n;
|
||||
|
||||
int64_t maxMul = 0;
|
||||
|
||||
for (size_t i = size_t(n); i < number.length(); ++i) {
|
||||
int64_t mul = 1;
|
||||
|
||||
for (size_t j = i - size_t(n); j < i; ++j) {
|
||||
mul *= (number[j] - '0');
|
||||
}
|
||||
|
||||
maxMul = std::max(mul, maxMul);
|
||||
}
|
||||
|
||||
return maxMul;
|
||||
}
|
||||
40
problem-8/src/solution.cpp
Normal file
40
problem-8/src/solution.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Project Euler Solutions - Main Translation Unit
|
||||
* ------------------------------------------
|
||||
* Problem : Largest Product in a Series
|
||||
* URL : https://projecteuler.net/problem=8
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Tuesday, August 05 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "impl.hpp"
|
||||
|
||||
int main(int, char *[]) {
|
||||
std::ios_base::sync_with_stdio(false),
|
||||
std::cin.tie(nullptr),
|
||||
std::cout.tie(nullptr);
|
||||
|
||||
std::string input = R"(@input@)";
|
||||
|
||||
decltype(Solve({})) ans = {};
|
||||
|
||||
{
|
||||
Timer<Microseconds> timer;
|
||||
ans = Solve(std::stringstream{input});
|
||||
}
|
||||
|
||||
std::println("{}", ans);
|
||||
}
|
||||
28
problem-8/src/test.cpp
Normal file
28
problem-8/src/test.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Project Euler Solutions - Test File
|
||||
* ------------------------------------
|
||||
* Problem : Largest Product in a Series
|
||||
* URL : https://projecteuler.net/problem=8
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Tuesday, August 05 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "impl.hpp"
|
||||
|
||||
const char *input = R"(@example@)";
|
||||
|
||||
TEST(Problem8, test) {
|
||||
const auto ans = Solve(std::stringstream{input});
|
||||
EXPECT_EQ(ans, 5832);
|
||||
}
|
||||
57
problem-9/src/impl.hpp
Normal file
57
problem-9/src/impl.hpp
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Project Euler Solutions - Core Implementation
|
||||
* ---------------------------------------------
|
||||
* Problem : Special Pythagorean Triplet
|
||||
* URL : https://projecteuler.net/problem=9
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Tuesday, August 05 2025
|
||||
*
|
||||
* Notes:
|
||||
* - By using:
|
||||
* a^2 + b^2 = c^2
|
||||
* a + b + c = 1000
|
||||
* - We can get that:
|
||||
* a = (1000 * (b - 500)) / (b - 1000)
|
||||
* b = (1000 * (a - 500)) / (a - 1000)
|
||||
* c = 1000 - a - b
|
||||
* - So we just look for a positive number that can satisfy that
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <print>
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
|
||||
static int64_t Solve(std::stringstream) {
|
||||
auto eqTop = [](int32_t n) {
|
||||
return 1000 * (n - 500);
|
||||
};
|
||||
|
||||
auto eqBot = [](int32_t n) {
|
||||
return n - 1000;
|
||||
};
|
||||
|
||||
auto isValidAorB = [&](int32_t n) {
|
||||
return eqTop(n) % eqBot(n) == 0;
|
||||
};
|
||||
|
||||
auto getAorB = [&](int32_t n) {
|
||||
return eqTop(n) / eqBot(n);
|
||||
};
|
||||
|
||||
for (int32_t i = 1; i < 500; ++i) {
|
||||
auto a = getAorB(i);
|
||||
auto b = getAorB(a);
|
||||
auto c = 1000 - a - b;
|
||||
|
||||
if (isValidAorB(i) && isValidAorB(a)) {
|
||||
return a * b * c;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
40
problem-9/src/solution.cpp
Normal file
40
problem-9/src/solution.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Project Euler Solutions - Main Translation Unit
|
||||
* ------------------------------------------
|
||||
* Problem : Special Pythagorean Triplet
|
||||
* URL : https://projecteuler.net/problem=9
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Tuesday, August 05 2025
|
||||
*
|
||||
* Notes:
|
||||
* -
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "impl.hpp"
|
||||
|
||||
int main(int, char *[]) {
|
||||
std::ios_base::sync_with_stdio(false),
|
||||
std::cin.tie(nullptr),
|
||||
std::cout.tie(nullptr);
|
||||
|
||||
std::string input = R"(@input@)";
|
||||
|
||||
decltype(Solve({})) ans = {};
|
||||
|
||||
{
|
||||
Timer<Microseconds> timer;
|
||||
ans = Solve(std::stringstream{input});
|
||||
}
|
||||
|
||||
std::println("{}", ans);
|
||||
}
|
||||
19
problem-9/src/test.cpp
Normal file
19
problem-9/src/test.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Project Euler Solutions - Test File
|
||||
* ------------------------------------
|
||||
* Problem : Special Pythagorean Triplet
|
||||
* URL : https://projecteuler.net/problem=9
|
||||
*
|
||||
* Author : erick-alcachofa
|
||||
* Created : Tuesday, August 05 2025
|
||||
*
|
||||
* Notes:
|
||||
* - No sample case provided for this problem
|
||||
*
|
||||
* License : GNU Affero General Public License v3.0 (AGPLv3)
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
const char *input = R"(@example@)";
|
||||
76
readme.md
Normal file
76
readme.md
Normal file
@ -0,0 +1,76 @@
|
||||
# Project Euler Solutions in C++
|
||||
|
||||
This repository contains my personal solutions in C++ to problems from:
|
||||
[Project Euler](https://projecteuler.net/), written using modern C++23
|
||||
and built with CMake.
|
||||
|
||||
Each problem is organized into three parts:
|
||||
|
||||
- A core implementation file (`impl.hpp`)
|
||||
- A runner file that builds an executable, measures the time it takes
|
||||
to run the solution and prints the answer.
|
||||
- A test file to verify correctness using sample inputs or test cases.
|
||||
|
||||
## Build Instructions
|
||||
|
||||
### Requirements
|
||||
|
||||
- C++23-compatible compiler (e.g., GCC 13+, Clang 16+, MSVC with C++23 support)
|
||||
- CMake 3.20 or higher
|
||||
- A working build system (Make, Ninja, etc.)
|
||||
|
||||
### Build with CMake
|
||||
|
||||
```bash
|
||||
mkdir -p build
|
||||
cd build
|
||||
cmake ..
|
||||
cmake --build .
|
||||
````
|
||||
|
||||
### Run a Problem
|
||||
|
||||
After building, executables will be available in the `bin/` directory of each
|
||||
problem subfolder:
|
||||
|
||||
```bash
|
||||
./problem-1/bin/solution
|
||||
```
|
||||
|
||||
### Run Tests (Optional)
|
||||
|
||||
Test files also compile into separate test executables under the same `bin/`
|
||||
directory of each problem subfolder:
|
||||
|
||||
```bash
|
||||
./problem-1/bin/test
|
||||
```
|
||||
|
||||
## Philosophy
|
||||
|
||||
This project is focused on:
|
||||
|
||||
* Using modern C++23 code
|
||||
* Separating computation from I/O and tests
|
||||
* Practicing problem solving, performance, and readability
|
||||
|
||||
All solution files are licensed under AGPLv3 and are intended for educational
|
||||
purpose and personal use.
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the
|
||||
**GNU Affero General Public License v3.0 (AGPLv3)**.
|
||||
|
||||
See the [LICENSE](./LICENSE) file for full terms.
|
||||
|
||||
> You may use, modify, and distribute this work under the terms of the AGPLv3.
|
||||
> If you distribute modified versions, you must also make the source code
|
||||
> available.
|
||||
|
||||
## About Project Euler
|
||||
|
||||
Project Euler is a series of challenging mathematical/computer programming
|
||||
problems that require creative problem-solving and efficient algorithm design.
|
||||
|
||||
Official site: [https://projecteuler.net](https://projecteuler.net)
|
||||
11
run-all.sh
Executable file
11
run-all.sh
Executable file
@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
for i in {1..10}
|
||||
do
|
||||
{
|
||||
IFS=$'\n' read -r -d '' STDERR;
|
||||
IFS=$'\n' read -r -d '' STDOUT;
|
||||
} < <((printf '\0%s\0' "$(./problem-$i/bin/solution)" 1>&2) 2>&1)
|
||||
|
||||
printf "%3d] %6s -> %s\n" $i "$STDERR" "$STDOUT"
|
||||
done
|
||||
Loading…
x
Reference in New Issue
Block a user