Created
March 18, 2018 20:21
-
-
Save Koepel/13b9dd42cdca50710a4dd0ffe3826854 to your computer and use it in GitHub Desktop.
Arduino sketch for Goldbach's conjecture.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arduino IDE 1.8.5, Arduino Uno. | |
// 2018 | |
// Warning: Just a test, it is still full of bugs. | |
// | |
// No use of tables, everything is calculated again for every number. | |
unsigned long number = 4; // start with 4, it must be even and greater than 2 | |
void setup() | |
{ | |
Serial.begin( 9600); | |
Serial.println(); | |
Serial.println( "GoldBach's Conjunction Analyzer"); | |
} | |
void loop() | |
{ | |
// All the numbers that will be tested for prime. | |
// Number 1 is also a prime, but that is not part of GoldBach's Conjunction. | |
// Since 10=3+5 and also 10=5+3, the tested numbers are only up to halfway. | |
boolean startOfLine = false; | |
for( unsigned long i = 2; i <= (number/2); i++) | |
{ | |
if( isPrime( i)) // is it a prime number ? | |
{ | |
unsigned long diff = number - i; | |
if( isPrime( diff)) | |
{ | |
if( !startOfLine) | |
{ | |
startOfLine = true; | |
Serial.print( number); | |
} | |
Serial.print( " = "); | |
Serial.print( i); | |
Serial.print( " + "); | |
Serial.print( diff); | |
} | |
} | |
} | |
if( startOfLine) | |
{ | |
Serial.println(); | |
} | |
number += 2; // only even numbers | |
} | |
boolean isPrime( unsigned long x) | |
{ | |
boolean prime = true; | |
if( x > 2) // number 2 is a prime, no need to test that | |
{ | |
if( x % 2 == 0) // even number above 2 ? | |
{ | |
prime = false; | |
} | |
else | |
{ | |
for( unsigned long i = 2; i <= SquareRoot( x); i++) // number to divide the input with | |
{ | |
if( x % i == 0) | |
{ | |
prime = false; // found a number that could divide it. | |
break; // break for-loop, it is not a prime | |
} | |
} | |
} | |
} | |
return( prime); | |
} | |
// Find the (unsigned long) integer square root. | |
// --------------------------------------------- | |
// There is code from c.snippets.org | |
// "All source code free as noted" according to Bob Stout from snippets.org | |
// | |
// Craig McQueen at stackoverflow.com used a Wikipedia article. | |
// | |
// Explanation: | |
// https://stackoverflow.com/questions/1100090/looking-for-an-efficient-integer-square-root-algorithm-for-arm-thumb2 | |
// | |
// | |
/** | |
* \brief Fast Square root algorithm | |
* | |
* Fractional parts of the answer are discarded. That is: | |
* - SquareRoot(3) --> 1 | |
* - SquareRoot(4) --> 2 | |
* - SquareRoot(5) --> 2 | |
* - SquareRoot(8) --> 2 | |
* - SquareRoot(9) --> 3 | |
* | |
* \param[in] a_nInput - unsigned integer for which to find the square root | |
* | |
* \return Integer square root of the input value. | |
*/ | |
uint32_t SquareRoot(uint32_t a_nInput) | |
{ | |
uint32_t op = a_nInput; | |
uint32_t res = 0; | |
uint32_t one = 1uL << 30; // The second-to-top bit is set: use 1u << 14 for uint16_t type; use 1uL<<30 for uint32_t type | |
// "one" starts at the highest power of four <= than the argument. | |
while (one > op) | |
{ | |
one >>= 2; | |
} | |
while (one != 0) | |
{ | |
if (op >= res + one) | |
{ | |
op = op - (res + one); | |
res = res + 2 * one; | |
} | |
res >>= 1; | |
one >>= 2; | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment