:Q1y"XHyw)Zp?5MQ}1MtYf1)/H(8MyfHq=*+9M]]&
输出基于1。该程序对于在线解释器中的最后一个测试用例超时。
在线尝试!
说明
该程序将按照挑战中所述的步骤进行操作。为此,它异常地大量使用了MATL的手动和自动剪贴板。
获得最小除数作为素因数分解中的第一项。
通过覆盖数组A的相应条目来完成“除”更新。“添加”更新是通过将包含零的数组(除了所需位置)添加到A来逐元素添加到A来完成的。
:Q % Implicitly input n. Push array [2 3 ... n+1]. This is the initial array A.
% It contains all required positions. Some values will be overwritten
1 % Push 1. This is the initial value for p
y % Duplicate from below
" % For each loop. This executes the following n times.
% STACK (contents whosn bottom to top): A, p
XH % Copy p into clipboard H
y % Duplicate from below. STACK: A, p, A
w % Swap. STACK: A, A, p
) % Reference indexing. STACK: A, A[p]
Zp % Isprime. STACK: A, false/true
? % If true (that is, if A[p] is prime)
5M % Push p from automatic clipboard. STACK: A, p
Q % Add 1. STACK: A, p+1
} % Else (that is, if A[p] is not prime)
1M % Push A[p] from automatic clipboard. STACK: A, A[p]
t % Duplicate. STACK: A, A[p], A[p]
Yf % Prime factors, with repetitions. STACK: A, A[p], prime factors of A[p]
1) % Get first element, d. STACK: A, A[p], d
/ % Divide. STACK: A, A[p]/d
H % Push p from clipboard H. STACK: A, A[p]/d, p
( % Assignment indexing: write value. STACK: A with A[p] updated
8M % Push d from automatic clipboard.
y % Duplicate from below. STACK: A with A[p] updated, d, A with A[p] updated
f % Find: push indices of nonzero entries.
% STACK: A with A[p] updated, d, [1 2 ... n]
Hq % Push p from clipboard H, subtract 1.
% STACK: A with A[p] updated, d, [1 2 ... n], p-1
= % Test for equality, element-wise.
% STACK: A with A[p] updated, d, [0 ... 0 1 0 ... 0]
* % Multiply, element-wise. STACK: A with A[p] updated, [0 ... 0 d 0 ... 0]
+ % Add, element-wise. STACK: A with A[p-1] and A[p] updated
9M % Push p-1 from automatic clipboard.
% STACK: A with A[p-1] and A[p] updated, p-1
] % End if. The stack contains the updated array and index
] % End for each. Process the next iteration
& % Specify that the following implicit display function should display only
% the top of the stack. Implicitly display