import java.util.ArrayList; public class LowerHalfDivisors { static private ArrayList[] getDivisors(int n) { // 'divisors' contains the lower half of the divisors of n, at index n ArrayList[] divisors = new ArrayList[n+2]; for (int i = 1; i <= n; i++) { divisors[i]=new ArrayList(); } // we only want the lower half of non-trivial divisors // so we only iterate to sqrt(n) and skip 1 // using a sieve approach to mark all multiples of i for (int i = 2; i*i <= n; ++i) { // start at i^2 since we only want the lower half of divisors for (int j = i*i; j <= n; j += i) divisors[j].add(i); } return divisors; } // prints all terms of sequence less than or equal to n public static void printSequence(int n) { ArrayList[] divisors = getDivisors(n); // iterate over each integer to check if it is in the sequence for (int i=1; i<=n; i++) { ArrayList divs=divisors[i]; if (divs.size()>1) { int prod=1; for (int d:divs) { prod*=d; } if (prod==i) { System.out.print(i+", "); } } } } public static void main(String[] args) { printSequence(1000); } }