mtester
|
  |
| Joined: 11 Jun 2009 |
| Total Posts: 146 |
|
|
| 20 Mar 2011 09:47 AM |
I want to see if there is a math formula that would allow me to do such a thing and I do not want to use a recursive function for this wastes time and speed depending on the number... Ex... if the number is 9, 9+8+7+6+5+4+3+2+1. The reason I am doing this is so I can find a similarity between a square and a simple addition factor. |
|
|
| Report Abuse |
|
|
|
| 20 Mar 2011 09:49 AM |
solid=0 number=9 for i = 1,number do solid = solid + (number-i) end print(solid) |
|
|
| Report Abuse |
|
|
billiland
|
  |
| Joined: 11 Nov 2008 |
| Total Posts: 4018 |
|
|
| 20 Mar 2011 09:49 AM |
| Hmm... Give me a second... Ill come up with something... |
|
|
| Report Abuse |
|
|
billiland
|
  |
| Joined: 11 Nov 2008 |
| Total Posts: 4018 |
|
|
| 20 Mar 2011 09:50 AM |
| Thelastbunny he said he did NOT want to use a recursive constraint... |
|
|
| Report Abuse |
|
|
billiland
|
  |
| Joined: 11 Nov 2008 |
| Total Posts: 4018 |
|
|
| 20 Mar 2011 09:51 AM |
y = (x(x+1))/2
Hmm that was easy enough.. |
|
|
| Report Abuse |
|
|
| |
|
billiland
|
  |
| Joined: 11 Nov 2008 |
| Total Posts: 4018 |
|
|
| 20 Mar 2011 09:54 AM |
Example
y = 10(9) = 90
72/2 = 45
9+8+7+6+5+4+3+2+1 = 45
Hmm... That was simple to solve @_@ yay for simple equations...
|
|
|
| Report Abuse |
|
|
|
| 20 Mar 2011 10:16 AM |
What billiland said is fundamental to series math...
Sum (k = 1) to N == N(N+1) / 2
Which can be explained by drawing a triangle and stuffs.
So E(E+1) / 2 - S(S+1) / 2 would be the sum of all numbers from S to E. |
|
|
| Report Abuse |
|
|
billiland
|
  |
| Joined: 11 Nov 2008 |
| Total Posts: 4018 |
|
|
| 20 Mar 2011 10:24 AM |
Indeed.... You can also use this formula to calculate differences of comparisons to points on a circle to possible places you can draw lines connecting one another with a simple change of sign.
y = (x(x-1))/2 |
|
|
| Report Abuse |
|
|
|
| 20 Mar 2011 01:05 PM |
He's asking for help,
Flame him!
You guys are hypocrites... |
|
|
| Report Abuse |
|
|
billiland
|
  |
| Joined: 11 Nov 2008 |
| Total Posts: 4018 |
|
|
| 20 Mar 2011 03:39 PM |
| Hes asking for help in a differential way... |
|
|
| Report Abuse |
|
|
Aaaboy97
|
  |
| Joined: 05 Apr 2009 |
| Total Posts: 6612 |
|
|
| 20 Mar 2011 08:44 PM |
Is there any way to make a non-recurring factorial calculation?
All I can think of uses a recursive function
function fact(n) return n == 0 and 1 or n * fact(n - 1) end
|
|
|
| Report Abuse |
|
|
Varp
|
  |
| Joined: 18 Nov 2009 |
| Total Posts: 5333 |
|
|
| 20 Mar 2011 10:04 PM |
| Aaa, there is a continuous factorial function (The gamma function), but it's harder and slower to calculate. |
|
|
| Report Abuse |
|
|
NXTBoy
|
  |
| Joined: 25 Aug 2008 |
| Total Posts: 4533 |
|
|
| 21 Mar 2011 11:46 AM |
Easy:
function fact(n) local result = n n = n - 1 while n > 1 do result = result * n end return result end |
|
|
| Report Abuse |
|
|
sncplay42
|
  |
| Joined: 27 Nov 2008 |
| Total Posts: 11891 |
|
|
| 21 Mar 2011 11:51 AM |
| I think "n = n - 1" should be inside the loop. |
|
|
| Report Abuse |
|
|
NXTBoy
|
  |
| Joined: 25 Aug 2008 |
| Total Posts: 4533 |
|
|
| 21 Mar 2011 11:53 AM |
Oops. You're right. That should've been the first statement in the loop:
function fact(n) local result = n while n > 2 do n = n - 1 result = result * n end return result end |
|
|
| Report Abuse |
|
|
HotThoth
|
  |
 |
| Joined: 24 Aug 2010 |
| Total Posts: 1176 |
|
|
| 21 Mar 2011 12:52 PM |
For those of you interested in nifty math tricks:
The trick to summing most series is how you group the numbers. Take 3+4+5+6+7+8+9+10, for example. If we group the outermost numbers, then the next-to-outermost, and so on, we get (3+10) + (4+9) + (5+8) + (6+7). Why do we do this? Each of these adds up to the same thing (13)!. So we have a total of 13*4 = 52. This "trick" is how you get the formulas that other people were talking about, namely: (First number + Last number) * HowManyNumbers/2. This way of thinking works as long as all the groupings add to the same number-- even when you have series that count by 2's or 3's or something else.
As for factorials, the recursion that Aaaboy97 provided seems like the best way to calculate them (since the recursion happens in the "return" statement, it is called tail-recursion, and lua optimizes for this), but the iteration of NXTBoy works as well. |
|
|
| Report Abuse |
|
|
HotThoth
|
  |
 |
| Joined: 24 Aug 2010 |
| Total Posts: 1176 |
|
|
| 21 Mar 2011 12:54 PM |
| As a fun and math-y follow-up: you can do something similar for series where each number is a multiple of the previous ones (so like 1 + 3 + 9 + 27 + 81). See if you can figure out the formula for series like this :). |
|
|
| Report Abuse |
|
|
HotThoth
|
  |
 |
| Joined: 24 Aug 2010 |
| Total Posts: 1176 |
|
|
| 21 Mar 2011 12:58 PM |
| Also, just in case people are really bored (and because I think this is awesome): the sum of the first cubes is always equal to the sum of those numbers squared. So like (1^3 + 2^3 + 3^3) = (1+2+3)^2. And that works for any number of them! |
|
|
| Report Abuse |
|
|
mtester
|
  |
| Joined: 11 Jun 2009 |
| Total Posts: 146 |
|
|
| 30 May 2013 10:31 AM |
y = (x(x+1))/2
thank you billiland. this is exactly what i wanted. |
|
|
| Report Abuse |
|
|
|
| 30 May 2013 10:34 AM |
| Hey, look! It is my old account. Hi me! |
|
|
| Report Abuse |
|
|