diff --git a/EulerProject.ino b/EulerProject.ino deleted file mode 100644 index b4e019d..0000000 --- a/EulerProject.ino +++ /dev/null @@ -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); - } -} \ No newline at end of file diff --git a/EulersProject.vcxproj b/EulersProject.vcxproj new file mode 100644 index 0000000..b9061d4 --- /dev/null +++ b/EulersProject.vcxproj @@ -0,0 +1,139 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {e5f5f7cd-3dcd-451d-851d-1a5547569bd9} + EulersProject + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/EulersProject.vcxproj.filters b/EulersProject.vcxproj.filters new file mode 100644 index 0000000..340b9d1 --- /dev/null +++ b/EulersProject.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Header Files + + + + + Header Files + + + \ No newline at end of file diff --git a/EulersProject.vcxproj.user b/EulersProject.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/EulersProject.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Exercises.cpp b/Exercises.cpp new file mode 100644 index 0000000..f54e664 --- /dev/null +++ b/Exercises.cpp @@ -0,0 +1,65 @@ +#include +#include +#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 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; +} \ No newline at end of file diff --git a/Exercises.h b/Exercises.h new file mode 100644 index 0000000..3c99c1e --- /dev/null +++ b/Exercises.h @@ -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); diff --git a/Main.h b/Main.h new file mode 100644 index 0000000..d1ee33e --- /dev/null +++ b/Main.h @@ -0,0 +1,23 @@ +#include +#include +#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(t2 - t1).count(); + cout << "Duration = " << duration_ns << " microseconds" << endl; + } +} \ No newline at end of file diff --git a/Problem1.cpp b/Problem1.cpp deleted file mode 100644 index 3876d00..0000000 --- a/Problem1.cpp +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/Problem1.hpp b/Problem1.hpp deleted file mode 100644 index fb37c08..0000000 --- a/Problem1.hpp +++ /dev/null @@ -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 \ No newline at end of file diff --git a/Problem2.cpp b/Problem2.cpp deleted file mode 100644 index 01fecbf..0000000 --- a/Problem2.cpp +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/Problem2.hpp b/Problem2.hpp deleted file mode 100644 index 636e4a3..0000000 --- a/Problem2.hpp +++ /dev/null @@ -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 \ No newline at end of file diff --git a/Problem3.cpp b/Problem3.cpp deleted file mode 100644 index cc6d316..0000000 --- a/Problem3.cpp +++ /dev/null @@ -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(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 deleted file mode 100644 index 5c3e1c4..0000000 --- a/Problem3.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#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 deleted file mode 100644 index 3e3dbe6..0000000 --- a/ProblemBase.hpp +++ /dev/null @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index c9115ab..5952aee 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # ProjectEuler + My source code for project Euler solutions -Extra challenge: All algorithms are run on an Arduino diff --git a/Timer.h b/Timer.h new file mode 100644 index 0000000..9349d65 --- /dev/null +++ b/Timer.h @@ -0,0 +1,29 @@ +#pragma once +#include +#include + +namespace tools +{ + template + class stopwatch + { + const typename Clock::time_point start_point; + public: + stopwatch() : + start_point(Clock::now()) + {} + + template + Rep elapsed_time() const + { + std::atomic_thread_fence(std::memory_order_relaxed); + auto counted_time = std::chrono::duration_cast(Clock::now() - start_point).count(); + std::atomic_thread_fence(std::memory_order_relaxed); + return static_cast(counted_time); + } + }; + + using precise_stopwatch = stopwatch<>; + using system_stopwatch = stopwatch; + using monotonic_stopwatch = stopwatch; +} \ No newline at end of file diff --git a/x64/Debug/EulersProject.Build.CppClean.log b/x64/Debug/EulersProject.Build.CppClean.log new file mode 100644 index 0000000..c48c6a3 --- /dev/null +++ b/x64/Debug/EulersProject.Build.CppClean.log @@ -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 diff --git a/x64/Debug/EulersProject.exe.recipe b/x64/Debug/EulersProject.exe.recipe new file mode 100644 index 0000000..ef6562f --- /dev/null +++ b/x64/Debug/EulersProject.exe.recipe @@ -0,0 +1,11 @@ + + + + + C:\Users\douwe\Projects\EulersProject\x64\Debug\EulersProject.exe + + + + + + \ No newline at end of file diff --git a/x64/Debug/EulersProject.ilk b/x64/Debug/EulersProject.ilk new file mode 100644 index 0000000..cc329fa Binary files /dev/null and b/x64/Debug/EulersProject.ilk differ diff --git a/x64/Debug/EulersProject.log b/x64/Debug/EulersProject.log new file mode 100644 index 0000000..fce1a1c --- /dev/null +++ b/x64/Debug/EulersProject.log @@ -0,0 +1 @@ + EulersProject.vcxproj -> C:\Users\douwe\Projects\EulersProject\x64\Debug\EulersProject.exe diff --git a/x64/Debug/EulersProject.tlog/CL.command.1.tlog b/x64/Debug/EulersProject.tlog/CL.command.1.tlog new file mode 100644 index 0000000..b757a14 Binary files /dev/null and b/x64/Debug/EulersProject.tlog/CL.command.1.tlog differ diff --git a/x64/Debug/EulersProject.tlog/CL.read.1.tlog b/x64/Debug/EulersProject.tlog/CL.read.1.tlog new file mode 100644 index 0000000..7d892a6 Binary files /dev/null and b/x64/Debug/EulersProject.tlog/CL.read.1.tlog differ diff --git a/x64/Debug/EulersProject.tlog/CL.write.1.tlog b/x64/Debug/EulersProject.tlog/CL.write.1.tlog new file mode 100644 index 0000000..1fddaa1 Binary files /dev/null and b/x64/Debug/EulersProject.tlog/CL.write.1.tlog differ diff --git a/x64/Debug/EulersProject.tlog/EulersProject.lastbuildstate b/x64/Debug/EulersProject.tlog/EulersProject.lastbuildstate new file mode 100644 index 0000000..1ae5b14 --- /dev/null +++ b/x64/Debug/EulersProject.tlog/EulersProject.lastbuildstate @@ -0,0 +1,2 @@ +PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.35.32215:TargetPlatformVersion=10.0.22000.0: +Debug|x64|C:\Users\douwe\Projects\EulersProject\| diff --git a/x64/Debug/EulersProject.tlog/link.command.1.tlog b/x64/Debug/EulersProject.tlog/link.command.1.tlog new file mode 100644 index 0000000..651a9d8 Binary files /dev/null and b/x64/Debug/EulersProject.tlog/link.command.1.tlog differ diff --git a/x64/Debug/EulersProject.tlog/link.read.1.tlog b/x64/Debug/EulersProject.tlog/link.read.1.tlog new file mode 100644 index 0000000..f4d107d Binary files /dev/null and b/x64/Debug/EulersProject.tlog/link.read.1.tlog differ diff --git a/x64/Debug/EulersProject.tlog/link.write.1.tlog b/x64/Debug/EulersProject.tlog/link.write.1.tlog new file mode 100644 index 0000000..01ad415 Binary files /dev/null and b/x64/Debug/EulersProject.tlog/link.write.1.tlog differ diff --git a/x64/Debug/EulersProject.vcxproj.FileListAbsolute.txt b/x64/Debug/EulersProject.vcxproj.FileListAbsolute.txt new file mode 100644 index 0000000..e69de29 diff --git a/x64/Debug/Exercises.obj b/x64/Debug/Exercises.obj new file mode 100644 index 0000000..6bb2f02 Binary files /dev/null and b/x64/Debug/Exercises.obj differ diff --git a/x64/Debug/Main.obj b/x64/Debug/Main.obj new file mode 100644 index 0000000..d691ece Binary files /dev/null and b/x64/Debug/Main.obj differ diff --git a/x64/Debug/vc143.idb b/x64/Debug/vc143.idb new file mode 100644 index 0000000..4615e8f Binary files /dev/null and b/x64/Debug/vc143.idb differ diff --git a/x64/Debug/vc143.pdb b/x64/Debug/vc143.pdb new file mode 100644 index 0000000..ca36d07 Binary files /dev/null and b/x64/Debug/vc143.pdb differ