So I am trying to use a while loop to show everything from 1 to 55 using (n * (n + 1)) / 2 equation.
while n <=55:
while loop for (n * (n + 1)) / 2 to get up to 55
Page 1 of 17 Replies  499 Views  Last Post: 16 October 2012  12:27 PM
Replies To: while loop for (n * (n + 1)) / 2 to get up to 55
#2
Re: while loop for (n * (n + 1)) / 2 to get up to 55
Posted 16 October 2012  11:15 AM
Ok I am not sure what had happened but it didn't let me finish so.
Not sure. I am a beginner in my second week.
n = 1 while n <=55: print n n +=1 (n * (n + 1)) / 2
Not sure. I am a beginner in my second week.
This post has been edited by Simown: 16 October 2012  11:21 AM
Reason for edit:: code tags!
#3
Re: while loop for (n * (n + 1)) / 2 to get up to 55
Posted 16 October 2012  11:16 AM
First of all, you are not assigning n to your equation on the last line. Also, you are incrementing n and I don't think you need to, getting to 55 will be done by the equation. Try:
Does that help?
n = 1 while n <=55: print n n = (n * (n + 1)) / 2
Does that help?
This post has been edited by Simown: 16 October 2012  11:21 AM
#4
Re: while loop for (n * (n + 1)) / 2 to get up to 55
Posted 16 October 2012  11:35 AM
With the way you did it it does not add 1 each time. Timeout my program with a ton of 1 1 1 1 1 1 1 1 1 1 1 etc.
Actually the darn thing worked after I posted. Not sure why it wasn't before.
Ok so If I were to incorporate it into a def. This one is kicking my butt.
Actually the darn thing worked after I posted. Not sure why it wasn't before.
Ok so If I were to incorporate it into a def. This one is kicking my butt.
from test import testEqual n = 1 def sumTo(n <=55): print n n +=1 (n * (n + 1)) / 2
#5
Re: while loop for (n * (n + 1)) / 2 to get up to 55
Posted 16 October 2012  11:40 AM
I didn't know it worked. I was just revising your code. I assumed it did.
What are you looking for in a function? I guess you want to set the upper limit
What are you looking for in a function? I guess you want to set the upper limit
def sumTo(x): n = 1 while(n <= x): print n n = (n * (n+1)) / 2
#6
Re: while loop for (n * (n + 1)) / 2 to get up to 55
Posted 16 October 2012  11:45 AM
This is what the online book is telling me.
Write a fruitful function sumTo(n) that returns the sum of all integer numbers up to and including n.
So sumTo(10) would be 1+2+3...+10 which would return the value 55. Use the equation (n * (n + 1)) / 2.
Write a fruitful function sumTo(n) that returns the sum of all integer numbers up to and including n.
So sumTo(10) would be 1+2+3...+10 which would return the value 55. Use the equation (n * (n + 1)) / 2.
#7
Re: while loop for (n * (n + 1)) / 2 to get up to 55
Posted 16 October 2012  12:14 PM
That's not what you have then, that's basically just applying a single function to the parameter:
def sumTo(n): print (n * (n+1)) / 2 ... >> sumTo(10) 55
#8
Re: while loop for (n * (n + 1)) / 2 to get up to 55
Posted 16 October 2012  12:27 PM
Oh my, So I was trying to make it harder than it actually was.
Thank you.
Thank you.
Page 1 of 1
