

Definition

A factorion is a natural number that equals the sum of the factorials of its digits.



Example
 
145   is a factorion in base 10 because:


          1! + 4! + 5!   =   1 + 24 + 120   =   145 





It can be shown (see talk page) that no factorion in base 10 can exceed   1,499,999.



Task

Write a program in your language to demonstrate, by calculating and printing out the factorions, that:

   There are   3   factorions in base   9
   There are   4   factorions in base 10
   There are   5   factorions in base 11 
   There are   2   factorions in base 12     (up to the same upper bound as for base 10)


See also

 Wikipedia article
 OEIS:A014080 - Factorions in base 10
 OEIS:A193163 - Factorions in base n


 
program Factorions;
 
{$APPTYPE CONSOLE}
 
uses
  System.SysUtils;
 
begin
  var fact: TArray<UInt64>;
  SetLength(fact, 12);
 
  fact[0] := 0;
  for var n := 1 to 11 do
    fact[n] := fact[n - 1] * n;
 
  for var b := 9 to 12 do
  begin
    writeln('The factorions for base ', b, ' are:');
    for var i := 1 to 1499999 do
    begin
      var sum := 0;
      var j := i;
      while j > 0 do
      begin
        var d := j mod b;
        sum := sum + fact[d];
        j := j div b;
      end;
      if sum = i then
        writeln(i, ' ');
    end;
    writeln(#10);
  end;
  readln;
end.