0
A program to add up the first "n" elements of a progression geometric, given the first element "A0" and the ratio "r" between the elements. For example, for n = 7, A0 = 1, r = 3: 1 + 3 + 9 + 27 + 81 + 243 + 729 = 1093
0
A program to add up the first "n" elements of a progression geometric, given the first element "A0" and the ratio "r" between the elements. For example, for n = 7, A0 = 1, r = 3: 1 + 3 + 9 + 27 + 81 + 243 + 729 = 1093
2
Mathematics [O(1)]
The general term of a geometric progression is given by:
Thus, the sum of n first terms would stand:
That if you apply the general term, you get the equation (1):
If we multiply both sides by reason, q, we will have the equation (2):
If we subtract the equations (2) and (1) from each other, we have:
Which can be simplified to:
Therefore, the function that returns the sum of n terms of an arithmetic progression described by the initial term to1 and by reason q is:
def soma_progressao_aritmetica(n, a1, q):
return a1 * (q**n - 1) / (q - 1)
See working on Repl.it | Ideone | Github GIST
To summon her would be enough:
print(soma_progressao_aritmetica(n=7, a1=1, q=3)) # 1093.0
Loop of repetition [O(n)]
Another way would be using the native function sum
, along with a Generator inline, which basically calculates all the n terms of progression and the sum.
def soma_progressao_aritmetica(n, a1, q):
return sum(a1*q**i for i in range(n))
Obtaining the same result, however it is a solution that has temporal performance O(n); that is, the execution time of the function will be directly proportional to the amount of terms summed, while the first solution does not.
0
If you intend to calculate the sum of the terms of a progressão geométrica
you have to take into consideration two things:
Progressão Geométrica
;Progressões Geométricas
.NOTE: From now on I will stop using the term Progressão Geométrica
and I will use only the term P.G.
.
The general formula used to calculate the sum of termos
of P.G. has already been defined by the previous answer.
What am I gonna enfatizar
here are the tipos
p.G.
In mathematics there is 4
types of P.G.
and, each of them can be defined according to its reason q
.
ALTERNADA
or OSCILANTE
(q < 0);DECRESCENTE
(0 < q < 1);CONSTANTE
(q == 1);CRESCENTE
(q > 1).The formula defined in the previous answer abrange
the solution of calculating the sum of the terms of the P.G. except in the case of the P.G. constante
.
In case P.G. is constant, (q == 1)
, the correct formula is:
Sn = (n * a1)
Where n
is the number of terms and a1
the first term of the p.g..
From these considerations I developed the following algorithm...
from math import pow
while True:
try:
a1 = float(input('Digite o primeiro termo: '))
if a1 == 0:
print('\033[31mValor INVÁLIDO! Digite números reais diferentes de "0"!\033[m')
else:
break
except:
print('\033[31mValor INVÁLIDO! Digite apenas números reais!\033[m')
while True:
try:
q = float(input('Digite a razão da progressão: '))
if q == 0:
print('\033[31mValor INVÁLIDO! Digite números reais diferentes de "0"!\033[m')
else:
break
except:
print('\033[31mValor INVÁLIDO! Digite apenas números reais!\033[m')
while True:
try:
n = int(input('Digite a quantidade de termos: '))
if n <= 1:
print('\033[31mValor INVÁLIDO! Digite números inteiros maiores que "1"!\033[m')
else:
break
except:
print('\033[31mValor INVÁLIDO! Digite apenas números inteiros!\033[m')
if q == 1:
Sn = (n * a1)
print(f'\033[32mA soma dos termos da P.G CONSTANTE é: {Sn:.2f}\033[m')
else:
Sn = (a1 * (pow(q, n) - 1)) / (q - 1)
if q < 0:
print(f'\033[32mA soma dos termos da P.G. ALTERNADA ou OSCILANTE é: {Sn:.2f}\033[m')
if 0 < q < 1:
print(f'\033[32mA soma dos termos da P.G. DECRESCENTE é: {Sn:.2f}\033[m')
if q > 1:
print(f'\033[32mA soma dos termos da P.G. CRESCENTE é: {Sn:.2f}\033[m')
Note the functioning of the algorithm in repl it.
It is good to note that this algorithm besides calculating the soma dos termos
of the AG, also the classifica
.
0
I’m studying Python and programming a few weeks, but I tried to come up with a solution. I don’t know if it’s the best code, but it works.
n = int(input("Informe quantos elementos irá somar na PG: "))
r = float(input("Informe a razão entre os elementos: "))
primeiroelemento = float(input("Informe o primeiro elemento: "))
total = 0
pg = 1
print("Os termos da progressão são: ")
print(primeiroelemento)
for i in range(1, n):
pg = pg * r
print(pg)
total = total + pg
print("O total da PG é: ", total+primeiroelemento)
If I may comment, as you are starting, instead of telling where there is a flaw in your solution, I will propose to you to insert an initial term of the PG other than 1 and see what happens.
Thanks Anderson, there really is a flaw at this point. It went unnoticed in my tests. I will check another solution
The error was just that you started pg
equal to 1 and not equal to the first element of the PG.
Browser other questions tagged python
You are not signed in. Login or sign up in order to post.
All he posted was a text that resembles quite a statement of an exercise, but forgot to put what he tried to do and describe the difficulties encountered. Please edit your question and show what you have done so far.
– Woss