huldu Posted February 23, 2006 Posted February 23, 2006 (edited) Its been a while since i were in school, and i werent that good at math even in school to start with . I ran into a problem calculating the following... For example: 1x1 + 2x1 + 3x1 + 4x1 + 5x1 = 15 The thing is i just dont remember how to do this math. Im pretty sure it involved something like this (x * y) and something else hehe. $x = 10 $y = 2 $sum = <some formula here> Hope someone knows how it worked! Edited February 23, 2006 by huldu "I'm paper, rock is fine, nerf scissors!!!"
BigDod Posted February 23, 2006 Posted February 23, 2006 $x = 10 $y = 2 $sum = $x*$y MsgBox(0,"Total",$sum) This seems too easy so I have probably misunderstood what you are asking Time you enjoyed wasting is not wasted time ......T.S. Elliot Suspense is worse than disappointment................Robert Burns God help the man who won't help himself, because no-one else will...........My Grandmother
huldu Posted February 23, 2006 Author Posted February 23, 2006 It doesnt give the result however. This is how the math looks before the "forumla": 1x1 + 1x2 + 1x3 + 1x4 + 1x5 = 15 The formula made the above math much more simple as you only needed to insert x and y to get the sum. "I'm paper, rock is fine, nerf scissors!!!"
BigDod Posted February 23, 2006 Posted February 23, 2006 It doesnt give the result however. This is how the math looks before the "forumla": 1x1 + 1x2 + 1x3 + 1x4 + 1x5 = 15 The formula made the above math much more simple as you only needed to insert x and y to get the sum.Try $sum = (1*1)+(1*2)+(1*3)+(1*4)+(1*5) MsgBox(0,"Total",$sum) Time you enjoyed wasting is not wasted time ......T.S. Elliot Suspense is worse than disappointment................Robert Burns God help the man who won't help himself, because no-one else will...........My Grandmother
huldu Posted February 23, 2006 Author Posted February 23, 2006 (edited) Ye hehe i understand that Lets say the user adds a number x and wants it added with y (like below). How would the formula look? $sum = (1*1)+(1*2)+(1*3)+(1*4)+(1*5) Since x is always the same, thats the easy part. However y is changing as you add up. $x = 1 $y = 5 $sum = ($x * $y)^2 / 2 .. ugh (just cant get it to work) When this "caluclation" is complete the total should be 15, thats all i know Edited February 23, 2006 by huldu "I'm paper, rock is fine, nerf scissors!!!"
sandyd Posted February 23, 2006 Posted February 23, 2006 This any good to you? $x = 1 $y = 5 $Sum = 0 For $i = 1 To $y $Sum = $Sum + ($x * $i) Next MsgBox(0,'Sum',$Sum) ----[ SandyD ]---
huldu Posted February 23, 2006 Author Posted February 23, 2006 Ye this seems right, thanks "I'm paper, rock is fine, nerf scissors!!!"
Valuater Posted February 23, 2006 Posted February 23, 2006 $x = 1 $y = 5 $sum = ($x * $y)^2 / 2 MsgBox(0,"", $sum) this is correct 1 x 5 = 5 5 Squared = 25 25 divided by 2 = 12.5 ( not 15 as you were looking for) and the message box reflects the correct answer of 12.5 8)
seandisanti Posted February 23, 2006 Posted February 23, 2006 (edited) $x = 1 $y = 5 $sum = ($x * $y)^2 / 2 MsgBox(0,"", $sum) this is correct 1 x 5 = 5 5 Squared = 25 25 divided by 2 = 12.5 ( not 15 as you were looking for) and the message box reflects the correct answer of 12.5 8)it's triangular numbers guys... Nth triangular number = N + (N-1) + (N-2) + (N-3) ... when i was playing with recursion i made a little script for figuring triangular numbers recursively... HotKeySet("{CAPSLOCK}","endit") Func endit() Exit EndFunc while 1 $blah = Number(InputBox("Enter a number","Enter a number N to find the Nth triangular number",6)) MsgBox(0,"triangular numbers",triangle($blah)) WEnd func triangle($blah) if $blah = 1 Then Return(1) Else Return($blah + triangle($blah-1)) EndIf EndFunc Edited February 23, 2006 by cameronsdad
SlimJim Posted February 23, 2006 Posted February 23, 2006 Although you may have to use Mathematical Induction to prove this is the answer it it is not necessary to use recursion to calculate 1 + 2 + ... + N. The answer is N * (N + 1 ) / 2. The solution to the problem x*1 + x * 2 + ... + x * y y a positive integer, is x * y * (y + 1) / 2.
Xenobiologist Posted February 24, 2006 Posted February 24, 2006 (edited) Although you may have to use Mathematical Induction to prove this is the answer it it is not necessary to use recursion to calculate 1 + 2 + ... + N. The answer is N * (N + 1 ) / 2. The solution to the problem x*1 + x * 2 + ... + x * y y a positive integer, is x * y * (y + 1) / 2. HI, yeah that is although what I can remember from school $x = 1 $y = 5 MsgBox(0, "", $x * $y * ($y + 1) / 2) So long, Mega Edited February 24, 2006 by th.meger Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times
seandisanti Posted February 24, 2006 Posted February 24, 2006 Although you may have to use Mathematical Induction to prove this is the answer it it is not necessary to use recursion to calculate 1 + 2 + ... + N. The answer is N * (N + 1 ) / 2. The solution to the problemx*1 + x * 2 + ... + x * yy a positive integer, is x * y * (y + 1) / 2.i didn't mean to imply that i thought recursion was the only way to go; i was on a little bit of a recursion kick for a little while last month or the month before, and that was just one implementation of it.
Valuater Posted February 24, 2006 Posted February 24, 2006 "Simple math question" not so simple!!! lol 8)
SlimJim Posted February 24, 2006 Posted February 24, 2006 "Simple math question"not so simple!!!You're right, but it takes me back (retired math lecturer). This problem is one of the first proofs for which we used Mathematical Induction (which is the backbone of recursion) but I also remember doing this at secondary school before I came across Mathematical Induction and the method is neat, simple when you see it, but not something you would necessarily think of immediately.Let S = 1 + 2 + .... + Nturn the order of the terms round, thenS = N + (N-1) + ... + 1add these two sums up term-by-term2S = (N + 1) + (N -1 + 2) + .... + (1 + N) = N * (N + 1)all the brackets are equal to N + 1 and there are N of them, so the right hand side adds up to N * (N + 1). Divide both sides by 2S = N * (N + 1) / 2
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now