sa se scrie un program care va determina cate nr prime mai mici egale ca n sunt? repede va rog dau coroana
Răspunsuri la întrebare
#include <iostream>
using namespace std;
int n, num, nrprime;
bool prim(int m)
{
int p=1;
for (int j=3; j*j<=m; j+=2)
{
if (m%j==0) {p=0; break;}
}
return p==1;
}
int main()
{
cout << "n= "; cin >> n;
if (n<2) nrprime=0;
if (n==2) nrprime=1;
if (n>2)
{
nrprime=1;
for (num=3; num<=n; num+=2)
{
if (prim(num)) ++nrprime;
}
}
cout << nrprime;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll const mod=1000000007;
ll const md=998244353;
ll mypowr(ll a,ll b) {ll res=1;a%=mod; assert(b>=0);
for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll mypow(ll a,ll b) {ll res=1;assert(b>=0);
for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;}
ifstream in("elevi.in");
ofstream out("elevi.out");
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
int p(ll x){
if(x<=1)return 0;
if(x<=3)return 1;
if(x%2==0||x%3==0)return 0;
for(ll i=5;i*i<=x;i+=6){
if(x%i==0||x%(i+2)==0)return 0;
}
return 1;
}
int main()
{
ios_base::sync_with_stdio(0);
cout<<fixed<<setprecision(15);
cin.tie(0);cout.tie(0);
ll n,cnt=0;
cin>>n;
if(n>1)++cnt;
for(int i=3;i<=n;i+=2){
cnt+=p(i);
}
cout<<cnt<<'\n';
return 0;
}