Eye Sun - 3 years ago 281

Python Question

To check whether a number is a multiple of 4 or not without using +, -, * ,/ and % operators.

`def multipleFour(n):`

if n == 1:

return False

XOR = 0

for i in range(n):

XOR = XOR ^ 1

return XOR == n

multipleFour(20)

My answer returns False

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You are mistaken in your **for loop** as well as **xor statement**. It should be like -

```
def multipleFour(n):
if n == 1:
return False
XOR = 0
for i in range(1, n+1):
XOR = XOR ^ i
return XOR == n
multipleFour(20)
```

i.e. your loop must run for i=1 to i=n and not i=0 to i=n-1. And it must be `xor = xor ^ i`

EDIT:

As pointed out in comments, **for i in range(1, n+1)** does not meet the condition of not using **+**, but by little modification, this thing can be solved as -

```
def multipleFour(n):
if n == 1:
return False
XOR = n
for i in range(1, n):
XOR = XOR ^ i
return XOR == n
multipleFour(20)
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**