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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Jordan-Pólya numbers step by step in the Raku 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 Raku programming language

Source code in the raku programming language

# 20230719 Raku programming solution

my \factorials = 1, | [\*] 1..18; # with 0!

sub JordanPolya (\limit) {
   my \ix = (factorials.keys.first: factorials[*] >= limit) // factorials.end; 
   my ($k, @res) = 2, |factorials[0..ix];

   while $k < @res.elems {
      my \rk = @res[$k];
      for 2 .. @res.elems -> \l {
         my \kl = $ = @res[l] * rk;
         last if kl > limit;
         loop {
            my \p = @res.keys.first: { @res[$_] >= kl } # performance
            if p < @res.elems and @res[p] != kl {
               @res.splice: p, 0, kl
            } elsif p == @res.elems { 
               @res.append: kl 
            }
            kl > limit/rk ?? ( last ) !! kl *= rk
         }
      }
      $k++
   }
   return @res[1..*]
}

my @result = JordanPolya 2**30 ; 
say "First 50 Jordan-Pólya numbers:";
say [~] $_>>.fmt('%5s') for @result[^50].rotor(10);
print "\nThe largest Jordan-Pólya number before 100 million: ";
say @result.first: * < 100_000_000, :end;


  

You may also check:How to resolve the algorithm Harshad or Niven series step by step in the Clojure programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Undefined values step by step in the Java programming language
You may also check:How to resolve the algorithm Doubly-linked list/Definition step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Tau function step by step in the Modula-2 programming language