Exemplo n.º 1
0
/**
 Initiates the program.
 
 @param argc amount of args
 @param argv array of args
 */
int main( int argc, char *argv[] ) {
    int max;
    int* count = malloc(sizeof(int));
    *count = 0;
    
    if(argc > 0){
       max = atoi(argv[1]);
    } else {
        return 1;
    }
    
    FILE* f = fopen("output.txt", "w");
    
    clock_t start = clock();
    int* primes = calculatePrime(max, count);
    clock_t end = clock();
    printf( "%d\n", (int)((end - start) / 1000));
    
    // Array printing loop
    for(int i = 0; i < (*count); i++)
        fprintf( f, "%i\n", primes[i] );
    
    free( primes );
    
    return 0;
    
    
}
Exemplo n.º 2
0
bool Primer::runOnce()
{
	long num = 0;

	while (true)
	{
		std::cout << "Enter target prime (0 to exit, -1 for max): ";

		try
		{
			num = readLong();

			if (num < -1)
			{
				std::cout << "Negative numbers invalid, try again" << std::endl;
				continue;
			}

			break;
		}
		catch (std::exception&)
		{
			std::cout << "Invalid number, try again" << std::endl;
		}
	}

	if (num == 0)
	{
		return true;
	}
	else if (num == -1)
	{
		num = MAX_PRIME_NUM;
	}

	unsigned long prime = calculatePrime(num - 1);
	if (prime == 0)
	{
		std::cout << "Prime to large, try smaller number" << std::endl;
	}
	else
	{
		std::cout << "Prime #" << std::setw(10) << num << ": " << std::setw(10) << prime << std::endl;
	}

	return false;
}