Please tell me how to solve this problem, because I have finally got confused. Thank you in advance!
Closed due to the fact that the essence of the question is not clear to the participants of Kromster , ermak0ff , user194374, jfs , fori1ton Jan 18 '17 at 14:44 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- fourTry to write more detailed questions. To get an answer, explain in what way you see the problem, what do you want to get in the answer, a ready-made solution on a platter? - Kromster
- 1. The recursive method 2. Cycles - Alexander Puzanov
- Yes, there is no need for recursion. Take a unit and multiply it in a cycle with respect to i from zero to n by (ai * n). - Akina
- The text should be placed in the form of text in the question, not pictures (to help other people with the same question find it). Edit the question and clearly indicate what is the difficulty with the "cycle operators": give your code, describe what you wanted to get, what is happening instead. - jfs
|
3 answers
Each time we multiply P by something less than the value from the previous iteration by n . We start with a , repeat n + 1 times. Total:
n = int(input("n = ")) a = float(input("a = ")) P, multiplier = 1, a for i in range(n + 1): P *= multiplier multiplier -= n print(P) |
from operator import mul try: from functools import reduce except: pass def p(a,n): return reduce(mul, [a - i * n for i in range(0, n + 1)]) a = int(input('Введите a: ')) n = int(input('Введите n: ')) print("Вычисленное значение P: {}".format(p(a,n))) Something like this. The try block is at the beginning due to the fact that it is unknown which version of Python.
- one[a - i * n for i in range (0, n + 1)] should be turned into an iterator: (a - i * n for i in range (0, n + 1)), because as n grows, the list will be linear consume RAM, and these are dozens of megabytes already at n> = 1000000 and if the import from functools fails (it means python 2.x) you should import reduce for python 2.x, pass is not an option here - whintu
- oneIn python2,
reducebuilt-in function (build-in). Why import it ?. Is it unsuccessful attempt to importfunctoolsit will be blocked? - Arnial - one@MaxU I apologize. Forgot to specify that the question was addressed whintu. He said that for python 2.x,
passdoes not work andreducealso needs to be imported. - Arnial - 1- do not use
except:(without specifying a name), especially in the code for beginners — this is a bad practice that can hide bugs and which is often overused by inexperience. In this case, try / except can be removed (import works on Python 2 as well as 3). Otherwise, you would useexcept ImportError: reduce = reduceinstead ofexcept: pass2- in conditionais a real number. Replacea = int(...witha = float(...- jfs
|
Generator solution:
def generator(n): r = 0 for i in range(n + 1): yield r r += n n = int(input('Введите n: ')) a = float(input('Введите a: ')) p = 1 for g in generator(n): p *= a - g print('Результат: %f.' % p) |
