(알고리즘) 에라토스테네스의 체
https://namu.wiki/w/에라토스테네스%20체
public class Eratos {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
int N=Integer.parseInt(reader.readLine());
ArrayList<Boolean> primList=new ArrayList<Boolean>(N+1);
primList.add(false);
primList.add(false);
for(int i=2;i<=N;i++)
primList.add(i,true);
for(int i=2;(i*i)<=N;i++){
if(primList.get(i)){
for(int j=i*i;j<=N;j+=i)primList.set(j,false);
}
}
StringBuffer sb=new StringBuffer();
sb.append("{");
for(int i=0;i<=N;i++){
if(primList.get(i)==true)
{
sb.append(i);
sb.append(",");
}
}
sb.setCharAt(sb.length()-1, ')');
String str=new String(sb);
System.out.println(str);
}
}
public class Eratos {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
int N=Integer.parseInt(reader.readLine());
ArrayList<Boolean> primList=new ArrayList<Boolean>(N+1);
primList.add(false);
primList.add(false);
for(int i=2;i<=N;i++)
primList.add(i,true);
for(int i=2;(i*i)<=N;i++){
if(primList.get(i)){
for(int j=i*i;j<=N;j+=i)primList.set(j,false);
}
}
StringBuffer sb=new StringBuffer();
sb.append("{");
for(int i=0;i<=N;i++){
if(primList.get(i)==true)
{
sb.append(i);
sb.append(",");
}
}
sb.setCharAt(sb.length()-1, ')');
String str=new String(sb);
System.out.println(str);
}
}
댓글
댓글 쓰기