How to resolve the algorithm Factorial step by step in the Pascal programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Factorial step by step in the Pascal programming language

Table of Contents

Problem Statement

Write a function to return the factorial of a number. Solutions can be iterative or recursive. Support for trapping negative   n   errors is optional.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Factorial step by step in the Pascal programming language

Source code in the pascal programming language

function factorial(n: integer): integer;
 var
  i, result: integer;
 begin
  result := 1;
  for i := 2 to n do
   result := result * i;
  factorial := result
 end;

{$mode objFPC}{R+}
FUNCTION Factorial ( n : qword ) : qword;

    (*)
           Update for version 3.2.0
           Factorial works until 20! , which is good enough for me for now
           replace qword with dword and rax,rcx with eax, ecx for 32-bit
           for Factorial until 12!
    (*)

    VAR
	
	F:	qword;
	
    BEGIN

	asm 
	
		mov		$1,	%rax
		mov		 n,	%rcx
		
	.Lloop1:
			imul	%rcx,	%rax
			loopnz	.Lloop1
		
		mov	%rax,   F

	end;

	Result := F ;
	
    END;

PROGRAM EXBigFac ;

{$IFDEF FPC}
    {$mode objfpc}{$H+}{$J-}{R+}
{$ELSE}
    {$APPTYPE CONSOLE}
{$ENDIF}

(*)

        Free Pascal Compiler version 3.2.0 [2020/06/14] for x86_64
        The free and readable alternative at C/C++ speeds
        compiles natively to almost any platform, including raspberry PI *
        Can run independently from DELPHI / Lazarus

        For debian Linux: apt -y install fpc
        It contains a text IDE called fp

        https://www.freepascal.org/advantage.var

(*)


USES

    gmp;

    FUNCTION WriteBigNum ( c: pchar ) : ansistring ;

    CONST

        CrLf = #13 + #10 ;

    VAR
        i:              longint;
        len:            longint;
        preview:        integer;
        ret:    ansistring = '';
        threshold:      integer;

    BEGIN

        len		:=	length ( c ) ;
        WriteLn ( 'Digits:  ', len ) ;
        threshold	:= 12 ;
        preview 	:= len div threshold ;
        
        IF	( len < 91 ) THEN
            BEGIN
                FOR i := 0 TO len DO
                    ret:= ret + c [ i ] ;
            END
        ELSE
            BEGIN
                FOR i := 0 TO preview DO
                    ret:= ret + c [ i ] ;
                    ret:= ret + '...'  ;
                FOR i := len - preview -1  TO len DO
                    ret:= ret + c [ i ] ;
            END;
        ret:= ret + CrLf ;
        WriteBigNum := ret;
    END;

    FUNCTION BigFactorial ( n : qword ) : ansistring ;

    (*)
	See https://gmplib.org/#DOC
    (*)

    VAR
        S:	mpz_t;
        c:	pchar;

    BEGIN

        mpz_init_set_ui          ( S, 1 ) ;
        mpz_fac_ui               ( S, n ) ;
        c := mpz_get_str   ( NIL, 10, S ) ;
        BigFactorial := WriteBigNum ( c ) ;
        
    END;

BEGIN

    WriteLn ( BigFactorial ( 99 ) ) ;

END.

Output:
Digits:  156
93326215443944...00000000000000

function factorial(n: integer): integer;
 begin
  if n = 0
   then
    factorial := 1
   else
    factorial := n*factorial(n-1)
 end;

  

You may also check:How to resolve the algorithm String prepend step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Comments step by step in the C# programming language
You may also check:How to resolve the algorithm Find limit of recursion step by step in the Axe programming language
You may also check:How to resolve the algorithm Sierpinski carpet step by step in the Oforth programming language
You may also check:How to resolve the algorithm User input/Text step by step in the SPL programming language