us37605
BAN USERIs my code efficient? If not, then how can i improve the time complexity?
import java.io.*;
import java.util.*;
class RangePrime{
static ArrayList<Integer> list;
public static void main(String args[]) throws Exception{
Scanner sc= new Scanner(System.in);
list= new ArrayList<Integer>();
int L=sc.nextInt();
int R=sc.nextInt();
for(int i=L;i<=R;i++)
{
if(isPrime(i)==false)
list.add(i);
}
int arr[]= new int[10];
for(int j=0;j<list.size();j++)
{
String str=Integer.toString(list.get(j));
if(str.length()<2)
{ if(str=="0")
arr[0]=arr[0]+1;
else if(str=="1") arr[1]+=1;
else if(str=="2") arr[2]+=1;
else if(str=="3") arr[3]+=1;
else if(str=="4") arr[4]+=1;
else if(str=="5") arr[5]+=1;
else if(str=="6") arr[6]+=1;
else if(str=="7") arr[7]+=1;
else if(str=="8") arr[8]+=1;
else if(str=="9") arr[9]+=1;
}
else
{
for(int i=0;i<str.length();i++)
{
if(str.contains("0")) arr[0]+=1;
else if(str.contains("1")) arr[1]+=1;
else if(str.contains("2")) arr[2]+=1;
else if(str.contains("3")) arr[3]+=1;
else if(str.contains("4")) arr[4]+=1;
else if(str.contains("5")) arr[5]+=1;
else if(str.contains("6")) arr[6]+=1;
else if(str.contains("7")) arr[7]+=1;
else if(str.contains("8")) arr[8]+=1;
else if(str.contains("9")) arr[9]+=1;
}
}
}
int max=1;
int result=0;
for(int x=0;x<arr.length;x++)
{ if(max<arr[x])
{
result=x;
max=arr[x];
}
}
System.out.println(result);
}
static boolean isPrime(int num)
{
boolean flag=false;
for(int i=2;i<=Math.sqrt(num);i++)
{
if(num%i==0)
flag=true;
}
return flag;
}
}
Is my code efficient? If not, then how can i improve the time complexity?
}
- us37605 August 10, 2016