-
Notifications
You must be signed in to change notification settings - Fork 0
/
HighlyDivisibleTriangularNumber.cpp
71 lines (59 loc) · 1.59 KB
/
HighlyDivisibleTriangularNumber.cpp
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
// This file is written by AlirezaKamyab
// Answer to problem projecteuler.net/problem=12
#include <iostream>
#include <cmath>
using namespace std;
bool* primeList(int limit);
int divisorsCount(bool* primeList, int number);
int main(){
int limit = 500;
bool* primes = primeList(1000);
int n = 1;
int n1 = n + 1;
while (true)
{
int countN = divisorsCount(primes, (n % 2 == 0)? n / 2 : n);
int countN1 = divisorsCount(primes, (n1 % 2 == 0)? n1 / 2 : n1);
if(countN * countN1 >= limit){
cout << n * (n1) / 2 << endl;
return 0;
}
n++;
n1 = n + 1;
}
return 0;
}
int divisorsCount(bool* primeList, int number){
int count = 1;
for(int i = 1; (2 * i + 1) * (2 * i + 1) <= number; i++){
if(primeList[i] == true) continue;
int n = number;
int exponent = 0;
while(n % (i * 2 + 1) == 0){
exponent++;
n /= (i * 2) + 1;
}
count *= (exponent + 1);
}
if(number % 2 == 0){
int exponent = 0;
while(number % 2 == 0){
exponent++;
number /= 2;
}
count *= (exponent + 1);
}
return count;
}
bool* primeList(int limit){
int sieveBound = (limit - 1) / 2;
int crossLimit = (floor(sqrt(limit)) - 1) / 2;
bool* primes = new bool[sieveBound] { 0 };
for(int i = 1; i <= crossLimit; i++){
if(primes[i] == true) continue;
for(int j = 2 * i * (i + 1); j <= sieveBound; j += (2 * i) + 1){
primes[j] = true;
}
}
return primes;
}