This is Discussion thread about Prime Visits
Discussion About Prime Visits
what is the error in my code
import java.util.Arrays;
import java.util.Scanner;
public class prime_visit {
static boolean prime[] = new boolean[1000001];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Arrays.fill(prime,false);
precompute();
int n=0,a=0,b=0;
n=scan.nextInt();
while(n-->0){
a=scan.nextInt();
b=scan.nextInt();
int count=0;
for(int i=a;i<=b;i++){
if(i==1)
continue;
if(prime[i]==false){
count++;
}
}
System.out.println(count);
}
}
public static void precompute(){
for(int i=2;i<1000001;i++){
if(prime[i]==true){
continue;
}else{
int mutitple = i*2;
while(mutitple<1000001){
prime[mutitple]=true;
mutitple+=i;
}
}
}
}
}
Why am i getting TLE in the below code
#include
using namespace std;
bool checkPrime(int n,long long int j=2){
if(n<=2){
return (n==2)?true:false;
}
if(n%j==0){
return false;
}
if((j*j)>n)
return true;
return checkPrime(n,j+1);
}
int main() {
int T;
cin>>T;
long long a,b,ans=0;
while(T-- && T<=1000){
ans=0;
cin>>a>>b;
if(a<=1000000 && b<=1000000){
for(long i=a;i<=b;i++){
if(checkPrime(i))
ans++;
}
cout<<ans<<endl;
}
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
bool isprime(int n){
// int count = 0;
if( n <= 1)
return false;
for( int j = 2; j <= n/2; j++){
if (n%j == 0){
return false;
}
}
return true;
}
int countprime (int a, int b){
int c =0;
for (int i = a; i <= b; i++){
if ( isprime(i)){
// cout << i << ’ ';
c++;
}
}
return c;
// cout << c << endl;
}
int main() {
int t, a, b;
cin >> t;
while(t>0 && t<=1000){
cin >> a >> b;
cout << countprime(a, b) << endl;
t--;
}
return 0;
}
Two of the three test cases are passing and I’m getting a TLE for the third test case, anyone help