In number theory, a weird number is a natural number that is abundant but not semiperfect (and therefore not perfect either).

In other words, the sum of the proper divisors of the number (divisors including 1 but not itself) is greater than the number itself (the number is abundant), but no subset of those divisors sums to the number itself (the number is not semiperfect).

For example:

 12 is not a weird number.
 It is abundant; its proper divisors 1, 2, 3, 4, 6 sum to 16 (which is > 12),
 but it is semiperfect, e.g.:     6 + 4 + 2 == 12.
 70 is a weird number.
 It is abundant; its proper divisors 1, 2, 5, 7, 10, 14, 35 sum to 74 (which is > 70),
 and there is no subset of proper divisors that sum to 70.


Task

Find and display, here on this page, the first 25 weird numbers.



Related tasks

 Abundant, deficient and perfect number classifications
 Proper divisors


See also

 OEIS: A006037 weird numbers
 Wikipedia: weird number
 MathWorld: weird number


def divisors(n)
    divs = [1]
    divs2 = []
 
    i = 2
    while i * i <= n
        if n % i == 0 then
            j = (n / i).to_i
            divs.append(i)
            if i != j then
                divs2.append(j)
            end
        end
 
        i = i + 1
    end
 
    divs2 += divs.reverse
    return divs2
end
 
def abundant(n, divs)
    return divs.sum > n
end
 
def semiperfect(n, divs)
    if divs.length > 0 then
        h = divs[0]
        t = divs[1..-1]
        if n < h then
            return semiperfect(n, t)
        else
            return n == h || semiperfect(n - h, t) || semiperfect(n, t)
        end
    else
        return false
    end
end
 
def sieve(limit)
    w = Array.new(limit, false)
    i = 2
    while i < limit
        if not w[i] then
            divs = divisors(i)
            if not abundant(i, divs) then
                w[i] = true
            elsif semiperfect(i, divs) then
                j = i
                while j < limit
                    w[j] = true
                    j = j + i
                end
            end
        end
        i = i + 2
    end
    return w
end
 
def main
    w = sieve(17000)
    count = 0
    max = 25
    print "The first %d weird numbers:\n" % [max]
    n = 2
    while count < max
        if not w[n] then
            print n, " "
            count = count + 1
        end
        n = n + 2
    end
    print "\n"
end
 
main()