7 Replies - 425 Views - Last Post: 16 October 2012 - 12:27 PM Rate Topic: -----

#1 SuperSlow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-October 12

while loop for (n * (n + 1)) / 2 to get up to 55

Posted 16 October 2012 - 11:07 AM

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:
Is This A Good Question/Topic? 0
  • +

Replies To: while loop for (n * (n + 1)) / 2 to get up to 55

#2 SuperSlow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-October 12

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.

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!

Was This Post Helpful? 0
  • +
  • -

#3 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

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:
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

Was This Post Helpful? 0
  • +
  • -

#4 SuperSlow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-October 12

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.

from test import testEqual

n = 1

def sumTo(n <=55):
print n
n +=1
(n * (n + 1)) / 2



Was This Post Helpful? 0
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

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

def sumTo(x):
  n = 1
  while(n <= x):
    print n
    n = (n * (n+1)) / 2


Was This Post Helpful? 0
  • +
  • -

#6 SuperSlow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-October 12

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.
Was This Post Helpful? 0
  • +
  • -

#7 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

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


Was This Post Helpful? 0
  • +
  • -

#8 SuperSlow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-October 12

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1