Home > Software engineering >  Can we solve this Arithmetic Problem using Python in a more efficient way?
Can we solve this Arithmetic Problem using Python in a more efficient way?

Time:01-22

I've been thinking about making an Equation Solving program, starting off with some basic Equations first.

I have this Arithmetic progression problem here

Problem Statement: If the nth term of AP is 3n 2 find the sum up to 15 terms

This is what I have tried,

n = 1
while n <= 15:
    ap = 3 * n   2
    n  = 1

print(ap)

#OUTPUT: 47

Is there a more efficient way to calculate these types of problems using python?

I'm thinking of handling bigger equations And the answer is not quite Correct

CodePudding user response:

If you want the sum of 3n 2 for 1<=n<=15 then:

print(sum(3*n 2 for n in range(1,16)))

Output:

390

CodePudding user response:

you can use the closed formula for arithmetic sequence

def fn(n):
    return 3*(n*(n 1)//2)   2*n

CodePudding user response:

ap = 0 for n in range(1,16): ap = 3 * n 2 print(ap)

  •  Tags:  
  • Related