Jordan-Pólya numbers: Difference between revisions

Line 45:
F=. f{~1 i.~b #inv isjp Y#~b=. (]=<.) Y=. y%f=. jpfct y
F,showjp y%F
}}
}}</syntaxhighlight>
 
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
}}</syntaxhighlight>
Task:
<syntaxhighlight lang=J> 5 10$I.isjpjpseq i.1000050
1 2 4 6 8 12 16 24 32 36
48 64 72 96 120 128 144 192 216 240
Line 60 ⟶ 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>.
 
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}}==
6,951

edits