Multiples of 4 (An Interesting Method)
Given a number n, the task is to check whether this number is a multiple of 4 or not without using +, -, * ,/ and % operators.
Examples :
Input: n = 4 Output - Yes
n = 20 Output - Yes
n = 19 Output - No
Basic Approach:
Method 1 (Using XOR)
- If n is equal to 1 return false.
- Run a loop from 1 to n and find XOR of all numbers.
- If the result is equal to n, then n is a multiple of 4 else not.
Implementation of the above approach.
C++
#include<bits/stdc++.h>
using namespace std;
bool isMultipleOf4( int n)
{
if (n == 1)
return false ;
int XOR = 0;
for ( int i = 1; i <= n; i++)
XOR = XOR ^ i;
return (XOR == n);
}
int main()
{
for ( int n=0; n<=42; n++)
if (isMultipleOf4(n))
cout << n << " " ;
return 0;
}
|
Java
class Test
{
static boolean isMultipleOf4( int n)
{
if (n == 1 )
return false ;
int XOR = 0 ;
for ( int i = 1 ; i <= n; i++)
XOR = XOR ^ i;
return (XOR == n);
}
public static void main(String[] args)
{
for ( int n= 0 ; n<= 42 ; n++)
System.out.print(isMultipleOf4(n) ? n : " " );
}
}
|
Python 3
def isMultipleOf4(n):
if (n = = 1 ):
return False
XOR = 0
for i in range ( 1 , n + 1 ):
XOR = XOR ^ i
return (XOR = = n)
for n in range ( 0 , 43 ):
if (isMultipleOf4(n)):
print (n, end = " " )
|
C#
using System;
class GFG {
static bool isMultipleOf4( int n)
{
if (n == 1)
return false ;
int XOR = 0;
for ( int i = 1; i <= n; i++)
XOR = XOR ^ i;
return (XOR == n);
}
public static void Main()
{
for ( int n = 0; n <= 42; n++)
{
if (isMultipleOf4(n))
Console.Write(n+ " " );
}
}
}
|
PHP
<?php
function isMultipleOf4( $n )
{
if ( $n == 1)
return false;
$XOR = 0;
for ( $i = 1; $i <= $n ; $i ++)
$XOR = $XOR ^ $i ;
return ( $XOR == $n );
}
for ( $n = 0; $n <= 42; $n ++)
if (isMultipleOf4( $n ))
echo $n , " " ;
?>
|
Javascript
<script>
function isMultipleOf4(n)
{
if (n == 1)
return false ;
let XOR = 0;
for (let i = 1; i <= n; i++)
XOR = XOR ^ i;
return (XOR == n);
}
for (let n = 0; n <= 42; n++)
document.write(isMultipleOf4(n) ? n : " " );
</script>
|
Output
0 4 8 12 16 20 24 28 32 36 40
Time Complexity: O(n)
Auxiliary Space: O(1)
How does this work?
When we do XOR of numbers, we get 0 as XOR value just before a multiple of 4. This keeps repeating before every multiple of 4.
Number Binary-Repr XOR-from-1-to-n
1 1 [0001]
2 10 [0011]
3 11 [0000]
Efficient Approach:
Method 2 (Using Bitwise Shift Operators)
- Remove last two bits using >>.
- Multiply with 4 using <<.
- If the result is equal to n, then last two bits were 0, hence number is multiple of 4.
Implementation of the above approach.
C++
#include<bits/stdc++.h>
using namespace std;
bool isMultipleOf4( long long n)
{
if (n==0)
return true ;
return (((n>>2)<<2) == n);
}
int main()
{
for ( int n=0; n<=42; n++)
if (isMultipleOf4(n))
cout << n << " " ;
return 0;
}
|
Java
class Test
{
static boolean isMultipleOf4( long n)
{
if (n== 0 )
return true ;
return (((n>> 2 )<< 2 ) == n);
}
public static void main(String[] args)
{
for ( int n= 0 ; n<= 42 ; n++)
System.out.print(isMultipleOf4(n) ? n : " " );
}
}
|
Python3
def isMultipleOf4(n):
if (n = = 0 ):
return True
return (((n>> 2 )<< 2 ) = = n)
for n in range ( 43 ):
if isMultipleOf4(n):
print (n, end = " " )
|
C#
using System;
class GFG {
static bool isMultipleOf4( int n)
{
if (n == 0)
return true ;
return (((n >> 2) << 2) == n);
}
static void Main()
{
for ( int n = 0; n <= 42; n++)
if (isMultipleOf4(n))
Console.Write(n + " " );
}
}
|
PHP
<?php
function isMultipleOf4( $n )
{
if ( $n == 0)
return true;
return ((( $n >> 2) << 2) == $n );
}
for ( $n = 0; $n <= 42; $n ++)
if (isMultipleOf4( $n ))
echo $n , " " ;
?>
|
Javascript
<script>
function isMultipleOf4(n)
{
if (n == 0)
return true ;
return (((n >> 2) << 2) == n);
}
for (let n = 0; n <= 42; n++)
if (isMultipleOf4(n))
document.write(n + " " );
</script>
|
Output
0 4 8 12 16 20 24 28 32 36 40
Time Complexity: O(1)
Auxiliary Space: O(1)
As we can see that the main idea to find multiplicity of 4 is to check the least two significant bits of the given number. We know that for any even number, the least significant bit is always ZERO (i.e. 0). Similarly, for any number which is multiple of 4 will have least two significant bits as ZERO. And with the same logic, for any number to be multiple of 8, least three significant bits will be ZERO. That’s why we can use AND operator (&) as well with other operand as 0x3 to find multiplicity of 4.
Last Updated :
11 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...