-
Notifications
You must be signed in to change notification settings - Fork 40
/
9 August "Problem of the Day" Answer
59 lines (53 loc) · 1.74 KB
/
9 August "Problem of the Day" Answer
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
GeeksforGeeks
The Question is :- "Nine Divisors" the code of the program is :-
Answer :-
class Solution{
public:
vector <long long> listPrime;
int countNotExceed(long long val, int n) {
int l = 0, r = n - 1, res = n;
while (l <= r) {
int mid = l + (r - l) / 2;
if (listPrime[mid] <= val)
l = mid + 1;
else {
r = mid - 1;
res = mid;
}
}
return res;
}
long long int nineDivisors(long long int N) {
//Code Here
vector <bool> isPrime(sqrt(N) + 1, true);
for (int p = 2; p * p <= sqrt(N); p++) {
if (isPrime[p]) {
for (int i = p * p; i <= sqrt(N); i += p)
isPrime[i] = false;
}
}
for (int i = 2; i <= sqrt(N); i++) {
if (isPrime[i])
listPrime.push_back(i * 1LL);
}
// Number present by p1^2 * p2^2
int n = listPrime.size();
long long res = 0;
for (int i = n - 1; i >= 0; i--) {
long long notExceedVal = floor(sqrt(N * 1.0) / listPrime[i]);
res += (countNotExceed(notExceedVal, i) * 1LL);
}
// Number present by p^8
for (int i = 0; i < n; i++) {
long long square = listPrime[i] * 1LL * listPrime[i];
if (square * square * square * square <= N)
res++;
else
break;
}
return res;
}
};
Hope you understand the answer, and complete it.
Stay Connected for daily Problem of the Day answers.
Thank you All!