Java Arithmetic getProperDivisors(long n)

PreviousNext

//package com.java2s;

import java.util.HashSet;

import java.util.Set;

public class Main {
    public static Set<Long> getProperDivisors(long n) {
        Set<Long> allDivisors = getDivisors(n);
        allDivisors.remove(n);/*w   w  w  .  d   em  o    2s   .  c   o  m*/
        return allDivisors;
    }

    public static Set<Long> getDivisors(long n) {
        if (n < 1) {
            throw new IllegalArgumentException(String.format("%d is less than 1", n));
        }

        Set<Long> factors = new HashSet<>();

        for (long l = 1; l * l <= n; l++) {
            if (n % l == 0) {
                factors.add(l);
                factors.add(n / l);
            }
        }
        return factors;
    }
}
PreviousNext

Related