How do I create a recursive python function for 5n? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I only got this far with the answer. I don't understand how recursive functions work. I would really appreciate any help. And also, if someone could explain what base and recursive calls are, that'd be great.
def multi(n):
if n==1:
return 4

Please try this code :
def multi(n):
return (5 + multi(n-1)) if n>0 else 0;
this function is a 5n, here's the explanation:
when n = 1 the call is like this :
output : 5 + multi(0) --> multi(0) = 0, so 5 + 0 = 5
It's something like this :
multi(3)
|
-- 5 + multi(2)
|
--- 5 + (5 + multi(1))
|
---- 5 + (5 + (5 + multi(0)))
|
----- 5 + 5 + 5 + 0
5n --> if n = 3 --> 5(3) = 15
output multi(3) = 15, as same as 5(3)=15.
5(3) = 5+5+5 = 15
5(2) = 5+5 = 10
in recursion, you'll do it like this:
5(0) = 0
5(1) = 5 + 0 = 5
5(2) = 5 + 5 + 0 = 10
...
5(n) = 5 + f(n-1), where f(0) = 0, so n must greater than 0. To prevent infinite loop.
Case for 5(3) :
1. 5(3) = 5 + f(2)
2. f(2) = 5 + f(1)
3. f(1) = 5 + f(0)
4. f(0) = 0
So, you will get 5(3) = 5 + (5 + (5 + 0)) = 15
Hope this help you to understand it :D

Recursive function is a function that makes a call to it self.
for example:
def multi(n):
if n>0:
return n * multi(n-1)
else:
return 0
To prevent infinit recursion you have to define a case where the function does not make a call to it self.(in our example it's in else statement when n==1).

Related

can you explain how this Loop works and how do we get that output? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
if __name__ == '__main__':
n = int(input())
for i in range (0,n):
result = i**2
print(result)
#input : 5
#output : 0 1 4 9 16
range 0,n = 0,1,2,3,4 and
we gave i**2 but how we got 0,1,4,9,16 as output?
range(start, stop, step)
** = square of Number
start Optional. An integer number specifying at which position to
start. Default is 0
stop Required. An integer number specifying at which position to
stop (not included).
step Optional. An integer number specifying the incrementation. Default is 1
you are passing required parameter as 5 which will not be included in the loop. so as per your calculation it will start from 0
result = 0**2 = 0
result = 1**2 = 1
result = 2**2 = 4
result = 3**2 = 9
result = 4**2 = 16
'i' will not reach 5 because of non-inclusive nature of range() operator.

Euclid's division algorithm (to find HCF ) is there a better way? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
#HCF
#input
C = int(input("the bigger number:" ))
D = int(input("the smaller number:" ))
#division
N = "="
M = "x"
A = "+"
#i don't know if I can add this to the while loop
Q = C//D
S = C%D
print (C,N,D,M,Q,A,S)
E = S
s = D
D = C
#Euclid's division algorithm
while S != 0:
Q = s//E
S = s%E
print(s, N,E, M, Q, A, S)
s = E
if S == 0:
print ("HCF =",E)
else :
E = S
is there a better way of writing this ?
if there is a syntax I am using incorrectly pls tell.
I don't know why I can't post this it's showing your post is mostly code pls explain ignore this last part it's only so this problem goes away.
Here's another way of writing your code
Corrects an error in posted code which provides incorrect results
Uses variables from an algorithm to provide a reference for variable names
Uses string interpolation to show formula
Euclidean Algorithm
Reference
Implementation
a, b, q, r corresponds to a, b, qi, ri in the algorithm
def euclidean_algorithm(a, b):
" Displays iterations of the euclidean algorithm "
if b > a:
# swap if necessary
# to ensure a is the larger number
a, b = b, a
while b:
q = a // b
r = a - q*b
print(f'{a} = {q} x {b} + {r}') # use string interpolation to show formula
a, b = b, r
print(f'HCF = {a}')
return a
Usage
a = int(input('First number: '))
b = int(input('Second number: '))
euclidean_algorithm(a, b)
Output
First number: 498
Second number: 222
498 = 2 x 222 + 54
222 = 4 x 54 + 6
54 = 9 x 6 + 0
HCF = 6

for loop through an array to display its contents [duplicate]

This question already has answers here:
Why does range(start, end) not include end? [duplicate]
(11 answers)
Closed 4 years ago.
The program calculates the multiplication table from 0 to 12 for the number the user has entered.
I am trying to display the contents of the array with the results in it by for looping through it. However, it only displays the tables from 0 to 11, but not the 12th one, even if the 12th data is there.
Here is what I came up with:
def multiplicationTable():
nb = int(input("Please enter a number between 1 and 12 : \n"))
table = array('i')
for i in range(0,12):
table.append(i * nb)
for i in range(len(table)):
print(str(nb) + "x" + str(i) + " = " + str(table[i]))
The output looks like this:
4x0 = 0
4x1 = 4
4x2 = 8
4x3 = 12
4x4 = 16
4x5 = 20
4x6 = 24
4x7 = 28
4x8 = 32
4x9 = 36
4x10 = 40
4x11 = 44
What could be causing that ? Coming from VB and C# so I may be mistaking the i as an index, while it is a value from the array, but I really don't see how I could be fixing this.
Thank you !
The range() function is non-inclusive of the upper bound. So when you're saying range(0,12), you're only going to get [0,11]. If you want [0,12], you have to do range(0,13). Note that range(0,13) is equivalent to range(13) because the default lower-bound is 0.
See the documentation.

is there possible to resolve this star pyramid [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Today, i have trying to resolve a small star pyramid :
Input:
5 1
Output:
*
**
***
****
Code:
x = 1
y = 0
m, e = map(int, raw_input().split())
while x < m:
print "\n" * y, "*" * e
m -= 1
e += 1
I did that but there is a better solution?? Thanks =)
I think this can be solved more easily:
stop, first = map(int, raw_input().split())
for i in range(stop - 1):
print '*' * (i + first)
just for fun >:)
class c:
def __init__(s,m,e):
s.e , s.m = sorted([e, m])
s.r = 42
def __iter__(s):
return s
def next(s):
if s.m < s.e:
t = "".join(chr(s.r) for _ in range(s.m))
s.m += 1
return t
else:
raise StopIteration
print "\n".join(c(*map(int,raw_input().split())))
n = int(raw_input())
for i in range(n): print "*"*i
This appears to do what your program intends to do, however I can't quite tell because of the issues I raised in my comment above.

Explain this syntax error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Can anyone tell me why this has a syntax error? I've run this exact code before and it worked perfectly. The line in strong text is where Python tells me the syntax error is. Thanks, everyone!
import random
count = 0
while count < 10:
attackerLV = 20
attackerST = 20
attackerSK = 20
baseAtkPwr = 20
attackPWR = ((random.randint(85,100) * (baseAtkPwr + attackerLV + attackerST + attackerSK)) // 100
**defenderLV = 20**
defenderCON = 20
defenderSKa = 20
baseDefPwr = 20
defensePWR = (((random.randint(85,100)) * (baseDefPwr + defenderLV + defenderCON + defenderSKa)) // 4) // 100
damage = attackPWR - defensePWR
if damage <= 1:
damage = 1
print(str(attackPWR))
print(str(defensePWR))
print(str(damage))
print()
count = count + 1
You missed a parenthesis here:
attackPWR = ((random.randint(85,100) * (baseAtkPwr + attackerLV + attackerST + attackerSK)) // 100

Categories

Resources