IA-32 machine code, 27 bytes
Hexdump:
60 33 db 8b f9 33 c0 92 43 50 f7 f3 85 d2 75 04
ab 93 ab 93 3b c3 5a 77 ec 61 c3
Source code (MS Visual Studio syntax):
pushad;
xor ebx, ebx;
mov edi, ecx;
myloop:
xor eax, eax;
xchg eax, edx;
inc ebx;
push eax;
div ebx;
test edx, edx;
jnz skip_output;
stosd;
xchg eax, ebx;
stosd;
xchg eax, ebx;
skip_output:
cmp eax, ebx;
pop edx;
ja myloop;
popad;
ret;
First parameter (ecx
) is a pointer to output, second parameter (edx
) is the number. It doesn't mark the end of output in any way; one should prefill the output array with zeros to find the end of the list.
A full C++ program that uses this code:
#include <cstdint>
#include <vector>
#include <iostream>
#include <sstream>
__declspec(naked) void _fastcall doit(uint32_t* d, uint32_t n) {
_asm {
pushad;
xor ebx, ebx;
mov edi, ecx;
myloop:
xor eax, eax;
xchg eax, edx;
inc ebx;
push eax;
div ebx;
test edx, edx;
jnz skip_output;
stosd;
xchg eax, ebx;
stosd;
xchg eax, ebx;
skip_output:
cmp eax, ebx;
pop edx;
ja myloop;
popad;
ret;
}
}
int main(int argc, char* argv[]) {
uint32_t n;
std::stringstream(argv[1]) >> n;
std::vector<uint32_t> list(2 * sqrt(n) + 3); // c++ initializes with zeros
doit(list.data(), n);
for (auto i = list.begin(); *i; ++i)
std::cout << *i << '\n';
}
The output has some glitches, even though it follows the spec (no need for sorting; no need for uniqueness).
Input: 69
Output:
69
1
23
3
The divisors are in pairs.
Input: 100
Output:
100
1
50
2
25
4
20
5
10
10
For perfect squares, the last divisor is output twice (it's a pair with itself).
Input: 30
Output:
30
1
15
2
10
3
6
5
5
6
If the input is close to a perfect square, the last pair is output twice. It's because of the order of checks in the loop: first, it checks for "remainder = 0" and outputs, and only then it checks for "quotient < divisor" to exit the loop.
O(sqrt(n))
. \$\endgroup\$99 (1 3 9 11 33 99)
\$\endgroup\$