Python Having some trouble with a little bit of Python Homework:

AI Thread Summary
The discussion revolves around solving a problem related to arithmetic progressions, specifically calculating the sum of elements in a sequence defined by a positive integer distance and a positive integer n. The arithmetic progression starts at 1 and increments by the specified distance. For example, with a distance of 2 and n equal to 10, the sum of the sequence (1, 3, 5, 7, 9) equals 25. Participants share code snippets to compute this sum, emphasizing the correct use of a loop and the range function in Python. The initial attempts highlight common mistakes, such as incorrect indexing and the need for proper summation syntax. The final suggested solution correctly utilizes a for loop with the range function to iterate through the sequence, ensuring the sum accumulates the correct values. The final code snippet provided is sum=0 followed by a for loop that correctly sums the elements from 1 to n, incrementing by the specified distance.
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!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
4
Views
1K
Replies
15
Views
2K
Replies
10
Views
2K
Replies
2
Views
2K
Back
Top