Results 1 to 25 of 25

Thread: Permutation and Combinations

  1. #1
    Join Date
    Feb 2007
    Location
    Alberta,Canada
    Posts
    2,358
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Permutation and Combinations

    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

    Example:
    SCAR Code:
    Factorial(4);

    returns

    4! = 24
    hope this somewhat helps anyone doing perms and coms, it helped me

    SCAR Code:
    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)
    “Ignorance, the root and the stem of every evil.”

  2. #2
    Join Date
    Feb 2007
    Location
    Access Violation at 0x00000000
    Posts
    2,865
    Mentioned
    3 Post(s)
    Quoted
    18 Post(s)

    Default

    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
    Ce ne sont que des gueux


  3. #3
    Join Date
    Feb 2007
    Location
    Alberta,Canada
    Posts
    2,358
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    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
    “Ignorance, the root and the stem of every evil.”

  4. #4
    Join Date
    Mar 2007
    Posts
    4,810
    Mentioned
    3 Post(s)
    Quoted
    3 Post(s)

    Default

    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!

  5. #5
    Join Date
    Dec 2008
    Posts
    2,813
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

  6. #6
    Join Date
    Feb 2006
    Location
    Belgium
    Posts
    3,137
    Mentioned
    3 Post(s)
    Quoted
    5 Post(s)

    Default

    Factorial is already in SCAR as Fract

  7. #7
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    SCAR Code:
    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.

  8. #8
    Join Date
    Feb 2007
    Location
    Alberta,Canada
    Posts
    2,358
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by NaumanAkhlaQ View Post
    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

    Quote Originally Posted by 99_ View Post
    What math class are you in? Algebra I? (Not being offensive! 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)

    Quote Originally Posted by Freddy1990 View Post
    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?

    Quote Originally Posted by Da 0wner View Post
    SCAR Code:
    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.
    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
    “Ignorance, the root and the stem of every evil.”

  9. #9
    Join Date
    Feb 2006
    Location
    Belgium
    Posts
    3,137
    Mentioned
    3 Post(s)
    Quoted
    5 Post(s)

    Default

    Quote Originally Posted by Blumblebee View Post
    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...
    Last edited by Freddy1990; 06-06-2009 at 09:12 PM.

  10. #10
    Join Date
    Apr 2007
    Location
    Perth, Australia
    Posts
    3,926
    Mentioned
    3 Post(s)
    Quoted
    2 Post(s)

    Default

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

  11. #11
    Join Date
    Oct 2006
    Location
    ithurtsithurtsithurtsithurts
    Posts
    2,930
    Mentioned
    7 Post(s)
    Quoted
    135 Post(s)

    Default

    Quote Originally Posted by ZephyrsFury View Post
    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.

  12. #12
    Join Date
    Mar 2008
    Location
    ::1
    Posts
    915
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Quote Originally Posted by senrath View Post
    Agreed. Fract makes me think of fractions, not factorials. Heck, even naming it Fact would be better than what it currently is.
    Agreed

    Records and Types Save Code (and make you look better)
    Quote Originally Posted by Wizzup? View Post
    Is it possible to make Runescape a 2D game with this?... That would greatly simplify... Just about anything.

  13. #13
    Join Date
    Feb 2007
    Location
    Alberta,Canada
    Posts
    2,358
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Freddy1990 View Post
    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...
    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
    “Ignorance, the root and the stem of every evil.”

  14. #14
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

  15. #15
    Join Date
    Feb 2007
    Location
    Alberta,Canada
    Posts
    2,358
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Da 0wner View Post
    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.
    “Ignorance, the root and the stem of every evil.”

  16. #16
    Join Date
    Dec 2008
    Posts
    2,813
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Blumblebee View Post
    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)
    I did this last year of middle school.. That seems a little odd to me. but oh well =X

  17. #17
    Join Date
    Mar 2007
    Posts
    4,810
    Mentioned
    3 Post(s)
    Quoted
    3 Post(s)

    Default

    @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 ):

    SCAR Code:
    Type GrtInt = [-999999999999999999..99999999999999999999999];

  18. #18
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Blumblebee View Post
    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.

  19. #19
    Join Date
    Feb 2007
    Location
    Alberta,Canada
    Posts
    2,358
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by 99_ View Post
    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

    Quote Originally Posted by Da 0wner View Post
    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
    “Ignorance, the root and the stem of every evil.”

  20. #20
    Join Date
    Dec 2008
    Posts
    2,813
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Blumblebee View Post
    hmmm you did like n!/(n-r)!r! and stuff? wow thats rediculous I didnt even know it existed until this year lol
    Well, it was an outside class, but nonetheless I did it in 8th grade (last year). There were a few 7th graders too.

  21. #21
    Join Date
    Feb 2007
    Location
    Alberta,Canada
    Posts
    2,358
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    wow I'm impressed, I totally just failed my final on this shit shoulda got you to help me haha
    “Ignorance, the root and the stem of every evil.”

  22. #22
    Join Date
    Dec 2008
    Posts
    2,813
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

  23. #23
    Join Date
    Feb 2006
    Location
    Belgium
    Posts
    3,137
    Mentioned
    3 Post(s)
    Quoted
    5 Post(s)

    Default

    Quote Originally Posted by NaumanAkhlaQ View Post
    @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 ):

    SCAR Code:
    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.

  24. #24
    Join Date
    Mar 2007
    Posts
    4,810
    Mentioned
    3 Post(s)
    Quoted
    3 Post(s)

    Default

    Quote Originally Posted by Freddy1990 View Post
    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?

  25. #25
    Join Date
    Jul 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •