Last active
December 25, 2015 22:29
-
-
Save RSquaredSoftware/7050304 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <cmath> | |
#include <stdlib.h> | |
using namespace std; | |
int main() { | |
int data_size = 0; // size of data set | |
int n = 0; // Number to test for prime-ness | |
int number_of_primes = 0; | |
bool is_prime = true; // Boolean flag | |
ifstream input("numbers.txt"); | |
if (!input) { | |
cout << "Error in opening file. "; | |
} | |
string number; | |
getline(input,number); | |
//get first number which tells us how many numbers to test | |
cout << number << endl; | |
data_size = atoi(number.c_str()); | |
// test each number to see if it is prime | |
for (int i = 0; i < data_size; i++) | |
{ | |
input >> n; | |
// TODO: Boolean flag, assume true until proven otherwise | |
is_prime = true; | |
// TODO: Test for prime by checking for divisibility | |
// by all whole numbers from 2 to sqrt(n). | |
// If a number divides evenly, set your boolean flag to false | |
for (int j = 2; j <= sqrt(n); ++j) { | |
if (n % j == 0) { | |
is_prime = false; | |
break; | |
} | |
} | |
// Print result of the test | |
if (is_prime) | |
{ | |
cout << n << " is prime." << endl; | |
number_of_primes++; | |
} | |
else | |
cout << n << " is not prime." << endl; | |
} | |
cout << endl << "There were a total of " << number_of_primes | |
<< " prime numbers in the file." << endl; | |
input.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment