Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to find all the numbers that are equal to the sum of the factorials of their digits in Pascal

1 Answer

0 votes
program DigitFactorialNumbers;

const
  MaxDigit = 9;

function Factorial(n: Integer): LongInt;
var
  i, fact: LongInt;
begin
  fact := 1;
  for i := 2 to n do
    fact := fact * i;
  Factorial := fact;
end;

procedure FindDigitFactorialNumbers;
var
  factorials: array[0..MaxDigit] of LongInt;
  i, num, temp, digit, sum, upperLimit: LongInt;
begin
  // Precompute factorials of digits 0-9
  for i := 0 to MaxDigit do
    factorials[i] := Factorial(i);

  upperLimit := 7 * factorials[9]; // 7 * 9! is a safe upper bound

  for num := 10 to upperLimit do
  begin
    sum := 0;
    temp := num;

    while temp > 0 do
    begin
      digit := temp mod 10;
      sum := sum + factorials[digit];
      temp := temp div 10;
    end;

    if sum = num then
      writeln(num, ' is a digit factorial number.');
  end;
end;

begin
  FindDigitFactorialNumbers;
end.




(*
run:

145 is a digit factorial number.
40585 is a digit factorial number.

*)

 



answered Nov 9, 2025 by avibootz
...