generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: How would I calculate all the numbers added up from a certain point to another.

Previous Thread :: Next Thread 
mtester is not online. 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
thelastbunny is not online. thelastbunny
Joined: 07 Aug 2009
Total Posts: 2879
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 is not online. 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 is not online. 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 is not online. 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
thelastbunny is not online. thelastbunny
Joined: 07 Aug 2009
Total Posts: 2879
20 Mar 2011 09:53 AM
I did it for the lulz.
Report Abuse
billiland is not online. 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
BlueTaslem is not online. BlueTaslem
Joined: 11 May 2008
Total Posts: 11060
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 is not online. 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
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
20 Mar 2011 01:05 PM
He's asking for help,

Flame him!

You guys are hypocrites...
Report Abuse
billiland is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. HotThoth
Forum Moderator
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 is not online. HotThoth
Forum Moderator
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 is not online. HotThoth
Forum Moderator
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 is not online. 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
VirtualUser is not online. VirtualUser
Joined: 16 Aug 2010
Total Posts: 533
30 May 2013 10:34 AM
Hey, look! It is my old account. Hi me!
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image