void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
}
void loop() {
int x1 = 0 << 1;
int y1 = 0 << 1;
int x2 = 11 << 1;
int y2 = 11 << 1;
// for (int x1 = 1; x1 <= 1000000; x1++) {
int varX = abs(x1 - x2);
int varY = abs(y1 - y2);
int rootXY = integer_sqrt(968); // this took 15 seconds so is 3 times quicker
// int rootXY = integer_sqrt(varX * varX + varY * varY); // this took 15 seconds so is 3 times quicker
// int sqrXY = sqrt(varX * varX + varY * varY); // this took 45 seconds when tested on its own
// int sqrXY = sqrt(pow(varX,2) + pow(varY,2)); // this took 81 seconds when tested on its own with pow(*,2)
// Serial.print("rootXY: ");
// Serial.print(rootXY);
// Serial.print(", sqrXY: ");
// Serial.print(sqrXY);
// Serial.print(", Difference: ");
// Serial.println(abs(sqrXY - rootXY));
// }
Serial.print("rootXY: ");
Serial.println(rootXY);
Serial.print("Done! Seconds taken: ");
Serial.println(millis() / 1000.0, 2);
delay(10); // this speeds up the simulation
while (1) {};
}
unsigned int integer_sqrt(unsigned int n) {
unsigned int root = 0;
unsigned int bit = 1 << 30; // The highest bit in a 32-bit
Serial.print("bit: ");
Serial.println(bit);
Serial.print("n: ");
Serial.println(n);
while (bit > n)
bit >>= 2; // Shift the bit to the right by 2, essentially dividing it by 4
Serial.print("bit: ");
Serial.println(bit);
while (bit != 0) {
if (n >= root + bit) {
n -= root + bit;
root = (root >> 1) + bit;
Serial.print("root: ");
Serial.println(root);
} else {
root >>= 1;
Serial.print("root: ");
Serial.println(root);
}
bit >>= 2;
Serial.print("bit: ");
Serial.println(bit);
}
return root;
}