p07.cpp 971 Bytes
/*
10001st prime
Problem 7
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

by: dbk
*/

#include <stdio.h>

#define SIZE_ARRAY 10001

int main(int argc, char const *argv[])
{

	int *ptrPrime;
	int numerador = 1;
	int denominador = 2;
	int arrayPrime[SIZE_ARRAY] = { 0 };

	int counter = 0;
	int tmpCounter = 0;

	bool flgBuscar = true;

	do
	{

		flgBuscar = true;
		numerador = 1;
		ptrPrime = arrayPrime;

		do
		{

			while(*ptrPrime != 0 && flgBuscar)
			{
				numerador = *ptrPrime;
				flgBuscar = !(denominador % *ptrPrime++ == 0);

			}

			if( denominador % numerador == 0 && denominador == numerador )
			{
				*ptrPrime = denominador;
				flgBuscar = false;
				counter++;

			}
			else numerador++;

		}while(flgBuscar);

		denominador++;


	} while (arrayPrime[SIZE_ARRAY-1] == 0);

	printf("El primo #%d es:\t%d\n", SIZE_ARRAY, arrayPrime[SIZE_ARRAY-1]);

	return 0;
}