How to resolve the algorithm Jordan-Pólya numbers step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Jordan-Pólya numbers step by step in the J programming language

Table of Contents

Problem Statement

Jordan-Pólya numbers (or J-P numbers for short) are the numbers that can be obtained by multiplying together one or more (not necessarily distinct) factorials. 480 is a J-P number because 480 = 2! x 2! x 5!. Find and show on this page the first 50 J-P numbers. What is the largest J-P number less than 100 million? Find and show on this page the 800th, 1,800th, 2,800th and 3,800th J-P numbers and also show their decomposition into factorials in highest to lowest order. Optionally, do the same for the 1,050th J-P number. Where there is more than one way to decompose a J-P number into factorials, choose the way which uses the largest factorials. Hint: These J-P numbers are all less than 2^53.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Jordan-Pólya numbers step by step in the J programming language

Source code in the j programming language

F=. !P=. p:i.100x
jpprm=: P{.~F I. 1+]

Fs=. 2}.!i.1+{:P
jpfct=: Fs |.@:{.~ Fs I. 1+]

isjp=: {{
  if. 2>y do. y return.
  elseif. 0 < #(q:y)-.jpprm y do. 0 return.
  else.
    for_f. (#~ ] = <.) (%jpfct) y do.
      if. isjp f do. 1 return. end.
    end.
  end.
  0
}}"0

showjp=: {{
  if. 2>y do. i.0 return. end.
  F=. f{~1 i.~b #inv isjp Y#~b=. (]=<.) Y=. y%f=. jpfct y
  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
}}


   5 10$jpseq 50
   1    2    4    6    8   12   16   24   32   36
  48   64   72   96  120  128  144  192  216  240
 256  288  384  432  480  512  576  720  768  864
 960 1024 1152 1296 1440 1536 1728 1920 2048 2304
2592 2880 3072 3456 3840 4096 4320 4608 5040 5184
   <:^:(0=isjp)^:_]1e8
99532800
   showjp 99532800
720 720 24 2 2 2


   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


  

You may also check:How to resolve the algorithm Permutations by swapping step by step in the Sidef programming language
You may also check:How to resolve the algorithm Guess the number step by step in the Tcl programming language
You may also check:How to resolve the algorithm Loops/Nested step by step in the Octave programming language
You may also check:How to resolve the algorithm Discordian date step by step in the Haskell programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the B programming language