Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Is a number whole? and Is a number a multiple of x?

  1. #11
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Thought I'd just add a factorial in :
    Code:
    <script type="text/javascript">
    Number.prototype.Fact = function(){
    var i = 1;
    for(x=1;x<this;x++){
    i += x * i;
    }
    return i;
    };
    var num = 5;
    alert(num.Fact());
    </script>
    In a few seconds, mburt is gonna post saying "I've got a shorter version Nile.", and then twey is gonna be like "Mine is only 2 lines though."
    Jeremy | jfein.net

  2. #12
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Code:
    Number.prototype.Fact = function() {
      for (i = 1, x = 1;i < this;i++) {
        x += i * x;
      }
      return x;
    };
    Haha... Only one line (I think) in difference.
    - Mike

  3. #13
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Twey's turn. Lol, that's basically the same thing as mine.
    Jeremy | jfein.net

  4. #14
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Quick 'n' dirty:
    Code:
    Number.prototype.fact = function() {
      for (var i = 1, r = 1; i <= this; r *= i++);
      return r;
    };
    One-liner:
    Code:
    Number.prototype.fact = function() { for (var i = 1, r = 1; i <= this; r *= i++); return r; };
    Safe:
    Code:
    Number.prototype.fact = function() {
      if (this < 0) return NaN;
      for (var i = 1, r = 1; i <= this; r *= i++);
      return r;
    };
    Using my toolkit:
    Code:
    var fact = (function(F, O) {
      return F.compose(F.curry(F.reduce)(O.multiply), F.curry(F.range)(1));
    })(Functional. Functional.Operator);
    Without shortcuts:
    Code:
    var fact = Functional.compose(Functional.curry(Functional.reduce)(Functional.Operator.multiply), Functional.curry(Functional.range)(1));
    In Python:
    Code:
    import operator
    def fact(x):
        return reduce(operator.mul, range(1, x + 1))
    In Haskell:
    Code:
    fact = product . (enumFromTo 1)
    Any more questions?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #15
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Wow... I like that first function. I never think of using loops like that. But I guess you just look at it like this:

    Code:
    for (variables;condition(s);assigment)
    Is your toolkit on the web anywhere Twey?
    - Mike

  6. #16
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Not yet. You can do most of it with Functional Javascript.

    You can do anything you like within a for loop. If I'd wanted to, I could have written it like this:
    Code:
    Number.prototype.fact = function() {
      for (var i = 0, r = 1; r *= ++i, i < this; );
      return r;
    };
    A for loop is divided into three sections. They're commonly labelled 'initialisation', 'condition', and 'incrementation' because that's what they generally do, but those labels are stylistic: you can put anything you like in there. Pragmatically speaking, they could be given the better (but admittedly less stylish) titles of 'code that runs once when the loop is first encountered', 'code that runs before each iteration of the loop', and 'code that runs after each iteration of the loop'. The first two have some special properties: the 'initialisation' section allows 'var' statements, and the 'condition section' can terminate the loop if its return value is falsy. The 'incrementation' section is free-form.

    This said, the 'initialisation', 'condition', 'incrementation' style is usually the least messy. There are some handy variants, though, like:
    Code:
    for (var i = 0, e; e = a[i++]; ) // leaves 'e' bound to the current value; terminates if it hits a falsy element.
    Last edited by Twey; 08-29-2008 at 07:14 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. The Following User Says Thank You to Twey For This Useful Post:

    mburt (08-29-2008)

  8. #17
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Thanks for the lesson on for-loops haha . I think I understand it a bit better now.
    - Mike

  9. #18
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    @Twey, I almost came up with your first solution before you posted, infact I had the same thing except I had one thing wrong, don't remember what it was.
    Jeremy | jfein.net

Bookmarks

Posting Permissions

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