Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- #define PRIMETOFIND 1000 // how manyeth prime to find?
- int main()
- {
- unsigned int prime = 2; // last found prime (first prime is 2)
- unsigned int count = 1; // prime counter (first prime hardcoded to be 2, skipping)
- unsigned int number = 3; // start number (skipping 2 again)
- while(count < PRIMETOFIND)
- {
- bool isprime = true;
- for(unsigned int i = 3; i <= sqrt((long double)number); i+=2) // optimized to only check until square root of the current value and skipping even values
- {
- if(number % i == 0)
- {
- isprime = false;
- break;
- }
- }
- if(isprime)
- {
- prime = number;
- count++;
- }
- number += 2; // optimized to skip even numbers
- }
- cout << count << "th prime is " << prime << endl;
- cin.get(); // pausing the console
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement