From 2eb04509db71b65da6ef0a151e661cb0c5dd8b7d Mon Sep 17 00:00:00 2001 From: Douwe Ravers Date: Fri, 7 Aug 2026 13:29:57 +0200 Subject: [PATCH] Added previous arduino implementations. --- EulerProject.ino | 24 ++++++++++++++++++++++++ Problem1.cpp | 21 +++++++++++++++++++++ Problem1.hpp | 18 ++++++++++++++++++ Problem2.cpp | 19 +++++++++++++++++++ Problem2.hpp | 18 ++++++++++++++++++ Problem3.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ Problem3.hpp | 19 +++++++++++++++++++ ProblemBase.hpp | 16 ++++++++++++++++ README.md | 3 +++ 9 files changed, 179 insertions(+) create mode 100644 EulerProject.ino create mode 100644 Problem1.cpp create mode 100644 Problem1.hpp create mode 100644 Problem2.cpp create mode 100644 Problem2.hpp create mode 100644 Problem3.cpp create mode 100644 Problem3.hpp create mode 100644 ProblemBase.hpp create mode 100644 README.md diff --git a/EulerProject.ino b/EulerProject.ino new file mode 100644 index 0000000..b4e019d --- /dev/null +++ b/EulerProject.ino @@ -0,0 +1,24 @@ +#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); + } +} \ No newline at end of file diff --git a/Problem1.cpp b/Problem1.cpp new file mode 100644 index 0000000..3876d00 --- /dev/null +++ b/Problem1.cpp @@ -0,0 +1,21 @@ +#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; +} \ No newline at end of file diff --git a/Problem1.hpp b/Problem1.hpp new file mode 100644 index 0000000..fb37c08 --- /dev/null +++ b/Problem1.hpp @@ -0,0 +1,18 @@ +#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 \ No newline at end of file diff --git a/Problem2.cpp b/Problem2.cpp new file mode 100644 index 0000000..01fecbf --- /dev/null +++ b/Problem2.cpp @@ -0,0 +1,19 @@ +#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; +} \ No newline at end of file diff --git a/Problem2.hpp b/Problem2.hpp new file mode 100644 index 0000000..636e4a3 --- /dev/null +++ b/Problem2.hpp @@ -0,0 +1,18 @@ +#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 \ No newline at end of file diff --git a/Problem3.cpp b/Problem3.cpp new file mode 100644 index 0000000..cc6d316 --- /dev/null +++ b/Problem3.cpp @@ -0,0 +1,41 @@ +#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(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; +} \ No newline at end of file diff --git a/Problem3.hpp b/Problem3.hpp new file mode 100644 index 0000000..5c3e1c4 --- /dev/null +++ b/Problem3.hpp @@ -0,0 +1,19 @@ +#ifndef Problem3_h +#define Problem3_h + +#include "ProblemBase.hpp" +#include "Vector.h" + +class Problem3 : public ProblemBase { + +private: + Vector primes; + unsigned long iterator = 2; + float fl_input; + +public: + Problem3(); + bool Step(); +}; + +#endif \ No newline at end of file diff --git a/ProblemBase.hpp b/ProblemBase.hpp new file mode 100644 index 0000000..3e3dbe6 --- /dev/null +++ b/ProblemBase.hpp @@ -0,0 +1,16 @@ +#ifndef ProblemBase_h +#define ProblemBase_h + +class ProblemBase { + +public: + unsigned long output; + +protected: + unsigned long input; + +public: + virtual bool Step(); +}; + +#endif \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..c9115ab --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# ProjectEuler +My source code for project Euler solutions +Extra challenge: All algorithms are run on an Arduino