CJam, 51 48 44 42 41 39 34 33 31 ไบต์
{mf_W=)1|{mp},\fe=(0a*+{)J}%}:J
ลองออนไลน์ใน ล่าม CJam
ขอบคุณ @ MartinBüttnerสำหรับการเล่นกอล์ฟ 3 ไบท์!
ขอบคุณ @PeterTaylor สำหรับการตีกอล์ฟขนาด 3 ไบต์และปูทางไปอีก 1 ทาง!
อย่างน้อยในคอมพิวเตอร์ของฉันการดาวน์โหลดไฟล์ใช้เวลานานกว่าการรันโปรแกรม ...
I / O
นี่คือฟังก์ชั่นที่มีชื่อที่ปรากฏและจำนวนเต็มจาก STDIN และผลักดันอาร์เรย์ในการกลับมา
เนื่องจาก 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.