Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • \$\begingroup\$ Nice! You can save one more character by changing "s=s*..." to "s*=..." \$\endgroup\$ Commented Oct 25, 2011 at 19:41
  • \$\begingroup\$ @DerekKurth: I had thought that when I was first doing optimizations, but that would mess up the logic because it needs to be s*(i-j)/j, not s*((i-j)/j). \$\endgroup\$ Commented Oct 25, 2011 at 19:49
  • \$\begingroup\$ Hmm, I tried it as s*=... in the jsfiddle and it seemed to work. Maybe I did something wrong, though. \$\endgroup\$ Commented Oct 25, 2011 at 20:16
  • 1
    \$\begingroup\$ @DerekKurth: Technically it is the same, but the idea is that if you multiply by (i-j) before dividing by j, then there is no need for floating point arithmetic because the results should always be an integer. If you do ((i-j)/j) first, this will result in decimal values which can be a source of error, and at the very least will require extra code for rounding/truncating. You don't begin to see this until you get to about n>11, and you'll see decimal values in the output, i.e., 1 11 55 165 330 461.99999999999994 461.99999999999994... \$\endgroup\$ Commented Oct 25, 2011 at 21:47
  • \$\begingroup\$ Ah, that makes sense! \$\endgroup\$ Commented Oct 25, 2011 at 22:10