why it is always showing output as 1?

cloudybunny
def power(x,n):
    if n == 0:
        return 1
    elif n % 2 == 0:
        return power(x * x, n % 2)
    else:
        return x * power(x, n - 1)
print power(2,3)

What ever the input I give it is showing either 1 or 2. can anybody please help me where I went wrong. I am newbie to programming. Thank you

camz

Try changing this line:

return power(x * x, n % 2)

to this:

return power(x * x, n / 2)

if n is divisible by 2, you are calculating pow(x * x, 0) which will always return 1.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is isDrawerVisible always showing as false

From Dev

Why is isDrawerVisible always showing as false

From Dev

Shortcode output always showing at top of custom template

From Dev

Why is Sunday always -1?

From Dev

Why it is not showing string on output screen

From Dev

Why is there no output showing in this python code?

From Dev

Why isnt the output showing k1, k2, k3?

From Dev

Why is this xts frequency always 1?

From Dev

Why number of heap is always 1?

From Dev

Why regex always returns 1?

From Dev

Why mblen() always returns 1?

From Dev

Why mblen() always returns 1?

From Dev

Why $1 output is nothing?

From Dev

Why $1 output is nothing?

From Dev

Java Encoding: why the output is always the same?

From Dev

Why does `pushd` always output the stack to stdout?

From Dev

Why does the c variable always output 0?

From Dev

Why output showing 0 instead of 0.0?

From Dev

Why toGray function is not showing output on device

From Dev

Why is IPython.display.Image not showing in output?

From Dev

Itcpdump output - why is it showing an Ethernet status?

From Dev

Why is echo showing the command itself and not the command output

From Dev

Why this function is not showing the correct result as output?

From Dev

Why is my alert always showing the wrong information? (addition)

From Dev

Why Is My Jquery Slide div always showing at the start

From Dev

Why Is My Jquery Slide div always showing at the start

From Dev

Why is bash 'not always' showing the 'Terminated' message after killing a process?

From Dev

why is facebook login fragment always showing invalid key hash in android?

From Dev

Why does echo $-1 output this?

Related Related

HotTag

Archive