Partition an integer x into n primes: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: is_prime() now builtin) |
|||
Line 1,075: | Line 1,075: | ||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
This can be done with IntegerPartitions: |
|||
{{output?|Mathematica}} |
|||
<lang Mathematica>partition[x_,n_]:= "Partitioned "<>ToString[x]<>" with "<>ToString[n]<>" primes: "<>StringRiffle[ |
|||
⚫ | |||
,"+"] |
|||
partition[18, 2] |
|||
{{incorrect|Mathematica| <br><br> the partitioning of '''40,356''' into three primes isn't the lowest primes that are possible, <br> |
|||
partition[19, 3] |
|||
the primes should be: <br><br> <big> '''3''', '''139''', '''40213'''. </big> <br>}} |
|||
⚫ | |||
Just call the function F[X,N] |
|||
<lang Mathematica>F[x_, n_] := |
|||
⚫ | |||
StringRiffle[ |
|||
ToString /@ |
|||
Reverse[First@ |
|||
⚫ | |||
Length@Union@# == n &], Last]], "+"]] |
|||
⚫ | |||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Partitioned |
Partitioned 18 with 2 primes: 5+13 |
||
⚫ | |||
Partitioned 20 with 4 primes: impossible |
|||
</pre> |
</pre> |
||