Advertisement
vulture2600

primes

Mar 12th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. //prime number generator
  2.  
  3. #include <Wire.h>
  4. #include <Adafruit_MCP23017.h>
  5. #include <Adafruit_RGBLCDShield.h>
  6.  
  7. Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
  8. unsigned long check = 2;
  9. unsigned long found = 1;
  10. unsigned long timeNow = 0;
  11.  
  12. void setup() {
  13.   Serial.begin(57600);
  14.   lcd.begin(16, 2);
  15.   //while(!Serial);
  16.  
  17. }
  18.  
  19. void loop() {
  20.   if (isPrime(check) == 1) {
  21.     Serial.print(found);
  22.     Serial.print(F("  "));
  23.     Serial.println(check);
  24.     found ++;
  25.   }
  26.  
  27.   //if (check > 1000) {
  28.    // while(1);
  29.   //}
  30.  
  31.  
  32.   if (millis() - timeNow > 1000) {
  33.     lcd.setCursor (0, 0);
  34.     lcd.print(F("found "));
  35.     lcd.print(found);
  36.     lcd.setCursor(0, 1);
  37.     lcd.print(F("in "));
  38.     lcd.print(millis() / 1000);
  39.     lcd.print(F("s. "));
  40.     lcd.print(check);
  41.     timeNow = millis();
  42.   }
  43.   check ++;
  44.  
  45.  
  46. }
  47.  
  48. int isPrime(unsigned long test) {
  49.   for (unsigned long div = 2; div <= sqrt(test); div ++) {
  50.     if ((test % div == 0)) {
  51.       return 0;
  52.     }      
  53.   }
  54.   return 1;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement