Having some trouble with a little bit of Python Homework:

  • Context: Python 
  • Thread starter Thread starter Spencero94
  • Start date Start date
  • Tags Tags
    Bit Homework Python
Click For Summary
SUMMARY

The discussion revolves around solving a Python homework problem involving arithmetic progressions. The task requires calculating the sum of elements in a sequence starting from 1, with a specified positive integer distance and up to a positive integer n. The correct implementation utilizes a for loop with the range function, specifically for i in range(1, n+1, distance): to iterate through the sequence and accumulate the sum. The final solution correctly computes the sum by adding each term in the arithmetic progression.

PREREQUISITES
  • Understanding of Python programming syntax
  • Familiarity with arithmetic progressions
  • Knowledge of the range() function in Python
  • Basic concepts of loops and variable assignment in Python
NEXT STEPS
  • Explore Python's range() function in detail
  • Learn about arithmetic progressions and their mathematical properties
  • Practice writing Python loops for various sequences
  • Investigate Python's built-in functions for summation, such as sum()
USEFUL FOR

Students learning Python programming, educators teaching arithmetic sequences, and anyone looking to enhance their coding skills in Python through practical problem-solving.

Spencero94
Messages
4
Reaction score
0

Homework Statement


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 .


Homework Equations



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

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
 
Technology news on Phys.org
Try this one instead:
Code:
sum=0
for i in range (1,n,distance):
    sum+=i
 
Almost, just tweek it slightly.

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

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
3K