Example #1
0
int main()
{
    printf("Project Euler - Problem 12:\n"
           "What is the value of the first triangle number to have over five hundred divisors?\n\n");

    // Begin time tracking
    struct timeval start;
    gettimeofday(&start, NULL);

    int index=0, triangle=0, divisors=0;
    int nprimes = 1500;
    int primes[nprimes];
    primeSieve(primes, nprimes);

    // Calculate Triangles
    index = 0;
    while(index<1000000)
    {
        index++;
        triangle += index;

        // Skip numbers not divisible by 3 or 5
        if ( (triangle % 3 != 0) || (triangle % 5 != 0) )
            continue;
        divisors = findDivisors(triangle, primes, nprimes);

        if (divisors > 500)
            break;
    }

    printf("Triangle Value: %d, Divisors: %d\n", triangle, divisors);
    printElapsedTime(start);

    return 0;
}
int main(){
	primeSieve();
	eulerCoprime();
	while(true){
		int n;
		scanf("%d",&n);
		if(!n)
			return 0;
		__int64 count=0;
		for(int i=2;i<=n;i++)
			count+=coprimeNum[i];
		printf("%I64d\n",count);
	}
}
Example #3
0
int main(int argc, char *argv[]) {

	const char *lim = argv[1];
	char *e;
	long limit = strtol(lim, &e, 0);

	long *primes = primeSieve(limit);
	long long sum = 0;
	long i;

	for (i = 0; i < limit; i++) {
		if (*(primes + i)) {
			sum += i;
		}
	}

	printf("The answer to problem 10 is %lld\n", sum);
	return 0;
}
Example #4
0
void Problem10::runProblem10(int sumPrimesUpToNumber) {
	//Create a list of primes
	int* primeList;
	int numPrimes = 0;

	//Generate primes
	std::cout << "Generate primes up to " << sumPrimesUpToNumber << std::endl;
	numPrimes = primeSieve(sumPrimesUpToNumber, primeList);
	std::cout << "Primes generated" << std::endl;

	long long int sumOfPrimes = 0;
	for (int i = 0; i < numPrimes; i++) {
		sumOfPrimes += primeList[i];
	}

	//Print result
	std::cout << "Sum of generated primes is: " << sumOfPrimes << std::endl;

}
Example #5
0
void main41(void)
{
    int primeSieveArr[C_SIZE_OF_PRIME_SIEVE];
    unsigned long long targetArr[C_SIZE_OF_TARGET_ARR];
    int targetCount;
    int numberOfPrimes;
	int i; // Adapied for VS2010

    //unsigned long long test;
    //printf_s("%d %d\n", sizeof(targetCount), sizeof(test));

    printf_s("Welcome to PrimeChecker!\n\n");

    // Skapar primtalssållet
    primeSieve(primeSieveArr, C_SIZE_OF_PRIME_SIEVE);
    do
    {
        do
        {
            printf_s("\nEnter the number of Integers to check: ");
            scanf_s("%d", &targetCount);
            flushRestOfLine();

            // Kollar att användaren inte matat in primtalet för tidigt
            if (10 <= targetCount)
            {
                printf_s("\nDo you realy want to check %d numbers?\n", targetCount);
            }
            else
            {
                break;
            }
        } while (1 == yesNoRepeater("Reenter the number of integers to check?"));

        // Läser in tal
        printf_s("Enter %d Integers: \n", targetCount);
        for (i = 0; i < targetCount; i++)
        {
            printf_s("Enter a number: ");
            scanf_s("%llu", &targetArr[i]);
            flushRestOfLine();
        }

        // Skriver ut talen
        printf_s("You entered the following: [ ");
        for (i = 0; i < targetCount; i++)
        {
            printf_s("%llu ", targetArr[i]);
        }
        printf_s("]\n");

        // Anropar keepPrimes algorithmen
        numberOfPrimes = keepPrimes(primeSieveArr, C_SIZE_OF_PRIME_SIEVE, targetArr, targetCount);

        // Skriver ut antalet primtal och vilka av talen som är primtal
        printf_s("You entered %d Prime(s) as follows: [ ", numberOfPrimes);
        for (i = 0; i < targetCount; i++)
        {
            if (-1 != targetArr[i])
            {
                printf_s("%llu ", targetArr[i]);
            }
        }
        printf_s("]\n");

    } while (1 == yesNoRepeater("Would you like to run the Primechecker again?"));


    printf_s("\nThank you for using PrimeChecker!\n\n");

} // main