-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_012.c
64 lines (52 loc) · 1.6 KB
/
problem_012.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// -*- compile-command: "gcc -std=c99 -o problem_012 problem_012.c ceuler.c -Wall -lm" -*-
// Copyright (c) 2014 Michael Caldwell
#include <stdio.h>
#include "ceuler.h"
int findDivisors(int input, int *primes, int nprimes)
{
int index, count=1;
int primeCount[nprimes];
for (index=0; index<nprimes; index++)
primeCount[index] = 0;
// Prime factorization
while (input != 1)
for (index=0; index<nprimes; index++)
if (input % primes[index] == 0)
{
input = input / primes[index];
primeCount[index]++;
}
// Convert prime factorization to divisors
for (index=0; index<nprimes; index++)
if (primeCount[index] > 0)
count *= (primeCount[index]+1);
return count;
}
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;
}