# 素数的映射

19

## 规则

• 您可以编写完整的程序或函数来执行此任务。
• 输出可以是可识别为序列的任何格式。
• 允许用于素数分解，素数测试等的内置。
• 不允许出现标准漏洞。
• 您的程序必须在10分钟内完成我机器上的最后一个测试用例。
• 这是代码高尔夫球，所以最短的代码获胜！

## 测试用例

• `10``{{},{{}},{}}`
• `21``{{{}},{},{{}}}`
• `42``{{{}},{},{{}},{}}`
• `30030``{{{}},{{}},{{}},{{}},{{}},{}}`
• `44100``{{{{}}},{{{}}},{{{}}},{},{}}`
• `16777215``{{{{}}},{{}},{{}},{},{{}},{{}},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{{}}}`
• `16777213`pastebin

@丹尼斯是的，您可以通过方括号告诉。
LegionMammal978

Akangka

Akangka

1

1

# Pyth，29个字节

``````L+'MhMtbmYhbL&JPby/LJf}TPTSeJ
``````

`y`给定素分解，辅助函数递归执行映射。基本情况和素分解在中执行`'`

5

# CJam，51 48 44 42 41 39 34 33 31字节

``````{mf_W=)1|{mp},\fe=(0a*+{)J}%}:J
``````

CJam解释器中在线尝试。

### 输入输出

``````[[""] "" [""] ""]
``````

``````[[[]] [] [[]] []]
``````

### 验证

``````\$ wget -q pastebin.com/raw.php?i=28MmezyT -O test.ver
\$ cat prime-mapping.cjam
ri
{mf_W=)1|{mp},\fe=(0a*+{)J}%}:J
~`
\$ time cjam prime-mapping.cjam <<< 16777213 > test.out

real    0m25.116s
user    0m23.217s
sys     0m4.922s
\$ diff -s <(sed 's/ //g;s/""/{}/g;y/[]/{}/' < test.out) <(tr -d , < test.ver)
Files /dev/fd/63 and /dev/fd/62 are identical
``````

### 怎么运行的

``````{                           }:J  Define a function (named block) J.
mf                              Push the array of prime factors, with repeats.
_W=                           Push a copy and extract the last, highest prime.
)1|                        Increment and OR with 1.
{mp},                   Push the array of primes below that integer.

If 1 is the highest prime factor, this pushes
[2], since (1 + 1) | 1 = 2 | 1 = 3.
If 2 is the highest prime factor, this pushes
[2], since (2 + 1) | 1 = 3 | 1 = 3.
If p > 2 is the highest prime factor, it pushes
[2 ... p], since (p + 1) | 1 = p + 2, where p + 1
is even and, therefor, not a prime.

\fe=               Count the number of occurrences of each prime
in the factorization.

This pushes [0] for input 1.

(              Shift out the first count.
0a*           Push a array of that many 0's.
+          Append it to the exponents.

This pushes [] for input 1.

{  }%     Map; for each element in the resulting array:
Increment and call J.
``````

`mf e=`比问题出在沙盒中时进行健全性测试时的结果要好得多，但是我发现您尚未使用的一项改进就是将两者的映射为`(0a*+`-即`ri{}sa2*{mf_W=){mp},\fe=(0a*+0j\{)j}%*}j`。而且还有一个更大的改进，我将在几小时内给您带来头绪...
Peter Taylor

@PeterTaylor感谢您的高尔夫和提示。

Peter Taylor

@PeterTaylor那个神奇`1|`。再次感谢！

2

# Mathematica，88个字节

``````f@1={};f@n_:=f/@Join[1+{##2},1&~Array~#]&@@SparseArray[PrimePi@#->#2&@@@FactorInteger@n]
``````

LegionMammal978
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.