Output the ALONED numbers
Python 2, 90 79 73 bytes
-6 bytes thanks to xnor
L=range(2,input()+1)
while L[0]*2<=L[-1]:L.remove(L[0]*2);L=L[1:]
print L
Takes the input number on stdin. Ideone it!
Explanation
We construct the initial list from the input number and store it in L
. Next, loop while the last number is greater than or equal to 2 times the first number and remove 2 times the first number from the list. This will always be the next number divisible by L[0]
. L=L[1:]
takes off the first number as well. When the condition is no longer true, no further removals can be made, and the list is printed.
05AB1E, 22 17 15 14 bytes
L¦¹F¬·©¹›_i¦®K
Try it online!
Explanation
L¦ # push the list [2..input]
¹F # input nr of times do:
i # if
¬·© # the first element in the list * 2
¹›_ # is less than or equal to input
# then
¦ # remove first element of list
®K # and remove it's multiple
Python, 61 bytes
lambda n:[i+1for i in range(n/2,n)if-~i&~i&4**n/3>>(-~i&i<1)]
It's a bit easier to understand this less golfed code:
lambda n:[i for i in range(n/2+1,n+1)if((i&-i)**.5%1>0)^(i&~-i>0)]
This uses a direct characterization of aloned numbers:
A number
i
is aloned if, when decomposed asi = a * 2^b
withb
odd, either
a>1
andb
is even, ora==1
andb
is oddThe aloned numbers for
n
are all aloned numbersi
in the intervaln/2 + 1 <= i <= n
.
Why does this hold? When doing the process for n
, say we remove an odd number a
in the lower half (1
to n/2
). Then, 2*a
is removed no matter where in the list it is. So, 4*a
remains (if it existed). But if it's in the lower half, the deletion process will get to it and remove both 4*a
and 8*a
. So, we see that an upper-half number gets removed if it's of form 2*a
, 8*a
... with odd c
, but stays if it has form a
, 4*a
, 8*a
, ...
The exception is for a=1
, which does not start in the list and so is not removed. As a result, the removal chain starts with a=2
, and the rule for powers of 2 is flipped.
lambda n:[i for i in range(n/2+1,n+1)if((i&-i)**.5%1>0)^(i&~-i>0)]
In the code above, (i&-i)**.5%1>0
checks whether i
lacks the form i = a * 2^b
with b
odd, by the bit-trick to extract the greatest power-of-two factor, 2^b = i&-i
, then checking if the result is not a perfect square. Then, i&~-i>0
is another bit trick to check if i
is not a perfect power of 2. These conditions are then xor'ed.
There's some more improvements here
lambda n:[i+1for i in range(n/2,n)if-~i&~i&4**n/3>>(-~i&i<1)]
First, we shift the range 1 index down to to shorten to range(n/2,n)
from range(n/2+1,n+1)
, compensating by replacing all i
with i+1
(or ~-i
).
Whether a power of 2 is number is a power of 4
(2^b
with b
even) can be checked by and-ing with 2**c/3
for some large c
. This is because 2**c/3
has binary representation 10101...101
with ones in the even-positioned bits. Using c=2*n
suffices. To negate result when i
is a power of 2, we halve this number is that case, putting 1
's in the odd positions instead.