Skip to content

Instantly share code, notes, and snippets.

@Sam-Belliveau
Created August 1, 2017 00:25
Show Gist options
  • Save Sam-Belliveau/30954320b256d3522756ed2e1d2a4ca6 to your computer and use it in GitHub Desktop.
Save Sam-Belliveau/30954320b256d3522756ed2e1d2a4ca6 to your computer and use it in GitHub Desktop.
#include <iostream> // cout and cin
#include <cmath> // sqrt()
using namespace std; // cout and cin
void prime(long long int number) {
// Establishing Variables
long long int i = 5;
int w = 2;
const long long int lim = sqrt(number);
// Gets 2 and 3 out of the way
if (number % 2 == 0) { cout << number << " is not Prime. \n"; return; }
if (number % 3 == 0) { cout << number << " is not Prime. \n"; return; }
while (i <= lim) {
if (number % i == 0) { cout << number << " is not Prime. \n"; return; }
// Tests Number
i = i + w; // Increments number
w = 6 - i; // We already tested 2 and 3
}
cout << number << " is Prime. \n"; return;
}
int main()
{
long long int input; // Establishes Variable
while (1) {
cout << "Enter Number: ";
cin >> input; // Enter Input
if (cin.fail()) { cout << "\nInput Failed!\n\n\n"; cin.clear(); cin.ignore(INT64_MAX, '\n'); } // Check For Failed Input
else { prime(input); cout << "\n\n"; } // Print Result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment