Primitive

As references, please use the OEIS series A006036 for primitive semiperfect numbers, and A005835 for semiperfects.

Goal

Write a program or a function in any language.
It will take as input a number n as a function parameter or from STDIN/your language's closest alternative, and will output all the primitive semi-perfect numbers from 1 to n (inclusive).

The output must be formated as 6[separator]20[separator]28[separator]88... where [separator] is either as newline, a space or a comma. There must not be a starting [separator] nor a ending one.

Edit : you can leave a trailing newline

Examples

input :

5

output :

input :

20

output :

6
20

input :

100

output :

6 20 28 88

Scoring

I'd be glad you could leave an explanation of your golfed code once you think you're done golfing it!

As this challenge already have some nice answers, and is slowly getting quiet, I'll put an end to it. The winner of this code-golf will be decided on Monday 29th, 00:00 GMT. Well done to all of you that have answered, and good luck to those who will try to beat them :)

6 Answers
6

Pyth, 28 27 bytes

VQI}KhNsMyJf!%KTSNI!@JYeaYK
Implicit:
Y = []
Q = eval(input())
VQ for N in range(Q):
KhN K = N+1
f SN filter T over range(1, N)
!%KT the logical not of K%T.
This is the list of divisors of K.
J Store the list in J.
y Create all of its subsets.
sM Map each subset to its sum.
I}K If K is in that list: (If K is semiperfect)
I!@JY If the intersection of J (the divisors)
and Y (the list of primitive semiperfect numbers)
is empty:
aYK Append K to Y
e And print its last element, K.

\$\begingroup\$@AlexA. Thanks! It is necessary to append K to Y to build Y, which is needed elsewhere. However, I could do the printing separately, such as with aYKK instead of eaYK. It's 4 bytes either way, however.\$\endgroup\$
– isaacgJun 19 '15 at 15:03

ÆDṖŒPS€i - helper function to check if input is a semiperfect number
ÆD - list of divisors of input
Ṗ - except for the last one (the input)
ŒP - power set = every possible subset of divisors
S€ - sum of each subset
i - return truthy iff input is one of these
ÆDÇ€TL’ - helper function to check if input is a primitive semiperfect number
ÆD - list of divisors of input
Ç€ - replace each with if they are a semiperfect number, based on
the above helper function. If input is a primitive semiperfect
number, we get something like [0,0,0,0,0,94].
T - get all truthy values.
L’ - return falsy iff there is only one truthy value
RÇÐḟY - main link
R - Range[input]
ÇÐḟ - Filter out those elements which are not primitive semiperfect
numbers, based on the helper function
Y - join by newlines.

CJam, 54 bytes

This solution feels a bit awkward, but since there have been few answers, and none in CJam, I thought I'd post it anyway:

Lli),2>{_N,1>{N\%!},_@&!\_,2,m*\f{.*:+}N#)e&{N+}&}fNS*

A good part of the increment over the posted Pyth solution comes from the fact that, as far as I could find, CJam does not have an operator to enumerate all subsets of a set. So it took some work to complete that with available operators. Of course, if there actually is a simple operator I missed, I'll look kind of silly. :)

Explanation:

L Start stack with empty list that will become list of solutions.
li Get input N and convert to int.
),2> Build list of candidate solutions [2 .. N].
{ Start for loop over all candidate solutions.
_ Copy list of previous solutions, needed later to check for candidate being primitive.
N,1> Build list of possible divisors [1 .. N-1].
{N\%!}, Filter list to only contain actual divisors of N.
_ Check if one of divisors is a previous solution. Start by copying divisor list.
@ Pull copy of list with previous solutions to top of stack
&! Intersect the two lists, and check the result for empty. Will be used later.
\ Swap top two elements, getting divisor list back to top.
_, Get length of divisor list.
2, Put [0 1] on top of stack.
m* Cartesian power. Creates all 0/1 sequences with same length as divisor list.
\ Swap with divisor list.
f{.*:+} Calculate element by element product of all 0/1 sequences with divisors,
and sum up the values (i.e. dot products of 0/1 sequences with divisors).
The result is an array with all possible divisor sums.
N#) Find N in list of divisor sums, and covert to truth value.
e& Logical and with earlier result from primitive test.
{N+}& Add N to list of solutions if result is true.
}fN Phew! We finally made it to the end of the for loop, and have a list of solutions.
S* Join the list of solutions with spaces in between.

Julia, 161 149 bytes

This creates an unnamed function that accepts an integer as input and prints the numbers to STDOUT separated by a newline. To call it, give it a name, e.g. f=n->....

Ungolfed + explanation:

# Define a function that determines whether the input is semiperfect
# (In the submission, this is defined as a named inline function within the
# primary function. I've separated it here for clarity.)
function S(m)
# Get all integer arrays which sum to m
p = partitions(m)
# Filter the partitions to subsets of the divisors of m
d = filter(i -> i == unique(i) && length(i) > 1 && all(j -> m % j == 0, i), p)
# If d is nonempty, the input is semiperfect
!isempty(d)
end
# The main function
function f(n)
# Loop through all integers from 2 to n
for i = 2:n
# Determine whether i is semiperfect
if S(i)
# If no divisors of i are semiperfect, print i
!any(S, filter(k -> i % k == 0, 1:i-1) && println(i)
end
end
end

Your Answer

If this is an answer to a challenge…

…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.

…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.

…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.

More generally…

…Please make sure to answer the question and provide sufficient detail.

…Avoid asking for help, clarification or responding to other answers (use comments instead).

Code Golf Stack Exchange is a site for recreational programming competitions, not general programming questions. Challenges must have an objective scoring criterion, and it is highly recommended to first post proposed challenges in the Sandbox.