Skip to content

Commit

Permalink
Merge pull request #44 from Ankur-Dwivedi22/day_8medium
Browse files Browse the repository at this point in the history
chore: completed day 8 medium task author:Ankur-Dwivedi22
  • Loading branch information
opabhijeet authored Apr 7, 2024
2 parents 6064247 + ae964d9 commit c7d340f
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Medium/Day8/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
// Write Your code Here
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;

const int max_num = 1e7;
int is_prime[max_num + 1];
int prime_count[max_num + 1];
// prime_count[i] stores the number of primes up to i

int main() {
// Input the number of test cases
int test_cases;
cin >> test_cases;

// Initialize arrays
memset(is_prime, 0, sizeof(is_prime));
prime_count[1] = prime_count[0] = 0;

// Sieve of Eratosthenes algorithm to find primes
for (int x = 2; x <= max_num; x++) {
prime_count[x] = prime_count[x - 1];
if (!is_prime[x]) {
// Mark multiples of x as not prime
for (int cur = x * 2; cur <= max_num; cur += x) {
is_prime[cur] = 1;
}
// Increment prime count for each prime found
prime_count[x]++;
}
}

// Process each test case
while (test_cases--) {
// Input the value of n
int n;
cin >> n;

// Initialize ans to 0
int ans = 0;

// Check if n/2 is greater than or equal to 2
if (n / 2 >= 2)
ans++;

// Calculate ans by subtracting prime count up to n/2 from prime count up to n
ans += prime_count[n] - prime_count[n / 2];

// Output the answer for the current test case
cout << ans << endl;
}

return 0;
}

0 comments on commit c7d340f

Please sign in to comment.