Reputation: 47
Here is the code
def power(x, y):
if y == 0:
return 1
if y % 2 == 0:
return power(x, y // 2) * power(x, y // 2)
return x * power(x, y // 2) * power(x, y // 2)
def order(x):
# Variable to store of the number
n = 0
while (x != 0):
n = n + 1
x = x // 10
return n
def isArmstrong(x):
n = order(x)
temp = x
sum1 = 0
while (temp != 0):
r = temp % 10
sum1 = sum1 + power(r, n)
temp = temp // 10
return (sum1 == x)
x = 153
print(isArmstrong(x))
in function power(x,y) the return statement
return power(x, y // 2) * power(x, y // 2)
what does (x,y//2) mean in this code , I am not able to dry run it.
Upvotes: 1
Views: 1768
Reputation: 21
//
is the floor division operator. The floor division is a division in which only the whole part of the result is given and the fractional part is truncated.
Normal division-
5/2 = 2.5
Floor division-
5//2 = 2
Upvotes: 1
Reputation: 15488
The statement will recursively call the power()
function again. Here it will first pass x
as first argument and y // 2
(//
does floor division, it removes the fraction part of fractional/float numbers) as the second argument. Like 5 / 2 = 2.5
BUT 5 // 2 = 2
.
Upvotes: 2
Reputation: 2977
//
means floor division, i.e. it rounds the result down to the nearest whole number
10 // 3 = 3
15 // 7 = 2
Upvotes: 2