Projects
Projects
1. A device to check the primality of a number. (Using Arduino)
Idea: Suppose n is a given number. The idea is to check all the prime factors less than the square root of n. (n < 2147483647)
Code:
void setup() {
Serial.begin(9600); // Initialize Serial communication at 9600 bps
while (!Serial) {
; // Wait for serial port to connect (needed for native USB boards)
}
Serial.println("Enter a number to check if it is prime:");
}
void loop() {
// Check if user has entered data in the Serial Monitor
if (Serial.available() > 0) {
// Read the incoming integer
long number = Serial.parseInt();
// Consume any leftover newline/carriage return characters
while(Serial.available() > 0) {
Serial.read();
}
// Print the number being evaluated
Serial.print("Testing number: ");
Serial.println(number);
// Call the function and output the result
if (isPrime(number)) {
Serial.println("Result: It is a PRIME number.\n");
} else {
Serial.println("Result: It is NOT a prime number.\n");
}
Serial.println("Enter another number to check:");
}
}
// Function to check if a number is prime
bool isPrime(long n) {
// Corner cases
if (n <= 1) return false; // 0 and 1 are not prime numbers
if (n <= 3) return true; // 2 and 3 are prime numbers
// Exclude even numbers and multiples of 3
if (n % 2 == 0 || n % 3 == 0) return false;
// Optimized loop checking up to the square root of n
// Every prime number can be expressed in the form (6k +/- 1)
for (long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false; // Found a factor, so it's not prime
}
}
return true; // No factors found, it is prime
}
A Line follower and obstacle-avoiding robot (Initially with a load of 10kg)
An Electric motor