Toph Number theory Problem Left Prime Solution
Solve in C++:
///**********ALLAH IS ALMIGHTY************/// ///AH Tonmoy ///Department of CSE ///Islamic University,Bangladesh #include<iostream> #include<math.h> const int n=10000000; bool marked[n+1]; int c[n+1]; using namespace std; void sieve() { marked[0] = marked[1] = true; for(int i=4; i<=n; i+=2) { marked[i] = true; } int sq=sqrt(n); for (int i = 3; i<=sq;i += 2) { for(int j=i*i; j<=n; j += i+i) { marked[j]=true; } } } void result() { int cnt = 0; for (int i = 0; i <= n; i++) { c[i] = cnt; if (!marked[i]) cnt++; } } int main() { sieve(); result(); int t, a; cin>>t; while (t--) { cin>>a; printf("%d\n", c[a]); } return 0; }
///**********ALLAH IS ALMIGHTY************/// ///AH Tonmoy ///Department of CSE ///Islamic University,Bangladesh #include<iostream> #include<math.h> const int n=10000000; bool marked[n+1]; int c[n+1]; using namespace std; void sieve() { marked[0] = marked[1] = true; for(int i=4; i<=n; i+=2) { marked[i] = true; } int sq=sqrt(n); for (int i = 3; i<=sq;i += 2) { for(int j=i*i; j<=n; j += i+i) { marked[j]=true; } } } void result() { int cnt = 0; for (int i = 0; i <= n; i++) { c[i] = cnt; if (!marked[i]) cnt++; } } int main() { sieve(); result(); int t, a; cin>>t; while (t--) { cin>>a; printf("%d\n", c[a]); } return 0; }
No comments