import java.util.*;class M{ static List c(int n){ List<Integer> l = new ArrayList(); int i = 1; for(; i++ < n; l.add(i)); for(i = 1; i++ < n;){ for(int x : l){ if(i != x & x%i < 1 & l.indexOf(i) >= 0){ l.remove((Integer)i); l.remove((Integer)x); break; } } } return l; } public static void main(String[] a){ System.out.println(Arrays.toString(c(2).toArray())); System.out.println(Arrays.toString(c(6).toArray())); System.out.println(Arrays.toString(c(15).toArray())); System.out.println(Arrays.toString(c(20).toArray())); System.out.println(Arrays.toString(c(22).toArray())); }}
Standard input is empty
[2] [5] [8, 9, 11, 12, 13, 15] [11, 12, 13, 15, 17, 19, 20] [12, 13, 15, 17, 19, 20, 21]
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!