SRL Archive - Other Scripts

Permutation and Combinations


Blumblebee

2009-06-05 19:19:44

So with some help from Widget on irc, I finally managed to get a working factorial calculator (n!) and nCr (n Choose r: where order is not important) and nPr(n Pick r: where Order is Important). It works to a point, keep in mind use low decimals ish. ex. 40! results in over a 47 decimal chain which scar cannot process. Exceeding the limit of scars calculation abilities will result in Scar overflowing, meaning your calculations will be off etc, so just dont do it :p

Example:

Factorial(4);

returns

4! = 24


hope this somewhat helps anyone doing perms and coms, it helped me ;)

program New;

Function Factorial(n: Int64): Int64;
var
i: Int64;
a : integer;
begin
i := 1;
for a := 1 to n do
i := i * a;
Result := i;
writeln(Int64ToStr(n)+'! = '+Int64ToStr(Result));
end;

Function nCr(n, r: Int64): Int64;
var a, b, c, d, e: Int64;
begin
a := Factorial(n);
b := n-r;
c := Factorial(b);
d := c*Factorial(r);
e := a/d;
WriteLn(Int64ToStr(a)+' div '+Int64ToStr(d)+' = '+Int64ToStr(e));
Result := e;
writeln(Int64ToStr(n)+' C '+Int64ToStr(r)+' = '+Int64ToStr(Result));
end;

Function nPr(n, r: Int64): Int64;
var a, b, c, d: Int64;
begin
a := Factorial(n);
b := n-r;
c := Factorial(b);
d := a/c;
Result := d;
writeln(Int64ToStr(n)+' P '+Int64ToStr(r)+' = '+Int64ToStr(Result));
end;

So have fun with it, I might add some more stuff later, like calculating probability or something. (yeah the code isnt too efficient but idc)

Floor66

2009-06-05 19:22:03

Maths. Not my favourite subject but I have a 9 for it (that's an A I believe). I still don't get this advanced shit :p

Blumblebee

2009-06-05 19:24:01

yeah tell me about it, I have a final on this shit today (worth 15% of my grade, appearently its like a predimploma…) :( and I only have a 78% in math

Naum

2009-06-05 20:12:16

I guess you used Int 64 because it has the largest range in SCAR :).

The only problem with a Factorial is that it can go up really quickly, good work!

ian.

2009-06-05 20:13:50

What math class are you in? Algebra I? (Not being offensive! :D Just wondering because that's when I did this).

Freddy1990

2009-06-05 22:48:54

Factorial is already in SCAR as Fract

Da 0wner

2009-06-06 01:30:22

program New;

Function Factorial(n: Int64): Int64;
var
i: Int64;
a : integer;
begin
i := 1;
for a := 1 to n do
i := i * a;
Result := i;
end;

begin
Writeln(Factorial(24));
Writeln(Fract(24));
end.
:o

Blumblebee

2009-06-06 04:01:52

From: NaumanAkhlaQ
I guess you used Int 64 because it has the largest range in SCAR :).

The only problem with a Factorial is that it can go up really quickly, good work!


yeah I know…I used this for my hw to a point haha, when the numbers got really big I got in a bit of trouble. and thanks :)

From: 99_
What math class are you in? Algebra I? (Not being offensive! :D Just wondering because that's when I did this).


I'm in Canada, so this would be Math 30, equivlent to the last math class you will take in Highschool (aside from pre calculous and AP courses)

From: Freddy1990
Factorial is already in SCAR as Fract


Didnt know, and I made this more for understanding myself. I assume they both have similiar code. Can Fract() hold larger numbers?

From: Da 0wner
program New;

Function Factorial(n: Int64): Int64;
var
i: Int64;
a : integer;
begin
i := 1;
for a := 1 to n do
i := i * a;
Result := i;
end;

begin
Writeln(Factorial(24));
Writeln(Fract(24));
end.
:o


as I said, scar overflows when the numbers get too large. You do realize Factorial 24 (24!) is equivelent to 24x23x22x21x20…x1 Thats a way larger number than scar can handle ;)

Freddy1990

2009-06-06 17:04:01

From: Blumblebee
as I said, scar overflows when the numbers get too large. You do realize Factorial 24 (24!) is equivelent to 24x23x22x21x20…x1 Thats a way larger number than scar can handle ;)

Well, the function in SCAR was written by Benland ;), so I can't really help that, I'll write a new one myself for 3.21. And for the record, yes I do realize what 24!, considering I'm in a math and science course, pretty basic stuff… :fiery:

ZephyrsFury

2009-06-06 18:27:39

Rename it to Factorial(), Fract() makes no sense. :(

senrath

2009-06-06 19:18:17

From: ZephyrsFury
Rename it to Factorial(), Fract() makes no sense. :(

Agreed. Fract makes me think of fractions, not factorials. Heck, even naming it Fact would be better than what it currently is.

Tniffoc

2009-06-06 20:02:40

From: senrath
Agreed. Fract makes me think of fractions, not factorials. Heck, even naming it Fact would be better than what it currently is.


Agreed

Blumblebee

2009-06-06 22:45:30

From: Freddy1990
Well, the function in SCAR was written by Benland ;), so I can't really help that, I'll write a new one myself for 3.21. And for the record, yes I do realize what 24!, considering I'm in a math and science course, pretty basic stuff… :fiery:


thaqt wasnt at you! that was at Da owner who did the writeln on them and they got negative numbers, sorry for the confusing I meant no disrespect ;)

Da 0wner

2009-06-06 23:45:26

Umm Blumblebee, I did that to show that they were the same not the output….

Blumblebee

2009-06-06 23:47:13

From: Da 0wner
Umm Blumblebee, I did that to show that they were the same not the output….


because they overflowed and his is only an integer while mine is a int64 meaning mine holds a larger number thus letting the overflow become larger? Do like 5! and you will see they are the same.

ian.

2009-06-06 23:52:05

From: Blumblebee
I'm in Canada, so this would be Math 30, equivlent to the last math class you will take in Highschool (aside from pre calculous and AP courses)


:p I did this last year of middle school.. That seems a little odd to me. but oh well =X

Naum

2009-06-06 23:53:56

@Freddy in SCAR can't you make your own type of numbers? And If so, whats the max amount you can have it so like (I know this wont work :p):

Type GrtInt = [-999999999999999999..99999999999999999999999];

Da 0wner

2009-06-06 23:54:49

From: Blumblebee
because they overflowed and his is only an integer while mine is a int64 meaning mine holds a larger number thus letting the overflow become larger? Do like 5! and you will see they are the same.


When I did that with 24 they both showed the same output in the debug box dude. I am not saying that they are not the same, I'm saying they are the same…chill dude.

Blumblebee

2009-06-06 23:55:21

From: 99_
:p I did this last year of middle school.. That seems a little odd to me. but oh well =X


hmmm you did like n!/(n-r)!r! and stuff? wow thats rediculous I didnt even know it existed until this year lol :p

From: Da 0wner
When I did that with 24 they both showed the same output in the debug box dude. I am not saying that they are not the same, I'm saying they are the same…chill dude.


no sorry didnt mean to sound disrespectful, they came out different for me haha, sorry :o

ian.

2009-06-07 00:54:53

From: Blumblebee
hmmm you did like n!/(n-r)!r! and stuff? wow thats rediculous I didnt even know it existed until this year lol :p


Well, it was an outside class, but nonetheless I did it in 8th grade (last year). There were a few 7th graders too.

Blumblebee

2009-06-07 00:58:15

wow I'm impressed, I totally just failed my final on this shit :p shoulda got you to help me haha

ian.

2009-06-07 01:05:48

:p if you get another chance, I'd love to help! ^^ I love math. <3 haha

Freddy1990

2009-07-12 14:58:38

From: NaumanAkhlaQ
@Freddy in SCAR can't you make your own type of numbers? And If so, whats the max amount you can have it so like (I know this wont work :p):

Type GrtInt = [-999999999999999999..99999999999999999999999];


No I can't, if I could I would add unsigned Int64, but even Delphi won't let me define that.

Naum

2009-07-12 17:39:43

From: Freddy1990
No I can't, if I could I would add unsigned Int64, but even Delphi won't let me define that.


Okay, just another question, couldn't you make Abs for Int64s?

Atlanta

2009-07-12 18:45:15

It's so much easier doing the Factorial of a number using Recursion, it's a shame SCAR doesn't support Recursion though.