-4
How could I compute the sum of the numbers in a range of 1 to 500 in which the summed numbers of that range should be odd and multiple of three?
I only managed to take the break so far:
for c in range(3, 500, 3):
if c % 2 != 0:
-4
How could I compute the sum of the numbers in a range of 1 to 500 in which the summed numbers of that range should be odd and multiple of three?
I only managed to take the break so far:
for c in range(3, 500, 3):
if c % 2 != 0:
8
This question seems to be quite a common exercise in programming initiation and I will answer as such. I’ve been a monitor of the discipline of algorithms at university and I’ve always noticed that students have a history of solving the problem without even analyzing it.
Solution 1
The first solution I see, and certainly the simplest, is to analyze the problem mathematically, since it is... a math problem. The sum of all numbers, within the range 1 to 500, odd and multiples of 3 are:
S = 3 + 9 + 15 + 21 + ... + 495
It is easy to see that a number is always the former summed in 6 (as sung in comments by Victor). This clearly forms the sum of a arithmetic progression, of which it may be formulated:
S = (N/2)*(a_1 + a_N)
= (N/2)*(3 + 495)
Being N
the number of elements summed. To calculate, it would be enough to analyze the expression of the general term of the progression:
a_n = a_1 + (n-1)*r
Knowing that 495 is the last element, a_N
, 3 is the first, a_1
, and the reason is 6, r
, we have:
N = (a_N - a_1)/6 + 1
= (495 - 3)/6 + 1
= 83
So we know that there are 83 elements being added up. Going back to the sum formula:
S = (N/2)*(a_1 + a_N)
= (83/2)*(3 + 495)
= 20667
So the first solution would be to:
a_1 = 3
a_N = 495
r = 6
N = (a_N - a_1)/r + 1
S = N/2 * (a_1 + a_N)
print(S)
See working on Ideone | Repl.it
The calculation time remains constant, regardless of the magnitude of N
.
Solution 2
Make a loop of repetition between all values, check which are odd and multiples of 3 and, when summing, sum into a variable:
S = 0
for x in range(1, 500):
if x % 2 != 0 and x % 3 == 0:
S = S + x
print(S)
See working on Ideone | Repl.it
The calculation time increases linearly according to the value of N
.
Solution 3
We can simplify the above solution using Python list comprehension, along with the native function sum
:
S = sum(x for x in range(1, 500) if x % 2 != 0 and x % 3 == 0)
print(S)
See working on Ideone | Repl.it
The calculation time increases linearly according to the value of N
.
Solution 4
We can further simplify the above solution using the 6 in 6 interval as applied in the mathematical solution and, knowing that the first value to be considered will be the 3, we have:
S = sum(range(3, 500, 6))
print(S)
See working on Ideone | Repl.it
The calculation time increases linearly according to the value of N
.
Why Mathematical Analysis Would Be Important If The With Repetition Loop Is Easier?
Performance. With a small run time test with the module timeit
, made the time necessary to run 1000 times each solution, printing the time spent:
Tempo gasto para calcular 1000x cada solução:
Solução 1: 0.0003348870013724081
Solução 2: 0.06511368200153811
Solução 3: 0.06661822800015216
Solução 4: 0.0016278160001093056
If we normalize relative to solution 1, which is the fastest, we have:
Solução 1: 1.0
Solução 2: 194.43478467272314
Solução 3: 198.927482186954
Solução 4: 4.8607918295971935
That is, solutions 2 and 3 take about 200x the runtime of solution 1, as solution 4 takes about 5x, because solution 1 has complexity O(1), that is, it will always do the same number of operations, regardless of the interval, and the other three solutions are O(n)the number of operations increased linearly with the range considered.
2
Wrong answer, fixed below in the edit
Using the tips in the comment, you can do something like this:
for c in range(3, 500, 3):
soma = c + c
print(soma)
See on Ideone
Edit
Well noted by @Anderson Carlos Woss, the above answer is wrong and fixed as follows
cur = [] #declara a lista para receber os ímpares multiplos de 3
for c in range(0, 500,3): # o loop para percorrer a lista gerada pelo range
if (c % 2 != 0) and (c % 3 == 0): #checa se o valor corrente é primo e multiplo de 3
cur.append(c) #adiciona a lista
print(sum(cur)) #printa a soma dos itens da lista
See on Ideone
I think there’s something wrong with the answer. Are you sure it’s c+c
, for so soma
would be twice the last multiple of 3 in the range, 498. Even if it were soma += c
, would still be considering even numbers. Victor’s hint to go from 6 to 6 seems to solve the problem.
@Andersoncarloswoss I fixed, see if it pleases.
Browser other questions tagged python python-3.x for
You are not signed in. Login or sign up in order to post.
the problem there "now" is not adding up is to find out which are primes, try to develop an algorithm that finds out if it is an odd number, ai se vai montando seu código...
– MagicHat
I think I got it, and now?
– Lucas Souza
How about
print(20667)
?– Victor Stafusa
@Magichat This problem has nothing to do with prime numbers.
– Victor Stafusa
You can add the numbers 6 by 6. Look: 3, 9, 15, 21, 27...
– Victor Stafusa
@Victorstafusa I was gonna say that
– Jefferson Quesado
I’m sorry, but I still don’t understand how I can write the sum in code form.
– Lucas Souza
I think you’re good with negatives, after all the AP edited and made a valid question. Alias 1 negative I believe is enough to indicate that something is wrong, this post does not justify mass negatives.
– MagicHat
soma = soma + c
– Victor Stafusa