30

# 例

``````   1  2  3  4
-----------
1| 1  2  3  4
|
2| 2  4  6  8
|
3| 3  6  9 12
|
4| 4  8 12 16
``````

``````1, 2, 3, 4, 6, 8, 9, 12, 16
``````

``[16,12,9,8,6,4,3,2,1]``
``````1
2
3
4
6
8
9
12
16``````
``16 12 9 8 4 3 2 1``

# 测试用例

``````N=1 -> [1]
N=2 -> [1, 2, 4]
N=3 -> [1, 2, 3, 4, 6, 9]
N=4 -> [1, 2, 3, 4, 6, 8, 9, 12, 16]
N=5 -> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 20, 25]
N=6 -> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 30, 36]
N=7 -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 28, 30, 35, 36, 42, 49]
N=8 -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 28, 30, 32, 35, 36, 40, 42, 48, 49, 56, 64]
N=9 -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, 28, 30, 32, 35, 36, 40, 42, 45, 48, 49, 54, 56, 63, 64, 72, 81]
N=10 -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, 28, 30, 32, 35, 36, 40, 42, 45, 48, 49, 50, 54, 56, 60, 63, 64, 70, 72, 80, 81, 90, 100]
N=11 -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 27, 28, 30, 32, 33, 35, 36, 40, 42, 44, 45, 48, 49, 50, 54, 55, 56, 60, 63, 64, 66, 70, 72, 77, 80, 81, 88, 90, 99, 100, 110, 121]
N=12 -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 27, 28, 30, 32, 33, 35, 36, 40, 42, 44, 45, 48, 49, 50, 54, 55, 56, 60, 63, 64, 66, 70, 72, 77, 80, 81, 84, 88, 90, 96, 99, 100, 108, 110, 120, 121, 132, 144]
``````

TanMath

N可以多大？
xsot

1
@xsot您可以假设N * N会小于您的语言的通常最大int值（可能是2 ^ 31-1）

gregsdennis 2015年

1
@gregsdennis不。没有很多复合材料。例如91，92，93，94，95，96为N = 10

12

# Pyth，8个字节

``````S{*M^SQ2
``````

@FryAmTheEggman输入5已经需要排序，否则输出中的10和9会混乱。

Maltysen

13

# Python 2，61 51字节

``````lambda n:sorted({~(i%n)*~(i/n)for i in range(n*n)})
``````

1
`set(...)`可以只是一组比较`{...}`。此外，此处默认情况下允许使用函数，因此您只需编写即可`lambda n:...`
xnor

xsot

xnor

11

# APL，18 16字节

``````{y[⍋y←∪,∘.×⍨⍳⍵]}
``````

``````             ⍳⍵]}   ⍝ Get the integers from 1 to the input
∘.×⍨       ⍝ Compute the outer product of this with itself
,           ⍝ Flatten into an array
∪            ⍝ Select unique elements
y←             ⍝ Assign to y
{y[⍋               ⍝ Sort ascending
``````

7

# CJam，14 12个字节

``````{)2m*::*0^\$}
``````

@Martin提出了另一个`{,:)_ff*:|\$}`同样长度的非常优雅的解决方案（）。我使用了aittsu的那个，因为它与我的原始解决方案非常相似。

``````{     Start anonymous function.
)     Increment to get N+1.
2m*   Cartesian power, to get all pairs of numbers in range [0, N].
::*   Reduce all pairs with multiplication.
0^    Remove 0, and remove duplicates at the same time since this is a set operation.
\$     Sort the list.
}     End anonymous function.
``````

2

Martin Ender

1

5

# 八度，22字节

``````@(n)unique((a=1:n)'*a)
``````

4

# 朱莉娅，24个字节

``n->sort(∪((x=1:n)*x'))``

``````function f(n::Integer)
# Construct a UnitRange from 1 to the input
x = 1:n

# Compute the outer product of x with itself
o = x * transpose(x)

# Get the unique elements, implicitly flattening
# columnwise into an array
u = unique(o)

# Return the sorted output
return sort(u)
end``````

4

## zsh，86 56字节

``(for a in {1..\$1};for b in {1..\$1};echo \$[a*b])|sort -nu``

``````(                      # begin subshell
for a in {1..\$1}     # loop through every pair of multiplicands
for b in {1..\$1}
echo \$[a*b]      # calculate a * b, output to stdout
) | sort -nu           # pipe output of subshell to `sort -nu', sorting
# numerically (-n) and removing duplicates (-u for uniq)``````

1

4

# Ruby，50个48字节

``````->n{c=*r=1..n;r.map{|i|c|=r.map{|j|i*j}};c.sort}
``````

``````->n {
c=*r=1..n
r.map { |i| c|=r.map{|j|i*j} }
c.sort
}
``````

50字节

``````->n{r=1..n;r.flat_map{|i|r.map{|j|i*j}}.uniq.sort}
``````

``````->n{c=*r=1..n;r.map{|i|c|=r.map{|j|i*j}};c.sort}[4]
=> [1, 2, 3, 4, 6, 8, 9, 12, 16]
``````

3

# R，39个字节

``cat(unique(sort(outer(n<-1:scan(),n))))``

2

# Mathematica，25个字节

``````Union@@Array[1##&,{#,#}]&
``````

2

# K，17个字节

``````t@<t:?,/t*\:t:1+!
``````

``````  t@<t:?,/t*\:t:1+!5
1 2 3 4 5 6 8 9 10 12 15 16 20 25
``````

2

``````import Data.List
f n=sort\$nub[x*y|x<-[1..n],y<-[1..x]]
``````

`nub` 从列表中删除重复的元素。

2

## J，21 20字节

``````/:~@~.@,@(1*/~@:+i.)
``````

2

## Kotlin，70个字节

``````val a={i:Int->(1..i).flatMap{(1..i).map{j->it*j}}.distinct().sorted()}
``````

``````val a: (Int) -> List<Int> = {
i -> (1..i).flatMap{ j -> (1..i).map{ k -> j * k } }.distinct().sorted()
}
``````

``````fun main(args: Array<String>) {
for(i in 1..12) {
println(a(i))
}
}
``````

2

# 壳牌+通用工具41

``seq -f"seq -f%g*%%g \$1" \$1|sh|bc|sort -nu``

# Bash + coreutils，48岁

``eval printf '%s\\n' \\$[{1..\$1}*{1..\$1}]|sort -nu``

`\\$[{1..n}*{1..n}]`扩展为算术扩展`\$[1*1] \$[1*2] ... \$[1*n] ... \$[n*n]`，将其求值并传递给`printf`，每行打印一个，管道传递给`sort`

# 纯重打击（60）

``````eval a=(\$(eval echo [\\$[{1..\$1}*{1..\$1}\]]=1))
echo \${!a[@]}``````

1

## Minkolang 0.14，25 22 18字节

``````1nLI20P[x*1R]sS\$N.
``````

### 说明

``````1                     Push a 1 onto the stack
n                    Take number from input (n)
L                   Pushes 1,2,...,n onto the stack
I                  Pushes length of stack so 0P knows how many items to pop
2                 Pushes 2 (the number of repeats)
0P               Essentially does itertools.product(range(1,n+1), 2)
[              Open for loop that repeats n^2 times (0P puts this on the stack)
x             Dump (I know each product has exactly two numbers
*            Multiply
1R          Rotate 1 step to the right
]         Close for loop
s        Sort
S       Remove duplicates ("set")
\$N.    Output whole stack as numbers and stop.
``````

1

# JavaScript（ES6），92个 90字节

``n=>eval(`for(r=[],a=n;a;a--)for(b=n;b;)~r.indexOf(x=a*b--)||r.push(x);r.sort((a,b)=>a-b)`)``

## 说明

``````n=>eval(`                 // use eval to remove need for return keyword
for(r=[],a=n;a;a--)     // iterate for each number a
for(b=n;b;)           // iterate for each number b
~r.indexOf(x=a*b--) // check if it is already in the list, x = value
r.sort((a,b)=>a-b)      // sort the results by ascending value
// implicit: return r
`)``````

## 测试

``````N = <input type="number" oninput="result.innerHTML=(

n=>eval(`for(r=[],a=n;a;a--)for(b=n;b;)~r.indexOf(x=a*b--)||r.push(x);r.sort((a,b)=>a-b)`)

)(+this.value)" /><pre id="result"></pre>``````

1

## Perl 6，27个字节

``````{squish sort 1..\$_ X*1..\$_} # 27
{unique sort 1..\$_ X*1..\$_} # 27
{sort unique 1..\$_ X*1..\$_} # 27``````

``````say {squish sort 1..\$_ X*1..\$_}(3); # (1 2 3 4 6 9)␤

my \$code = {squish sort 1..\$_ X*1..\$_}

for 1..100 -> \N { say \$code(N) }

my &code = \$code;

say code 4; # (1 2 3 4 6 8 9 12 16)␤``````

1

``f n=[i|i<-[1..n*n],elem i[a*b|a<-[1..n],b<-[1..n]]]``

``````f n=filter(`elem`[a*b|a<-[1..n],b<-[1..n]])[1..n*n]
``````

``````f n=[k|k<-[1..n*n],any(\a->k`mod`a<1&&k<=n*a)[1..n]]
``````

ბიმო

1

# JAVA-86字节

``````Set a(int a){Set s=new TreeSet();for(;a>0;a--)for(int b=a;b>0;)s.add(a*b--);return s;}
``````

# 不打高尔夫球

``````Set a(int a){
Set s = new TreeSet();
for (;a>0;a--){
for(int b = a;b>0;){
}
}
return s;
}
``````

1

## Pyth，11个字节

``````S{sm*RdSdSQ
``````

1

# PHP，74,73 70字节

``````while(\$i++<\$j=\$n)while(\$j)\$a[]=\$i*\$j--;\$a=array_unique(\$a);sort(\$a);

print_r(\$a); // Not counted, but to verify the result
``````

``````while(\$i++<\$j=\$n)
while(\$j)
\$a[]=\$i*\$j--;
``````

`while((\$j=\$i++)<\$n)for(;\$j++<\$n;)\$a[]=\$i*\$j;\$a=array_unique(\$a);sort(\$a);`

1

# TeaScript，37个 35个字符；40字节

@Downgoat节省了2个字节

TeaScript是用于高尔夫的JavaScript。

``````(b+r(1,+x¬)ßam(z=>z*l±s`,`.u¡s»l-i)
``````

### 脱节和解释

``````(b+r(1,+x+1)m(#am(z=>z*l)))s(',').u()s(#l-i)
// Implicit: x = input number
r(1,+x+1)     // Generate a range of integers from 1 to x.
m(#           // Map each item "l" in this range "a" to:
am(z=>       //  a, with each item "z" mapped to
z*l))       //   z * l.
(b+      )    // Parse this as a string by adding it to an empty string.
s(',')        // Split the string at commas, flattening the list.
.u()          // Take only the unique items from the result.
s(#l-i)       // Sort by subtraction; the default sort sorts 10, 12, 100, etc. before 2.
// Implicit: output last expression
``````

Downgoat 2015年

manatwork 2015年

@manatwork这将是ISO / IEC_8859-1编码格式的 35个字节。但是我不确定TeaScript是否支持该编码，因此现在将其更改为40个字节。
ETHproductions's

0

# C，96字节

``````i,a[1<<16];main(n){for(scanf("%d",&n);i<n*n;a[~(i%n)*~(i++/n)]="%d ");while(i)printf(a[i--],i);}
``````

0

# JavaScript（ES6），86个字节

``````n=>{n++;a=[];for(j=1;j<n;j++)for(i=1;i<n;i++)if(a.indexOf(i*j)<0)a.push(i*j);return a}
``````

0

# Perl 5，91个字节

``````for my \$y (1 .. \$ARGV[0]){
map {\$s{\$n}++ unless(\$s{\$n=\$y*\$_}) } (\$y .. \$ARGV[0])
}
print join(" ", sort {\$a<=>\$b} keys %s) . "\n";
``````

0

## Python，124102字节

``````n=input()
l=[1]
for i in range(1,n+1):
for j in range(1,n+1):l.append(i*j)
print sorted(list(set(l)))
``````

2

1

El'endia Starman

3

Kartik

@ El'endiaStarman编辑，谢谢！
TanMath

1

El'endia Starman

0

# Perl 5，67位元组

``````for\$i(1..(\$n=pop)){\$a{\$_*\$i}++for 1..\$n}map say,sort{\$a<=>\$b}keys%a
``````