```
/*
* File: main.cpp
* Author: K0ns3rv
* Email: info@k0nserv.se
* Description: Utility tool when working with prime numbers
* Created on den 11 juni 2010, 19:12
*
* Usage: Use, modify and distribute as you wish. I do like credit :)
*/
#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
#include <vector.h>
/*
* Checks if a given number is a prime number e.g only evenly divisible by itself and 1
*/
bool isPrime (int n) {
for ( int i = 2; i < n; i++ ) {
if ( n % i == 0 ) {
return false;
}
}
return true;
}
/*
* Display the command syntax
*/
void displayOptions () {
std::cout << "Invalid arguments " << std::endl;
std::cout << "The following arguments are valid " << std::endl;
std::cout << "-s Finds all numbers in the range 0 to second argument. Usage -s [max] [output] [filepath]" << std::endl;
std::cout << "-f Factorize a given number, the second argument, as prime numbers. Usage -f [number]" << std::endl;
std::cout << "-r Find all numbers in range first argument to second argument. Usage -r [min] [max] [output] [filepath]" << std::endl;
std::cout << "-c Check if given number is a prime. Usage -c [number]" << std::endl;
}
/*
* Find all prime numbers in the the interval lowerRange to range
* outputNumbers will ,if true, write the found primes the console.
* OutputPath will, if defined, write the numbers to a file
* array will, if true, return the numbers as an std::vector<int>
*/
std::vector<int> find (int range, bool outputNumbers, char* outputPath = NULL, bool array = false, int lowerRange = 2) {
std::vector<int> primes;
int startPos; //Starting position in the input file
bool fileOutput = true;
ofstream file;
if ( !array ) {
std::cout << "Min: " << lowerRange << std::endl;
std::cout << "Max: " << range << std::endl;
}
if ( outputPath != NULL ) {
std::cout << "Selected output path: " << outputPath << std::endl;
}
else {
fileOutput = false;
}
std::cout << "Running... \n" << std::endl;
if ( fileOutput ) {
file.open ( outputPath, ios::app );
startPos = file.tellp ( );
}
for ( int i = lowerRange; i < range; i++ ) {
if ( isPrime ( i ) ) {
if ( array ) {
primes.push_back ( i );
}
if ( outputNumbers ) {
std::cout << i << std::endl;
}
if ( fileOutput ) {
file << i;
if ( i + 1 < range ) {
file << ",";
}
file << "\r\n";
}
}
}
if ( fileOutput ) {
std::cout << "Wrote " << (int) file.tellp ( ) - startPos << " bytes to " << outputPath << std::endl;
file.close ( );
}
return primes;
}
/*
* Factorize a given number in primes e.g 6 = 3 * 2 and 25 = 5^2
*/
void factorize (int value) {
bool running = true;
std::vector<int> primes = find ( value, false, NULL, true );
std::vector<int> result;
while ( running ) {
//std::cout << "value: " << value << std::endl;
if ( isPrime ( value ) ) {
result.push_back ( value );
running = false;
break;
}
for ( int loc = 0; loc < primes.size ( ); loc++ ) {
if ( value % primes.at ( loc ) == 0 ) {
//std::cout << "Divided by : " << primes.at(loc) << std::endl;
result.push_back ( primes.at ( loc ) );
value = value / primes.at ( loc );
break;
}
}
}
std::cout << "Result: ";
int numericResult = 1;
for ( int loc = 0; loc < result.size ( ); loc++ ) {
std::cout << result.at ( loc );
if ( !(result.back ( ) == result.at ( loc )) ) {
std::cout << " * ";
}
numericResult *= result.at ( loc );
}
std::cout << " = " << numericResult << std::endl;
}
/*
* Check if a given number is a prime number
*/
void checkPrime (int c) {
if ( isPrime ( c ) ) {
std::cout << c << " is a prime number" << std::endl;
}
else {
char* check;
std::cout << c << " is not a prime number, run \"-f " << c << "\" to find out what prime numbers is it's factors" << std::endl;
std::cout << "Would you like to do that now ? (Y/N): ";
std::cin >> check;
if ( !strcmp ( check, "Y\0" ) || !strcmp ( check, "y\0" ) ) {
factorize ( c );
}
}
}
int main (int argc, char** argv) {
if ( argc < 3 ) {//Invalid amount of arguments
displayOptions ( );
return 0;
}
else {
if ( !strcmp ( argv[1], "-f\0" ) || !strcmp ( argv[1], "-F\0" ) ) {//Factorize
factorize ( atoi ( argv[2] ) );
}
else if ( (!strcmp ( argv[1], "-s\0" ) || !strcmp ( argv[1], "-S\0" )) && argc > 3 ) {//search
if ( argc == 5 ) {
find ( atoi ( argv[2] ), !strcmp ( argv[3], "true\0" ), argv[4] );
}
else if ( argc == 4 ) {
find ( atoi ( argv[2] ), !strcmp ( argv[3], "true\0" ) );
}
else {
displayOptions ( );
}
}
else if ( (!strcmp ( argv[1], "-r\0" ) || !strcmp ( argv[1], "-R\0" )) && argc > 4 ) {//Search with min and max
if ( argc == 6 ) {
find ( atoi ( argv[3] ), !strcmp ( argv[4], "true\0" ), argv[5], false, atoi ( argv[2] ) );
}
else if ( argc == 5 ) {
find ( atoi ( argv[3] ), !strcmp ( argv[4], "true\0" ), NULL, false, atoi ( argv[2] ) );
}
else {
displayOptions ( );
}
}
else if ( (!strcmp ( argv[1], "-c\0" ) || !strcmp ( argv[1], "-c\0" )) ) {//check prime
checkPrime ( atoi ( argv[2] ) );
}
else {
displayOptions ( );
return 0;
}
}
return (EXIT_SUCCESS);
}
```

Recommended Topics