Euler 1-5
This commit is contained in:
parent
9cf858b860
commit
68a75987e2
6 changed files with 161 additions and 0 deletions
15
project_euler/problem1.cc
Normal file
15
project_euler/problem1.cc
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#include <cstdint>
|
||||
#include <fmt/format.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::int64_t result = 0;
|
||||
|
||||
for (std::int64_t value = 3; value < 1000; value += 3)
|
||||
result += value;
|
||||
|
||||
for (std::int64_t value = 5; value < 1000; value += 5)
|
||||
result += value % 3 == 0 ? 0 : value;
|
||||
|
||||
fmt::print("Result: {}\n", result);
|
||||
}
|
||||
31
project_euler/problem2.cc
Normal file
31
project_euler/problem2.cc
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#include <cstdint>
|
||||
#include <fmt/format.h>
|
||||
|
||||
std::int64_t fibonacci()
|
||||
{
|
||||
static std::int64_t first = 1;
|
||||
static std::int64_t second = 2;
|
||||
|
||||
std::int64_t result = first + second;
|
||||
std::swap(first, second);
|
||||
second = result;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::int64_t result = 2;
|
||||
|
||||
while (true)
|
||||
{
|
||||
std::int64_t value = fibonacci();
|
||||
if (value >= 4'000'000)
|
||||
break;
|
||||
|
||||
if (value % 2 == 0)
|
||||
result += value;
|
||||
}
|
||||
|
||||
fmt::print("Result: {}\n", result);
|
||||
}
|
||||
16
project_euler/problem3.cc
Normal file
16
project_euler/problem3.cc
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#include <cstdint>
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "sieve.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
std::uint64_t input = 600'851'475'143;
|
||||
auto sieve = std::make_unique<Sieve<1'000'000>>();
|
||||
|
||||
for (auto value = static_cast<std::uint64_t>(std::sqrt(input)); value--; )
|
||||
{
|
||||
if (sieve->is_prime(value) && input % value == 0)
|
||||
fmt::print("Result: {}\n", value);
|
||||
}
|
||||
}
|
||||
19
project_euler/problem4.cc
Normal file
19
project_euler/problem4.cc
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#include <cstdint>
|
||||
#include <fmt/format.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::uint64_t max = 0;
|
||||
|
||||
for (std::size_t a = 999; a > 99; a--)
|
||||
{
|
||||
for (std::size_t b = 999; b > 99; b--)
|
||||
{
|
||||
std::string number = std::to_string(a * b);
|
||||
if (std::equal(number.begin(), number.end(), number.rbegin()))
|
||||
max = std::max(max, a * b);
|
||||
}
|
||||
}
|
||||
|
||||
fmt::print("Result: {}\n", max);
|
||||
}
|
||||
24
project_euler/problem5.cc
Normal file
24
project_euler/problem5.cc
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include <cstdint>
|
||||
#include <fmt/format.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
for (std::int64_t value = 40; ; value += 20)
|
||||
{
|
||||
bool valid = true;
|
||||
for (std::size_t divisor = 2; divisor <= 20; ++divisor)
|
||||
{
|
||||
if (value % divisor != 0)
|
||||
{
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (valid)
|
||||
{
|
||||
fmt::print("Result: {}\n", value);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
56
project_euler/sieve.h
Normal file
56
project_euler/sieve.h
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#ifndef SIEVE_H
|
||||
#define SIEVE_H
|
||||
|
||||
#include <array>
|
||||
#include <concepts>
|
||||
#include <cstdint>
|
||||
|
||||
template <std::uint64_t Max>
|
||||
class Sieve
|
||||
{
|
||||
std::array<bool, Max> _primes{};
|
||||
|
||||
public:
|
||||
Sieve()
|
||||
{
|
||||
for (bool& primeness : _primes)
|
||||
primeness = true;
|
||||
|
||||
for (std::size_t idx = 3; idx < static_cast<std::size_t>(std::sqrt(Max)); idx += 2)
|
||||
{
|
||||
if (!_primes[index(idx)])
|
||||
continue;
|
||||
|
||||
for (std::size_t value = (2 * idx); value < Max; value += idx)
|
||||
{
|
||||
if (value % 2 == 0)
|
||||
continue;
|
||||
_primes[index(value)] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool is_prime(std::uint64_t number) const noexcept
|
||||
{
|
||||
if (number == 2)
|
||||
return true;
|
||||
|
||||
if ((number == 0) || (number >= Max) || (number % 2 == 0))
|
||||
return false;
|
||||
|
||||
return _primes[index(number)];
|
||||
}
|
||||
|
||||
std::uint64_t size() const noexcept
|
||||
{
|
||||
return Max;
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t index(std::int64_t number) const noexcept
|
||||
{
|
||||
return (number - 1) / 2;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue