Hey guys, I am new on here. I am a first year engineering student, and we are learning programming via arduino uno's.
Anyways, we had to write from scratch a prime number generator that would display each prime number between 2 and 101.
I have it to where it displays all prime numbers, but because 2 and 3 are special cases, I have my x>3, and my serial moniter is still spitting out 0,1,2,3 before the other prime numbers. I know I will eventually want it to show 2 and 3, but why am I getting 0 and 1??
Your help is very much appreciated.
( sorry for all the commenting, our proff requires it.)
Source code:
//Homework #3
// This program is designed to test each number from 2 to 101, and determine if they are prime or not.
// If they are prime, it will display them on the serial moniter.
#include<stdio.h>.
int primeMax = 101;
//sets variable primeMax to a constant integer of 101.
int primeMin = 3;
//sets the variable primeMin to a constant integer of 3.
boolean prime;
//sets prime so it returns only one bit, allows it to display true or false.
unsigned int d;
//sets variable d to be an integer.
unsigned int x ;
//sets variable x to be an integer.
void setup(){
//sets up objects for the program.
Serial.begin(9600);
//sets up the serial communications rate at 9600 bits per second.
}
void loop()
// continues the loop forever.
{
for ( x > primeMin; x <= primeMax; x++){
//says that x must be greater than 3, and x is less than or equal to 101.
// x increments by one every time it goes through the for loop.
prime = true;
//sets prime to equal true
for ( d=2; d <=ceil(sqrt(x)); d++)
// 'for' loop setting that d begins at 2, and d is less than or equal to the rounded square root of x( x being the number tested if prime). d increments one every cycle.
{
if (x%d ==0){
prime = false;
break;
/* this 'if' loops says that if x has a remiander of zero, there is no prime, and breaks to return to the inner loop for the next x value.*/
}// ends the 'if' statement
}// ends the 'for' loop setting up d
if (prime ==true){
Serial.println(x);
/* this 'if' statement saying that if a prime is found, then it will print it to the serial moniter*/
}//ends 'if' prime true statement
}//ends first 'for' loop.
}//ends of the main loop.
( sorry for all the commenting, our proff requires it.)
Never apologise for there being too many comments, unless they are really dumb comments where for example you give a really awesomely brilliant descriptive name to a variable and then redundantly describe it in the comment.
int rawValueOfTheTemperaturePinInVolts; //initialise the variable for the raw voltage of the temperature pin
.... is kind of redundant.
Otherwise comments are basically good things and tell Prof I said he's ok 8)
But PLEASE keep your comments up to date. Too often someone obviously wrote something like this at first:
delay(1000); //set a delay of 1000
.... which although redundant is at least true. But then they change the value but not the comment:
To check that a number is prime you only need test it against all the other prime numbers before it. You could build this list up as you go along something like this.
int primes[101];
int pcount=0;//number of prime numbers found so far
int primeMin=2;
int primeMax=101;
int test;
void setup()
{
Serial.begin(9600);
test=primeMin;
}
void loop()
{
int n;
for (n=0;n<pcount;n++)
{if ((test%primes[n])==0)
break; //break out of the for loop if a divisor is found
}
if(n==pcount) //if it completed the for loop we've found another prime number
{primes[pcount++]=test; //add it to the array
Serial.print(test,DEC); //and report to the user
Serial.println("");
}
test++; //update test for next go round.
if (test>primeMax)
while(1);//we're done so halt
}