-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem012.cs
175 lines (147 loc) · 4.52 KB
/
Problem012.cs
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace Euler
{
class Problem012
{
public int Run()
{
int triangle = 1;
int i = 1;
var primes = GetPrimesUsingSieveOfSundaram(40);
Console.WriteLine(string.Join(", ", primes));
var result = GetDivisors(primes);
return result;
return FindTriangularIndex(500);
IEnumerable<int> divisors = new List<int>();
int mostFactors = 0;
do
{
i++;
triangle += i;
divisors = GetDivisors(triangle);
if (divisors.Count() > mostFactors)
{
mostFactors = divisors.Count();
Console.WriteLine("{0}: {1}", triangle, mostFactors);
}
// Console.WriteLine("{0}: {1}", triangle, string.Join(",", GetDivisors(triangle)));
} while (divisors.Count() + 1 < 500);
Console.WriteLine("{0}: {1}", triangle, string.Join(",", divisors));
return triangle;
}
private static List<BigInteger> GetPrimesUsingSieveOfSundaram(int topCandidate = 1000000)
{
int totalCount = 0;
int k = topCandidate / 2;
var myBa1 = new BitArray(k + 1);
List<BigInteger> primes = new List<BigInteger>();
/* SEIVE */
int maxVal = 0;
int denominator = 0;
for (int i = 1; i < k; i++)
{
denominator = (i << 1) + 1;
maxVal = (k - i) / denominator;
for (int j = i; j <= maxVal; j++)
{
myBa1[i + j * denominator] = true;
}
}
BigInteger sum = 2;
for (int i = 1; i < k; i++)
{
if (!myBa1[i])
{
totalCount++;
primes.Add((i << 1) + 1);
//sum += (i << 1) + 1; // dummy contains prime number.The code is here not ignore the prime number calcuation part.
}
}
return primes;
//return sum;
//return (totalCount + 1); // 2 will be missed in Sieve Of Sundaram
}
private int GetDivisors(List<BigInteger> primes)
{
int t = 1;
int a = 1;
int cnt = 0;
//int i;
int P = primes.Count;
while (cnt < 6)
{
cnt = 1;
a = a + 1;
BigInteger tt = t;
for (int i = 0; i < P; i++)
{
if (primes[i]*primes[i] > tt)
{
cnt = 2*cnt;
break;
}
int exponent = 1;
while (tt%primes[i] == 0)
{
exponent++;
tt = tt/primes[i];
}
if (exponent > 1)
cnt = cnt*exponent;
if (tt == 1)
break;
}
}
return t;
}
static IEnumerable<int> GetDivisors(int n)
{
return Enumerable.Range(2, n/2).Where(a => n%a == 0);
}
private int FindTriangularIndex(int factorLimit)
{
int n = 1;
int lnum = NumDivisors(n);
int rnum = NumDivisors(n + 1);
while (lnum * rnum < factorLimit)
{
n += 1;
lnum = rnum;
rnum = NumDivisors(n + 1);
}
return n;
}
private int NumDivisors(int n)
{
if (n%2 == 0)
n = n/2;
int divisors = 1;
int count = 0;
while (n%2 == 0)
{
count++;
n = n/2;
}
divisors = divisors*(count + 1);
int p = 3;
while (n != 1)
{
count = 0;
while (n%p == 0)
{
count ++;
n = n/p;
}
divisors = divisors*(count + 1);
p += 2;
}
return divisors;
}
}
}