Added previous arduino implementations.
This commit is contained in:
24
EulerProject.ino
Normal file
24
EulerProject.ino
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
21
Problem1.cpp
Normal file
21
Problem1.cpp
Normal file
@@ -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;
|
||||||
|
}
|
||||||
18
Problem1.hpp
Normal file
18
Problem1.hpp
Normal file
@@ -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
|
||||||
19
Problem2.cpp
Normal file
19
Problem2.cpp
Normal file
@@ -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;
|
||||||
|
}
|
||||||
18
Problem2.hpp
Normal file
18
Problem2.hpp
Normal file
@@ -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
|
||||||
41
Problem3.cpp
Normal file
41
Problem3.cpp
Normal file
@@ -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<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;
|
||||||
|
}
|
||||||
19
Problem3.hpp
Normal file
19
Problem3.hpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#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
|
||||||
16
ProblemBase.hpp
Normal file
16
ProblemBase.hpp
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user