0
Can you guys help me with this ??
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
+ 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.
+ 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
+ 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.
+ 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.
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.
0
Thanks for the advice everyone.