Jordan-Pólya numbers: Difference between revisions
Content added Content deleted
(→{{header|J}}: bugfix) |
(→{{header|J}}: bonus) |
||
Line 45: | Line 45: | ||
F=. f{~1 i.~b #inv isjp Y#~b=. (]=<.) Y=. y%f=. jpfct y |
F=. f{~1 i.~b #inv isjp Y#~b=. (]=<.) Y=. y%f=. jpfct y |
||
F,showjp y%F |
F,showjp y%F |
||
}} |
|||
⚫ | |||
NB. generate a Jordan-Pólya of the given length |
|||
jpseq=: {{ |
|||
r=. 1 2x NB. sequence, so far |
|||
f=. 2 6x NB. factorial factors |
|||
i=. 1 0 NB. index of next item of f for each element of r |
|||
g=. 6 4x NB. product of r with selected item of f |
|||
while. y>#r do. |
|||
r=. r, nxt=. <./g NB. next item in r |
|||
j=. I.b=. g=nxt NB. items of g which just be recalculated |
|||
if. nxt={:f do. NB. need new factorial factor/ |
|||
f=. f,!2+#f |
|||
end. |
|||
i=. 0,~i+b NB. update indices into f |
|||
g=. (2*nxt),~((j{r)*((<:#f)<.j{i){f) j} g |
|||
end. |
|||
y{.r |
|||
⚫ | |||
Task: |
Task: |
||
<syntaxhighlight lang=J> 5 10$ |
<syntaxhighlight lang=J> 5 10$jpseq 50 |
||
1 2 4 6 8 12 16 24 32 36 |
1 2 4 6 8 12 16 24 32 36 |
||
48 64 72 96 120 128 144 192 216 240 |
48 64 72 96 120 128 144 192 216 240 |
||
Line 60: | Line 77: | ||
Note that jp factorizations are not necessarily unique. For example, <tt>120 120 6 6 6 2 2 2 2 2</tt> would also be a jp factorization of <tt>99532800</tt>. |
Note that jp factorizations are not necessarily unique. For example, <tt>120 120 6 6 6 2 2 2 2 2</tt> would also be a jp factorization of <tt>99532800</tt>. |
||
Bonus (indicated numbers from jp sequence, followed by a jp factorization): |
|||
<syntaxhighlight lang=J> s=: jpseq 4000 |
|||
(,showjp) (<:800){s |
|||
18345885696 24 24 24 24 24 24 24 2 2 |
|||
(,showjp) (<:1800){s |
|||
9784472371200 720 720 24 24 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 |
|||
(,showjp) (<:2800){s |
|||
439378587648000 87178291200 5040 |
|||
(,showjp) (<:3800){s |
|||
7213895789838336 24 24 24 24 24 24 24 24 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2</syntaxhighlight> |
|||
=={{header|jq}}== |
=={{header|jq}}== |