DarkWater Posted September 12, 2008 Share Posted September 12, 2008 Just curious to know if anyone has ever coded a summation notation function in PHP. =P Has anyone? If not, I'll write one and post it up here, I was just curious. Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 17, 2008 Share Posted September 17, 2008 Not that I can find... I'd much appreciate if you did one for us. Thanks Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 17, 2008 Share Posted September 17, 2008 Summation notation? You mean function like function sigma($nStart,$nEnd,$nExpression) { for ($i = $nStart;$i<=$nEnd;$i++) { //work on nExpression } } ? Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 17, 2008 Share Posted September 17, 2008 I'll have to go back to that project and test it later... but thanks in advance because I have a feeling it will work. Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 17, 2008 Share Posted September 17, 2008 Well yeah... What I wrote is just oversimplified example. I have a feeling there might much more to it than just simple for() loop. For example, what would be the format of $nExpression ? Would it be like $nExpression = "(1+(1/%N%))"; and then replace "%N%" with $i, parse and iterate? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 17, 2008 Author Share Posted September 17, 2008 See, that's the problem. It would use eval() like 4327908432190 times, lol. That was an exaggeration. Like, there are certain rules with summation notation to "simplify" things, like if you had: You could really simplify it to: And stuff like that makes it hard to get this to work. xD Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 17, 2008 Share Posted September 17, 2008 Within my project, I need it to find the summation of 1-$given which is normally like 1-9 or 1-55... never any thing more complex. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 17, 2008 Author Share Posted September 17, 2008 Wait, so you end up getting a negative number? =P Anyway, you can easily hardcode that one. I'm talking about truly dynamic summations. Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 17, 2008 Share Posted September 17, 2008 excuse me, I should have put 1 through $given. This should be something like $given = 100 1+2+3..+98+99+$given (which is = 5050) Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 17, 2008 Author Share Posted September 17, 2008 That's simple. <?php $total = array_sum(range(1, $given)); ?> Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 17, 2008 Share Posted September 17, 2008 THANK YOU!!! I didn't know about the range function. Cool I'm a newb still :'( -Solved for me- Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 17, 2008 Author Share Posted September 17, 2008 Aha, no problem. Now, if someone could figure this one out...that would be great. I don't think PHP would be the best option for this though, to be honest. Just wanted to see if it could be done in any coherent manner. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted September 18, 2008 Share Posted September 18, 2008 It really depends on the scope of the problems you're trying to solve. Obviously the advantage of a computer is that it is often acceptable to repeat the calculation many times, negating the need to find a way of simplifying the expression. On the other hand, it shouldn't be too hard to come up with something to simplify an expression given a relatively small size of potential expressions to solve. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 18, 2008 Author Share Posted September 18, 2008 Lol, I don't actually NEED it for anything, I just thought it would be kinda cool. Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 18, 2008 Share Posted September 18, 2008 I am just developing a text based game for the learning experience during my free time. But I gave up on that piece of math and just created a table with all the number input>>outputs. made things so much more simple. This was only an option because they can't use a number higher than 100. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 18, 2008 Share Posted September 18, 2008 There's an easier way. This formula works: $start = 1; $end = 100; echo ($start + $end) * ($end / 2); Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 18, 2008 Share Posted September 18, 2008 This will work for specific series. We're looking for general solution here Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.