15

# 测试用例

``````11 -> [11]
4 -> [2, 2]
39 -> [3, 13]
6 -> -1
1207 -> [17, 71]
393 -> -1
2352 -> [2, 2, 7, 3, 7, 2, 2]
``````

1

-1作为元素不同于仅包含-1的一个数组
RosLuP

4

# 05AB1E，7个字节

``````Òœ.ΔJÂQ
``````

``````Ò            # prime factorization of the input
œ           # permutations
.Δ         # find the first one such that
J        # concatenated
ÂQ      # is a palindrome
``````

`.Δ`方便地默认为-1，因此不需要额外的工作）

3

# Pyth，14个字节

-2字节@FryAmTheEggman

``````h+f_IjkT.pPQ_1
``````

``````h                 first element of
+                (append a -1 to the end in case the filter is empty)
f                 filter by lambda T:
_I                 is invariant under reversing
jkT              stringified list
.p                over permutations of
P Q             prime factors of Q with duplicates
_1              -1
``````

`jk``s`M`
Maltysen '16

3

# CJam-17个字节

``````Wqimfe!{s_W%=}=p;
``````

``````W              # Push a -1 onto the stack
q               # Get input
i               # Convert to integer
mf              # Find prime factorization
e!              # Find all permutations
{...}=          # Find permutation which...
s               # Convert to string
_               # Copy string
W%              # Get inverse
=               # Check if inverse == original
p;              # Print top of stack and discard the rest
``````

3

Martin Ender

3
@MartinBüttner这就是为什么我喜欢PPCG。每个人都非常乐于助人和友好！
KoreanwGlasses

2

## 红宝石，89 + 7 = 96 102 + 7 = 109

``````->n{n.prime_division.flat_map{|*a,b|a*b}.permutation.find{|x|x.join==x.join.reverse}||-1}
``````

`-rprime`国旗为+7 。

`flat_map`位是因为`prime_division`返回ex。`[[2, 2], [3, 1]]`用于输入`12`（代表）。`2231`

@histocrat那是OP的一个错误（请参阅问题注释）。谢谢，这是splat的巧妙技巧。
Doorknob

2

# 利亚，132个 122字节

``n->(x=filter(p->(q=join(p))==reverse(q),permutations(foldl(vcat,[[repeated(k,v)...]for(k,v)=factor(n)]))))==[]?-1:first(x)``

``````function f(n::Int)
# Construct an array of all prime factors of n
P = foldl(vcat, [[repeated(k, v)...] for (k, v) in factor(n)])

# Filter the set of permutations of this array to only
# those such that the string constructed by concatenating
# all elements is a palindrome
x = filter(p -> (q = join(p)) == reverse(q), P)

# If x is empty, return -1, otherwise get the first array
# in the collection
return x == [] ? -1 : first(x)
end``````

Glen O

Glen O

@GlenO一如既往的感谢！我最初尝试过`==[]`，但是却给我带来了错误，但现在我又尝试了一次，并且可以正常工作。我以前一定搞砸了。¯\ _（ツ）_ /¯我唯一无法使用的建议是摆脱`first`；在这种情况下，我必须使用，`first`因为它`x`是一个没有`getindex`定义的迭代器/集合/东西。
Alex A.

2

# Brachylog，10个字节

``````ḋp.cX↔X∨_1
``````

``````  .           The output is
p            a permutation of
ḋ             the prime factorization of
the input
c          such that concatenated
X         it is the variable X
↔        which reversed
X       is still X;
∨      if this is not possible,
the output is
_1    -1.
``````

1

``````import Data.Numbers.Primes
import Data.List
``````

1

# Perl 6，100字节

``{\$/=\$_;map(->\f{|({\$/%f||(\$//=f)&&f}...^*!==f)},2..\$_).permutations.first({.join.flip eq.join})||-1}``
``````{
# store copy of argument in \$/
\$/ = \$_;
# uses \$/ so that I don't have to declare a variable

# find the prime factors
map(
->\f{
# Slip so that outer list of all prime factors is flat
|(
{
\$/ % f    # return modulus
||        # or
(\$/ /= f) # factor the prime out of \$/
&&        # and
f         # return factor
}
# produce a list of them and
# stop when it returns something other than the factor
# also ignoring the last non-factor value
...^ * !== f
)
},
# find the factors out of the values from 2
# up to the original argument
2..\$_
# don't need to skip the non-primes as their
# prime factorization will have already be
# factored out of \$/
)

# try all permutations of the prime factors
.permutations

# find the first palindromic one
.first({ .join.flip eq .join })

# return -1 if .first returned Nil or empty list
|| -1
}``````

``````# give it a lexical name
my &prime-palindrome = {...}

say prime-palindrome    1; # -1
say prime-palindrome    2; # (2)
say prime-palindrome   11; # (11)
say prime-palindrome   13; # -1
say prime-palindrome   39; # (3 13)
say prime-palindrome   93; # (31 3)
say prime-palindrome    6; # -1
say prime-palindrome 1207; # (17 71)
say prime-palindrome  393; # -1
say prime-palindrome 2352; # (2 2 7 3 7 2 2)
say prime-palindrome 2351; # -1
say prime-palindrome 2350; # -1``````

``\$/=\$_;map(->\f{|({\$/%f||(\$//=f)&&f}...^*!= f)},2..\$_)``

``{.prime-factorize.permutations.first({.join.flip eq.join})||-1} # 63``

Jo King

1

# 果冻，16字节

``````ÆFŒṙŒ!VŒḂ\$ƇḢ¹-¹?
``````

``````ÆFŒṙŒ!VŒḂ\$ƇḢ¹-¹?
ÆFŒṙ                Get the prime factors (gets them as exponents then run-length decodes).
Œ!              Get the permutations.
Ƈ         Filter (keep) the ones that...
ŒḂ\$          ...are palindromic when...
V             ...joined.
Ḣ        Take the first.
¹?    If the value is truthy...
¹       ...return the value...
-      else return -1.
``````

1

# Japt`-F-1`，9字节

``````k á æ_¬êS
``````

RosLuP

@RosLuP解释器仍然很新。我将对创作者Shaggy进行ping操作。这是TIO链接
Oliver的

1
@RosLuP，您使用的是什么浏览器？

Windows Phone 8.1的Internet Explorer ：（手机）正在消失的某些东西，可能更好些，我使用我的全新手机android或Windows 10的浏览器（似乎他们叫Edge）
RosLuP，

0

# Japt，18个字节

``````Uk á f_¬¥Z¬w} g ªJ
``````

### 怎么运行的

``````        // Implicit: U = input, e.g. 2352
Uk      // Factorize the input.      [2,2,2,2,3,7,7]
á       // Take permutations.        [[2,2,2,2,3,7,7],[2,2,2,2,7,3,7],[2,2,2,7,2,3,7],...]
f_   }  // Filter to only the ones that return truthily to this function:
Z¬¥Z¬w  //  Return Z.join('') == Z.join('').reverse().
//                           [[2,2,7,3,7,2,2],[2,7,2,3,2,7,2],[7,2,2,3,2,2,7]]
g       // Take the first item.      [2,2,7,3,7,2,2]
ªJ      // If falsy, resort to -1.   [2,2,7,3,7,2,2]
``````

0

# 的JavaScript（ES6），256个244 208 187字节

@Neil节省了36个字节

``````x=>eval("for(a=[],i=2;x>1;x%i?i++:(a.push(i),x/=i));p=-1,f=(z,t=[])=>z[0]?z.map((u,i)=>f([...z.slice(0,i),...z.slice(i+1)],[...t,u])):(y=t.join``)==[...y].reverse().join``&&(p=t),f(a),p")
``````

Neil

@Neil谢谢，这恰好比我的算法快几倍！
ETHproductions 2016年

36个字节？我认为那一定是我的记录。

0

# APL（NARS），169个字符，338个字节

``````∇r←F w;i;k;a;m;j
r←⊂,w⋄→0×⍳1≥k←↑⍴w⋄a←⍳k⋄j←i←1⋄r←⍬⋄→C
A: m←i⊃w⋄→B×⍳(i≠1)∧j=m⋄r←r,m,¨∇w[a∼i]⋄j←m
B: i+←1
C: →A×⍳i≤k
∇
G←{F⍵[⍋⍵]}
f←{∨/k←{⍵≡⌽⍵}¨∊¨⍕¨¨v←Gπ⍵:↑k/v⋄¯1}
``````

G是找到排列的函数，而f是此练习的函数；测试：

``````  ⎕fmt f¨11 4 39 6 1207 393 2352
┌7───────────────────────────────────────────────────┐
│┌1──┐ ┌2───┐ ┌2────┐    ┌2─────┐    ┌7─────────────┐│
││ 11│ │ 2 2│ │ 3 13│ ¯1 │ 17 71│ ¯1 │ 2 2 7 3 7 2 2││
│└~──┘ └~───┘ └~────┘ ~~ └~─────┘ ~~ └~─────────────┘2
└∊───────────────────────────────────────────────────┘
``````