Recent

Author Topic: !Num (Number Factorial)  (Read 7493 times)

nikel

  • Full Member
  • ***
  • Posts: 186
!Num (Number Factorial)
« on: July 23, 2021, 06:12:07 am »
Hello, can I do this in pascal:

When number is 3 I want it to multiply 3 to 1 (3 * 2 * 1).
When number is 5 it should multiply 5 * 4 * 3 * 2 * 1

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: !Num (Number Factorial)
« Reply #1 on: July 23, 2021, 06:47:14 am »
Basic stuff, that ;D
Code: Pascal  [Select][+][-]
  1. function factorial(num: Integer): Integer;
  2. var
  3.   i: Integer;
  4. begin
  5.   Result := num;
  6.   for i := num-1 downto 1 do
  7.     Result := Result * i;
  8. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: !Num (Number Factorial)
« Reply #2 on: July 23, 2021, 07:13:16 am »
 It would be good for a factorial function to ensure the number it received is greater than or equal to  zero (0) and, if it is zero, it should return 1 not zero ;)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

nikel

  • Full Member
  • ***
  • Posts: 186
Re: !Num (Number Factorial)
« Reply #3 on: July 23, 2021, 07:49:26 am »
Yes, that makes a number number.

1 * 0 = 0 (1 is a number)
200 * 0 = 0 (200 is a number)

But 0 * 0 = nonsense

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: !Num (Number Factorial)
« Reply #4 on: July 23, 2021, 07:58:44 am »
It would be good for a factorial function to ensure the number it received is greater than or equal to  zero (0) and, if it is zero, it should return 1 not zero ;)

You're right, of course, and I knew it. Don't know how I managed to skip it. And one should also check for negative numbers, which I forgot too ...  :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: !Num (Number Factorial)
« Reply #5 on: July 23, 2021, 11:01:52 am »
Since factorials will blow up rather fast (at least if you use an integer type as result: Fac(20) = 2432902008176640000 (or $21C3677C82B40000, Fac(21) is greater than largest possible QWORD), you might as well make a lookup table.

Bart

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: !Num (Number Factorial)
« Reply #6 on: July 23, 2021, 12:15:52 pm »
[...] you might as well make a lookup table.

Yes, but unless you're good at calculating factorials on your head you'd need a function such as the one I gave to calculate the LUT at least once in a lifetime.

Though if one really needed to calculate factorials for a living I guess one would use some BigNum lib or such, like one does to calculate Pi or find primes or things like that :D

Fact is the OP looks more like the tipical homework and I'm tempted to   replace the code in my first post for a nice explanation of how would go about building such a thing. My excuse is that it was around 7:00 after passing a night sleeping badly and I didn't think before replying. And it's such a basic thing ... :-X
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: !Num (Number Factorial)
« Reply #7 on: July 23, 2021, 01:44:40 pm »
Fact is the OP looks more like the tipical homework ...

Yep, and (s)he will get extra points for a recursive version, which (s)he must implement her/himself  ::)

Bart

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: !Num (Number Factorial)
« Reply #8 on: July 23, 2021, 02:33:21 pm »
Yep, and (s)he will get extra points for a recursive version, which (s)he must implement her/himself  ::)

Yeah, probably. Though I can't understand why people insist on implementing factorial recursively when a simple loop is more than enough. Factorials and similar operations must be the worst examples of recursive algorithms %) ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: !Num (Number Factorial)
« Reply #9 on: July 23, 2021, 03:14:19 pm »
hello,
Since factorials will blow up rather fast (at least if you use an integer type as result: Fac(20) = 2432902008176640000 (or $21C3677C82B40000, Fac(21) is greater than largest possible QWORD), you might as well make a lookup table.

with gmp library you can calculate big number and you have a factorial fonction :
Code: Pascal  [Select][+][-]
  1. procedure CalculateFactorial();
  2. Var
  3.  R1 : mpz_t;
  4.  resultat: String;
  5.  P_resultat: Pchar;
  6. begin
  7. mpz_init(@R1);
  8. mpz_fac_ui(@R1,100);
  9. P_resultat := StrAlloc(1024);
  10. gmp_sprintf(P_resultat, '100! = %Zd', @R1);
  11. resultat := P_resultat;
  12. Form1.Memo1.Append(resultat);
  13. end;  

Result in attachment

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: !Num (Number Factorial)
« Reply #10 on: July 23, 2021, 03:18:22 pm »
Fact is the OP looks more like the tipical homework and I'm tempted to   replace the code in my first post for a nice explanation of how would go about building such a thing. My excuse is that it was around 7:00 after passing a night sleeping badly and I didn't think before replying. And it's such a basic thing ... :-X

Assuming that only one person is using the login ID zhe's been here for years doing a variety of stuff including databases etc., so this is unlikely to be homework.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: !Num (Number Factorial)
« Reply #11 on: July 23, 2021, 03:34:47 pm »
Hi!

Here is the beloved recursive version. What a nonsense...

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var facStr : String;
  3.     fac,err: Integer;
  4.     facResult : Int64;
  5.  
  6.     procedure factorial (num,maxnum: Integer; var fresult: Int64);
  7.     begin
  8.     if num <= maxnum then
  9.       begin
  10.       fresult := Fresult * num;
  11.       factorial (num+1,maxnum,Fresult);
  12.       end;
  13.      end;
  14.  
  15. begin
  16.     facStr := InputBox('Factorial of:',
  17.     'Please type in', '');
  18.     val (facStr,fac,err);
  19.     if (err >0) or (fac<1) then
  20.       begin
  21.       ShowMessage ('Input error');
  22.       exit;
  23.    end;
  24.  
  25.     facResult := 1;
  26.     factorial(1,fac,FacResult);
  27.     showMessage ('Result: '+IntToStr(FacResult));
  28. end;

Winni
« Last Edit: July 23, 2021, 03:37:24 pm by winni »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: !Num (Number Factorial)
« Reply #12 on: July 23, 2021, 04:35:40 pm »
Assuming that only one person is using the login ID zhe's been here for years doing a variety of stuff including databases etc., so this is unlikely to be homework.

Where I abbreviated as OP I meant Original Post, which to me looks like the tipical exercise, but even so, the task is so basic that I can't think of an even moderately experienced programmer failing to do it without help, hence my assumption (strenghtened by his previous recent posts) that the original poster is if not an absolute beginner at least not very experienced. At least with Pascal.

Of course, I might be wrong (and I am with distressing frequency lately); if so, my excuses to whoever might feel slighted, which was very far from my intention.

Here is the beloved recursive version. What a nonsense...
[... etc ...]

And here the "classical" one:
Code: Pascal  [Select][+][-]
  1. function factorial(const num: Int64): Int64;
  2. { Just an example: no range checks, inappropiate types, etc. }
  3. begin
  4.   if num < 1 then
  5.     Result := 1
  6.   else
  7.     Result := num * factorial(num-1);
  8. end;
:D
Let's hope is not indeed homework; else we have just helped a student to cheat ...  :-\
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: !Num (Number Factorial)
« Reply #13 on: July 23, 2021, 04:52:23 pm »
It might be worth mentioning that it is possible to use threads to increase the speed, assuming your CPU has suitable cores. And if accurate answer is not necessary, it is possible to use logarithms to do the calculations much faster.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: !Num (Number Factorial)
« Reply #14 on: July 23, 2021, 05:04:38 pm »
Where I abbreviated as OP I meant Original Post, which to me looks like the tipical exercise, but even so, the task is so basic that I can't think of an even moderately experienced programmer failing to do it without help, hence my assumption (strenghtened by his previous recent posts) that the original poster is if not an absolute beginner at least not very experienced. At least with Pascal.

Well, I'm notorious for casual intolerance but look through the user's posting history, which goes back ten years. Now it /might/ be a student borrowing a parents' old account, or somebody who's found a user ID and password somewhere on the web, but it looks far more like an occasional business user or similar in which case we should be grateful that zhe's persevering with Lazarus and Pascal.

@nikel: what are you actually trying to do here? Are you trying to calculate n! as an end to itself, or are you trying to do some basic combinations/permutations stuff?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018