You are viewing a single comment's thread. Return to all comments →
Java 8
import java.util.Iterator; import java.util.LinkedList; import java.util.Scanner; import java.util.stream.LongStream; public class Solution { static long gcd(final long a, final long b) { return b == 0 ? a : gcd(b, a % b); } public static void main(String args[]) { try (final Scanner scanner = new Scanner(System.in)) { final int n = scanner.nextInt(); final long f = scanner.nextLong(); final LinkedList<Long> factors = new LinkedList<>(); for (long i = 1; i * i <= f; i++) { if (f % i == 0) { factors.add(i); if (i * i != f) { factors.add(f / i); } } } LongStream.generate(scanner::nextLong).limit(n).map(u -> gcd(u, f)).distinct().forEach(gcdU -> { for (final Iterator<Long> i = factors.iterator(); i.hasNext();) { if (gcdU % i.next() == 0) { i.remove(); } } }); System.out.println(factors.size()); } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Unfriendly Numbers
You are viewing a single comment's thread. Return to all comments →
Java 8