12th May 2023, 5:21 PM
Kyaw Hok
Kyaw Hok - avatar
7 Answers
+ 5
I can give you a basic understanding so that you can understand and learn this logic: run a loop from 1 to N and go on adding it in a variable S then print it. Now let's talk about a hack to create O(1) logic, Well again a hint there's a mathematical formula for this
12th May 2023, 5:29 PM
Just Some Random Guy
Just Some Random Guy - avatar
+ 4
Smith Welder , the purpose of this baaic exercise is to learn something about looping in python and also practice it, but not to apply a formula.
13th May 2023, 10:53 AM
Lothar
Lothar - avatar
+ 3
As Snehil said, this requires a loop (in the easiest form). If you are not familiar with loops, why not try out the Python course, it's in there: https://www.sololearn.com/Course/CC-Mandatory-Python-for-Beginners/?ref=app
12th May 2023, 5:58 PM
Ausgrindtube
Ausgrindtube - avatar
+ 3
Smith Welder Pls avoid giving finished code as answer, because it makes the OP skip the most important part of learning. Prefer giving hints for the OP to find the solution instead.
13th May 2023, 12:41 AM
Emerson Prado
Emerson Prado - avatar
+ 1
This is quite simple. This is some clue for you to get started: 1. You can assign a variable before running a loop. What number should we put to get a accurate sum of consecutive numbers?Hmmm.. 2. Run a for loop to get every number, you can add them to the variable you created before until the loop reaches the variable N. There are other ways to count the sum from 1 to N, like using recursion method(Can't be used on large number input),sum of list(Not recommended, but if you want to get every number in a list then calculate the sum of it, it's okay.),defining a function that calculates the sum of 1 to N,... . You will be able to interact with them in up-coming lessons.
14th May 2023, 3:07 PM
Dragon RB
Dragon RB - avatar
0
Use can use the builtin function sum(iterable, start) It returns a number, the sum of all numbers in an iterable/list. The iterable can be a list comprehension where you loop through the range 1 to n.
13th May 2023, 3:13 PM
Corey
0
Thanks for the advice everyone.
14th May 2023, 3:28 AM
Kyaw Hok
Kyaw Hok - avatar