prime numbers
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 ...
prime numbers
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 ...
how to find prime numbers in a range
class main{
public static void main(String [] args){
Scanner sc = new Scanner(System.in); // Scanner function should be imported//
int a = sc.nextInt();
int c = 0;
for(int i=2;i<=a;i++) {
for(int j=2;j<=i;j++) {
if(i==j) {
c++;
}
if(i%j==0) {
break;
}
}
}
System.out.println("\nTotal prime numbers ("+a+") - "+c);
}
}
prime number in a range
//Prime Numbers & Sieve of Eratosthenes
#include <iostream>
#include<math.h>
using namespace std;
int count_prime(int n)
{
bool bo[n+1];
bo[0]=false;
bo[1]=false;
for(int i=2;i<=n;i++)
{
bo[i]=true;
}
int count=0;
for(int i=2;i<=sqrt(n);i++)
{
for(int j=2*i;j<=n;j=j+i)
{
bo[j]=false;
}
}
for(int i=2;i<=n;i++)
{
if(bo[i]==true)
{
count++;
}
}
return count;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int ans=count_prime(n);
cout<<ans<<endl;
}
return 0;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us