果冻,27 25字节
⁽0(ḃs
Ɠḃd2Ḥ’×€Ç
3r5DṭÇæ×/
这是@AndersKaseorg的Haskell答案中树方法的实现,具有不同的分支顺序。该程序使用基于0的索引,并从STDIN获取输入。
在线尝试!
背景
如Wikipedia页面上的原始勾股三叉树的树中所述,每个PPT可以通过将行向量(3,4,5)反复左乘以具有某些属性的矩阵来获得。
在每次迭代中,可以将先前的结果与A,B或C左乘,可以如下选择。
当A,B和C固定时,可以以唯一的方式获得每个PPT。
怎么运行的
3r5DṭÇæ×/ Main link. No arguments.
3 Set the argument and the return value to 3.
r5 Create a range from 3 to 5, i.e., [3, 4, 5].
D Decimal; convert each integer to base 10, yielding [[3], [4], [5]].
Ç Call the second helper link with argument 3.
ṭ Tack; append [[3], [4], [5]] to the result.
æ×/ Reduce by matrix multiplication.
Ɠḃd2Ḥ’×€Ç Second helper link. Argument: 3
Ɠ Read and evaluate one line of input, yielding an integer n.
ḃ Convert n to bijective base 3.
d2 Divmod 2; map each digit d to [d/2, d%2].
Ḥ Unhalve; multiply the results by 2.
’ Decrement the doubled results.
The previous four atoms apply the following mapping to the digits.
1 -> [0, 1] -> [0, 2] -> [-1, 1]
2 -> [1, 0] -> [2, 0] -> [ 1, -1]
3 -> [1, 1] -> [2, 2] -> [ 1, 1]
Ç Call the helper link with argument 3, yielding the following 2D array.
[[ 1, 2, 2],
[ 2, 1, 2],
[ 2, 2, 3]]
×€ Multiply each [-1, 1], [ 1, -1], and [ 1, 1] by that matrix, using
vectorizing multiplication (not matrix multiplication), yielding one
of the following three 2D arrays.
[[-1, 2, 2], [[ 1, -2, 2], [[ 1, 2, 2],
[-2, 1, 2], [ 2, -1, 2], [ 2, 1, 2],
[-2, 2, 3]] [ 2, -2, 3]] [ 2, 2, 3]]
⁽0(ḃs First helper link. Argument: 3
⁽0( Numeric literal; yield 13041.
ḃ Convert 13041 to bijective base 3, yielding [1, 2, 2, 2, 1, 2, 2, 2, 3].
s Split the result into chunks of length 3, yielding the aforementioned
2D array.