1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Having some trouble with a little bit of Python Homework:

  1. Feb 8, 2014 #1
    1. The problem statement, all variables and given/known data
    The problem shows as follows:
    An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ... , the distance is 2 while in the sequence 6, 12, 18, 24, ... , the distance is 6.

    Given the positive integer distance and the positive integer n , associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance . For example, if distance is 2 and n is 10 , then sum would be associated with 25 because 1+3+5+7+9 = 25 .


    2. Relevant equations

    This is definitely a "for" statement, and distance is added to a given integer i within the range of (1,n)

    3. The attempt at a solution
    So far i have the following written, but it seems to be telling me to use a "+" sign somewhere in my script:
    sum = 0
    i = 1
    for i in range (1, n):
    i += distance
    sum += i
     
  2. jcsd
  3. Feb 8, 2014 #2

    ShayanJ

    User Avatar
    Gold Member

    Try this one instead:
    Code (Text):


    sum=0
    for i in range (1,n,distance):
        sum+=i
     
     
  4. Nov 29, 2014 #3
    Almost, just tweek it slightly.

    sum=0
    for i in range(1,n+1,distance):
    sum += i


    viola!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Having some trouble with a little bit of Python Homework:
  1. Having trouble with c (Replies: 2)

Loading...