1 Commits
arduino ... cpp

Author SHA1 Message Date
78a1607789 Updated to initial implementation using cpp. 2026-08-07 13:32:49 +02:00
32 changed files with 325 additions and 177 deletions

View File

@@ -1,24 +0,0 @@
#include "Timer.h"
#include "ProblemBase.hpp"
#include "Problem3.hpp"
Problem3 problem = Problem3();
Timer timer(MICROS);
// Initialize serial connection
void setup() {
Serial.begin(9600);
Serial.println("Start calculating...");
timer.start(); // start the timer
}
// Calculation loop
void loop() {
if (problem.Step()) {
// Print solution
Serial.println("Result = " + String(problem.output) + " in " + String(timer.read()) + "us");
timer.stop();
delay(100);
exit(0);
}
}

139
EulersProject.vcxproj Normal file
View File

@@ -0,0 +1,139 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{e5f5f7cd-3dcd-451d-851d-1a5547569bd9}</ProjectGuid>
<RootNamespace>EulersProject</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Exercises.cpp" />
<ClCompile Include="Main.h" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Exercises.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Exercises.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Main.h">
<Filter>Header Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Exercises.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>

65
Exercises.cpp Normal file
View File

@@ -0,0 +1,65 @@
#include <iostream>
#include <vector>
#include "Exercises.h"
// Gives multiple of all multiples of 3 or 5 under given number.
int Ex1(int in) {
int out = 0;
for (int i = 3; i < in; i += 3) out += i;
for (int i = 5, t = 1; i < in; i += 5, t++) {
if (t == 3) t = 0;
else out += i;
}
return out;
}
// Gives multiple of all even fibonacci sequence values under given number
int Ex2(int in) {
int v1 = 0, v2 = 1, f = 0, out = 0;
while (true) {
// calc fibonacci value
f = v1 + v2;
v1 = v2;
v2 = f;
// stop if fibonacci value over max
if (in < f) break;
// skip if uneven
if ((f & 0b1) == 1) continue;
// sum
out += f;
}
return out;
}
/*
Works but needs to be optimised for large number: 600851475143
*/
// Gives biggest prime factor of given number
unsigned long long Ex3(unsigned long long in) {
vector<unsigned long long> primes = { 2 };
unsigned long long biggest_prime = -1;
for (unsigned long long num = 2; num <= in; num++)
{
// Find prime
bool isPrime = true;
for (auto prime : primes)
{
isPrime = num % prime != 0;
if (!isPrime) break;
}
if (!isPrime) continue;
primes.push_back(num);
// Is number factor of input, if not go to next number.
if (in % num != 0) continue;
// Save current prime as biggest prime and promote other factor as new max.
biggest_prime = num;
in = in / num;
}
return biggest_prime;
}

7
Exercises.h Normal file
View File

@@ -0,0 +1,7 @@
#pragma once
using namespace std;
int Ex1(int x);
int Ex2(int x);
unsigned long long Ex3(unsigned long long in);

23
Main.h Normal file
View File

@@ -0,0 +1,23 @@
#include <iostream>
#include <chrono>
#include "Exercises.h"
int main()
{
while (true) {
// Input
char input;
cout << "Check(c), test(t) or quit(q) -> ";
cin >> input;
cout << endl;
// action
auto t1 = std::chrono::high_resolution_clock::now();
if (input == 'c') cout << "check result: " << Ex3(13195) << endl;
else if (input == 't') cout << "test result: " << Ex3(600851475143) << endl;
else if (input == 'q') break;
auto t2 = std::chrono::high_resolution_clock::now();
auto duration_ns = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
cout << "Duration = " << duration_ns << " microseconds" << endl;
}
}

View File

@@ -1,21 +0,0 @@
#include "Problem1.hpp"
Problem1::Problem1() {
input = 1000;
}
bool Problem1::Step() {
// add all factors of 3 under input
factor3 += 3;
if (factor3 < input) output += factor3;
// add all factors of 5 under input but skip factors of 3
factor5 += 5;
if (factor5 < input) {
factor5_tick++;
if (factor5_tick != 3) output += factor5;
else factor5_tick = 0;
}
return input < factor3 && input < factor5;
}

View File

@@ -1,18 +0,0 @@
#ifndef Problem1_h
#define Problem1_h
#include "ProblemBase.hpp"
class Problem1 : public ProblemBase {
private:
unsigned long factor3 = 0;
unsigned long factor5 = 0;
unsigned long factor5_tick = 0;
public:
Problem1();
bool Step();
};
#endif

View File

@@ -1,19 +0,0 @@
#include "Problem2.hpp"
#include "Arduino.h"
Problem2::Problem2() {
input = 4000000;
}
bool Problem2::Step() {
// Sum fibonacci values
if ((fibonacci & 0b1) == 0) output += fibonacci;
// calculate next fibonacci value
fibonacci = value1 + value2;
value1 = value2;
value2 = fibonacci;
// stop if fibonacci value over max
return input < fibonacci;
}

View File

@@ -1,18 +0,0 @@
#ifndef Problem2_h
#define Problem2_h
#include "ProblemBase.hpp"
class Problem2 : public ProblemBase {
private:
unsigned long value1 = 0;
unsigned long value2 = 1;
unsigned long fibonacci;
public:
Problem2();
bool Step();
};
#endif

View File

@@ -1,41 +0,0 @@
#include "WString.h"
#include "HardwareSerial.h"
#include "Problem3.hpp"
#include "Arduino.h"
#include "Vector.h"
Problem3::Problem3() {
fl_input = 600851475143;
unsigned long primeArray[] = { 2 };
primes = Vector<unsigned long>(primeArray, 1);
}
bool Problem3::Step() {
Serial.println("iterator = " + String(iterator));
// Find prime
bool isPrime;
Serial.println("known primes = " + String(primes.size()));
for (unsigned long prime : primes) {
Serial.println("prime = " + String(prime));
isPrime = iterator % prime != 0;
if (!isPrime) break;
}
// Current value is new prime
if (isPrime) {
primes.push_back(iterator);
Serial.println("Found prime = " + String(iterator));
// Is number factor of input
if (fl_input / iterator == floor(fl_input / iterator)) {
// Save current prime as biggest prime and promote other factor as new max.
Serial.println("Found prime factor = " + String(iterator));
output = iterator;
fl_input = fl_input / iterator;
}
}
iterator++;
delay(1000);
return fl_input < iterator;
}

View File

@@ -1,19 +0,0 @@
#ifndef Problem3_h
#define Problem3_h
#include "ProblemBase.hpp"
#include "Vector.h"
class Problem3 : public ProblemBase {
private:
Vector<unsigned long> primes;
unsigned long iterator = 2;
float fl_input;
public:
Problem3();
bool Step();
};
#endif

View File

@@ -1,16 +0,0 @@
#ifndef ProblemBase_h
#define ProblemBase_h
class ProblemBase {
public:
unsigned long output;
protected:
unsigned long input;
public:
virtual bool Step();
};
#endif

View File

@@ -1,3 +1,3 @@
# ProjectEuler # ProjectEuler
My source code for project Euler solutions My source code for project Euler solutions
Extra challenge: All algorithms are run on an Arduino

29
Timer.h Normal file
View File

@@ -0,0 +1,29 @@
#pragma once
#include <chrono>
#include <atomic>
namespace tools
{
template <typename Clock = std::chrono::high_resolution_clock>
class stopwatch
{
const typename Clock::time_point start_point;
public:
stopwatch() :
start_point(Clock::now())
{}
template <typename Rep = typename Clock::duration::rep, typename Units = typename Clock::duration>
Rep elapsed_time() const
{
std::atomic_thread_fence(std::memory_order_relaxed);
auto counted_time = std::chrono::duration_cast<Units>(Clock::now() - start_point).count();
std::atomic_thread_fence(std::memory_order_relaxed);
return static_cast<Rep>(counted_time);
}
};
using precise_stopwatch = stopwatch<>;
using system_stopwatch = stopwatch<std::chrono::system_clock>;
using monotonic_stopwatch = stopwatch<std::chrono::steady_clock>;
}

View File

@@ -0,0 +1,13 @@
c:\users\douwe\projects\eulersproject\eulersproject\x64\debug\vc143.pdb
c:\users\douwe\projects\eulersproject\eulersproject\x64\debug\vc143.idb
c:\users\douwe\projects\eulersproject\eulersproject\x64\debug\main.obj
c:\users\douwe\projects\eulersproject\eulersproject\x64\debug\exercises.obj
c:\users\douwe\projects\eulersproject\eulersproject\x64\debug\eulersproject.ilk
c:\users\douwe\projects\eulersproject\x64\debug\eulersproject.pdb
c:\users\douwe\projects\eulersproject\x64\debug\eulersproject.exe
c:\users\douwe\projects\eulersproject\eulersproject\x64\debug\eulersproject.tlog\cl.command.1.tlog
c:\users\douwe\projects\eulersproject\eulersproject\x64\debug\eulersproject.tlog\cl.read.1.tlog
c:\users\douwe\projects\eulersproject\eulersproject\x64\debug\eulersproject.tlog\cl.write.1.tlog
c:\users\douwe\projects\eulersproject\eulersproject\x64\debug\eulersproject.tlog\link.command.1.tlog
c:\users\douwe\projects\eulersproject\eulersproject\x64\debug\eulersproject.tlog\link.read.1.tlog
c:\users\douwe\projects\eulersproject\eulersproject\x64\debug\eulersproject.tlog\link.write.1.tlog

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<ProjectOutputs>
<ProjectOutput>
<FullPath>C:\Users\douwe\Projects\EulersProject\x64\Debug\EulersProject.exe</FullPath>
</ProjectOutput>
</ProjectOutputs>
<ContentFiles />
<SatelliteDlls />
<NonRecipeFileRefs />
</Project>

BIN
x64/Debug/EulersProject.ilk Normal file

Binary file not shown.

View File

@@ -0,0 +1 @@
EulersProject.vcxproj -> C:\Users\douwe\Projects\EulersProject\x64\Debug\EulersProject.exe

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,2 @@
PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.35.32215:TargetPlatformVersion=10.0.22000.0:
Debug|x64|C:\Users\douwe\Projects\EulersProject\|

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
x64/Debug/Exercises.obj Normal file

Binary file not shown.

BIN
x64/Debug/Main.obj Normal file

Binary file not shown.

BIN
x64/Debug/vc143.idb Normal file

Binary file not shown.

BIN
x64/Debug/vc143.pdb Normal file

Binary file not shown.