That works, and is probably pretty fast. The real test is how long it takes to sum a million numbers, or a billion. No doubt it will be fast, but I can only speculate since I’m not a qualified bench tester.
The mathematical formula is,
s = n * (a + k) / 2
s is the sum of the sequence;
n is the number of terms in the sequence;
a is the first term in the sequence;
k is the last term in the sequence.
This formulaic approach is far and away the fastest way to add the sequence.
However, if you do not have the math background to support its use, the don’t use it in an interview or on a test. You’ll be asked how you came to learn this and someone telling it to you is not the answer they want to hear. That you learned it from study of Series and Sequences in academic math is what they want to hear.
It’s for this same reason that I tell people my first rule of coding… Never use code you do not fully comprehend and could not have come up with yourself and explain. One should always endeavor to work out solutions based upon their level of understanding. Write something that works, then study it, refine it, and write it again some other way.
This is not the correct solution. If you reread the question and look at my example you will see that no numbers can be divisible by 5. Check the solution set and see if there are any in there that are divisible.
Can you explain why someone might question this solution and ask where you got it? The usage of range is a dead give away that there is some lack of understanding. Okay for a beginner, but not okay otherwise.
A beginner would not have come up with even that solution because they are not adept at logic yet, and that approach (one I’ve explained many times over the years) is quite intuitive and shows an understanding of Prime Numbers (which are not explained in the problem in the PMP unit). Still, don’t let me criticize if you actually did come up with this on your own. Who am I to doubt? Still, there are flags going up, so you know.
If as you say these exercises are intended to learn what you know, then don’t set the bar too high for yourself or expectations of your teacher will match. Write simple code that a beginner would write, and don’t be afraid to skip a question if it is over your head. Be honest, especially with yourself and you will be very teachable. Any teacher will prefer that to having their expectations dashed by disappointment because someone was trying to paint themselves with a brighter brush. Be ignorant and let the teacher be your guide.
Proof of the above formula
s = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
s' = 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1
2s = 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10
2s = 90
s = 90 / 2
s = 45
n = 9
t1 = 1
t'1 = 9
s = n * (t1 + t'1) / 2
We’ll note that t1 and t’1 are the first and last terms of the sequence. Computationally it is faster to multiply integers than it is to multiply floats so the division is left to the last step. (4.5 is a float 4.5 * 10 could take up to 4 times longer to compute.)
But if you haven’t studied this math, then stop right there because what if someone asks, “What is the sum if the common difference is 2, or 3, or 5, …?”? Only if we understand the math can we answer this question. It is not a far reach to say that I learned a lot of math without even knowing it while I learned computing with algorithms. The one above, though, I learned in high school.
Be yourself, without trying to win recognition at this stage. Your eagerness, honesty, willingness to work and learn will be all you need. I think you’ll do just fine.
And I think a teacher would be just as impressed by the naive solution, off the bat…
total = 0
for x in range(1, 778):
total = total + x
Never discount the value these algorithms possess. While extremely slow compared to your solution (or mine), they keep us connected to the roots of programming, and how the thinking expanded from there. Don’t miss out on this chapter of the book of learning.