Check if a large number is divisible by 8 or not
Given a number, the task is to check if a number is divisible by 8 or not. The input number may be large and it may not be possible to store even if we use long long int.
Examples:
Input : n = 1128
Output : Yes
Input : n = 1124
Output : No
Input : n = 363588395960667043875487
Output : No
Since input number may be very large, we cannot use n % 8 to check if a number is divisible by 8 or not, especially in languages like C/C++. The idea is based on following fact.
A number is divisible by 8 if number formed by last three digits of it is divisible by 8.
Illustration:
For example, let us consider 76952
Number formed by last three digits = 952
Since 952 is divisible by 8, answer is YES.
How does this work?
Let us consider 76952, we can write it as
76942 = 7*10000 + 6*1000 + 9*100 + 5*10 + 2
The proof is based on below observation:
Remainder of 10i divided by 8 is 0 if i greater
than or equal to three. Note than 10000,
1000,... etc lead to remainder 0 when divided by 8.
So remainder of "7*10000 + 6*1000 + 9*100 +
5*10 + 2" divided by 8 is equivalent to remainder
of following :
0 + 0 + 9*100 + 5*10 + 2 = 52
Therefore we can say that the whole number is
divisible by 8 if 952 is divisible by 8.
Below is the implementation of the above fact :
C++
#include<bits/stdc++.h>
using namespace std;
bool check(string str)
{
int n = str.length();
if (n == 0)
return false ;
if (n == 1)
return ((str[0]- '0' )%8 == 0);
if (n == 2)
return (((str[n-2]- '0' )*10 + (str[n-1]- '0' ))%8 == 0);
int last = str[n-1] - '0' ;
int second_last = str[n-2] - '0' ;
int third_last = str[n-3] - '0' ;
return ((third_last*100 + second_last*10 + last) % 8 == 0);
}
int main()
{
string str = "76952" ;
check(str)? cout << "Yes" : cout << "No " ;
return 0;
}
|
Java
import java.io.*;
class IsDivisible
{
static boolean check(String str)
{
int n = str.length();
if (n == 0 )
return false ;
if (n == 1 )
return ((str.charAt( 0 )- '0' )% 8 == 0 );
if (n == 2 )
return (((str.charAt(n- 2 )- '0' )* 10 + (str.charAt(n- 1 )- '0' ))% 8 == 0 );
int last = str.charAt(n- 1 ) - '0' ;
int second_last = str.charAt(n- 2 ) - '0' ;
int third_last = str.charAt(n- 3 ) - '0' ;
return ((third_last* 100 + second_last* 10 + last) % 8 == 0 );
}
public static void main (String[] args)
{
String str = "76952" ;
if (check(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def check(st) :
n = len (st)
if (n = = 0 ) :
return False
if (n = = 1 ) :
return (( int )(st[ 0 ]) % 8 = = 0 )
if (n = = 2 ) :
return (( int )(st[n - 2 ]) * 10 +
(( int )( str [n - 1 ]) % 8 = = 0 ))
last = ( int )(st[n - 1 ])
second_last = ( int )(st[n - 2 ])
third_last = ( int )(st[n - 3 ])
return ((third_last * 100 + second_last * 10 +
last) % 8 = = 0 )
st = "76952"
if (check(st)) :
print ( "Yes" )
else :
print ( "No " )
|
C#
using System;
class IsDivisible
{
static bool check(String str)
{
int n = str.Length;
if (n == 0)
return false ;
if (n == 1)
return ((str[0] - '0' ) %8 == 0);
if (n == 2)
return (((str[n - 2] - '0' ) * 10 +
(str[n - 1] - '0' )) % 8 == 0);
int last = str[n - 1] - '0' ;
int second_last = str[n - 2] - '0' ;
int third_last = str[n - 3] - '0' ;
return ((third_last * 100 + second_last
* 10 + last) % 8 == 0);
}
public static void Main ()
{
String str = "76952" ;
if (check(str))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function check(str)
{
let n = str.length;
if (n == 0)
return false ;
if (n == 1)
return ((str[0] - '0' ) %8 == 0);
if (n == 2)
return (((str[n - 2] - '0' ) * 10 +
(str[n - 1] - '0' )) % 8 == 0);
let last = str[n - 1] - '0' ;
let second_last = str[n - 2] - '0' ;
let third_last = str[n - 3] - '0' ;
return ((third_last * 100 + second_last
* 10 + last) % 8 == 0);
}
let str = "76952" ;
if (check(str))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
PHP
<?php
function check( $str )
{
$n = strlen ( $str );
if ( $n == 0)
return false;
if ( $n == 1)
return (( $str [0] - '0' ) %
8 == 0);
if ( $n == 2)
return ((( $str [ $n - 2] - '0' ) *
10 + ( $str [ $n - 1] - '0' ))
% 8 == 0);
$last = $str [ $n - 1] - '0' ;
$second_last = $str [ $n - 2] - '0' ;
$third_last = $str [ $n - 3] - '0' ;
return (( $third_last * 100 +
$second_last * 10 +
$last ) % 8 == 0);
}
$str = "76952" ;
$x = check( $str )? "Yes" : "No " ;
echo ( $x );
?>
|
Time Complexity: O(1), as we are not using any loops for traversing.
Auxiliary Space: O(1), as we are not using any extra space.
Another Approach(Using Bitwise Operator):
The binary representation of the number 8 is 1000. If a number is divisible by 8, it means that its last three bits in binary representation are 000. Therefore, we can check if a number is divisible by 8 by checking if its last three bits are 000 or not.
To do this, we perform a bitwise AND operation between the input number and 7 (which is 111 in binary and has its last three bits set to 0). If the result of the bitwise AND operation is 0, then the input number is divisible by 8, otherwise it is not divisible by 8.
Note that we use long long int data type to handle large numbers.
C++
#include <iostream>
bool isDivisibleBy8( long long int n) {
return (n & 7) == 0;
}
int main() {
long long int n = 76952;
if (isDivisibleBy8(n)) {
std::cout << n << " is divisible by 8\n" ;
} else {
std::cout << n << " is not divisible by 8\n" ;
}
return 0;
}
|
Java
import java.util.*;
public class Main {
static boolean isDivisibleBy8( long n) {
return (n & 7 ) == 0 ;
}
public static void main(String[] args) {
long n = 76952 ;
if (isDivisibleBy8(n)) {
System.out.println(n + " is divisible by 8" );
} else {
System.out.println(n + " is not divisible by 8" );
}
}
}
|
Python3
def isDivisibleBy8(n: int ) - > bool :
return (n & 7 ) = = 0
n = 76952
if isDivisibleBy8(n):
print (n, "is divisible by 8" )
else :
print (n, "is not divisible by 8" )
|
C#
using System;
public class Program {
static bool isDivisibleBy8( long n) {
return (n & 7) == 0;
}
public static void Main( string [] args) {
long n = 76952;
if (isDivisibleBy8(n)) {
Console.WriteLine(n + " is divisible by 8" );
} else {
Console.WriteLine(n + " is not divisible by 8" );
}
}
}
|
Javascript
function isDivisibleBy8(n) {
return (n & 7) == 0;
}
let n = 76952;
if (isDivisibleBy8(n)) {
console.log(n + " is divisible by 8" );
} else {
console.log(n + " is not divisible by 8" );
}
|
Output
76952 is divisible by 8
Time Complexity: O(1)
Auxiliary Space: O(1)
Another Approach (Using String) :
- We can reduce the number of checks by breaking the string into substrings of length 3.
- If a substring is divisible by 8, then the entire number is divisible by 8.
- We can check the divisibility of a substring of length 3 using the following formula: A number is divisible by 8 if the number formed by its last three digits is divisible by 8.
- We can use modular arithmetic to compute the number formed by the last three digits of a substring.
C++
#include<bits/stdc++.h>
using namespace std;
bool check(string str)
{
int n = str.length();
if (n == 0)
return false ;
if (n == 1)
return ((str[0]- '0' )%8 == 0);
if (n == 2)
return (((str[n-2]- '0' )*10 + (str[n-1]- '0' ))%8 == 0);
for ( int i = 0; i < n - 2; i++) {
int num = (str[i]- '0' )*100 + (str[i+1]- '0' )*10 + (str[i+2]- '0' );
if (num % 8 == 0)
return true ;
}
return false ;
}
int main()
{
string str = "76952" ;
check(str)? cout << "Yes" : cout << "No" ;
return 0;
}
|
Java
import java.util.Scanner;
public class Main {
static boolean check(String str) {
int n = str.length();
if (n == 0 )
return false ;
if (n == 1 )
return ((str.charAt( 0 ) - '0' ) % 8 == 0 );
if (n == 2 )
return (((str.charAt(n - 2 ) - '0' ) * 10 + (str.charAt(n - 1 ) - '0' )) % 8 == 0 );
for ( int i = 0 ; i < n - 2 ; i++) {
int num = (str.charAt(i) - '0' ) * 100 + (str.charAt(i + 1 ) - '0' ) * 10 + (str.charAt(i + 2 ) - '0' );
if (num % 8 == 0 )
return true ;
}
return false ;
}
public static void main(String[] args) {
String str = "76952" ;
if (check(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def check( str ):
n = len ( str )
if n = = 0 :
return False
if n = = 1 :
return int ( str ) % 8 = = 0
if n = = 2 :
return int ( str ) % 8 = = 0
for i in range (n - 2 ):
num = int ( str [i:i + 3 ])
if num % 8 = = 0 :
return True
return False
str = "76952"
if check( str ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class MainClass
{
public static bool Check( string str)
{
int n = str.Length;
if (n == 0)
return false ;
if (n == 1)
return ((str[0] - '0' ) % 8 == 0);
if (n == 2)
return (((str[n - 2] - '0' ) * 10 + (str[n - 1] - '0' )) % 8 == 0);
for ( int i = 0; i < n - 2; i++)
{
int num = (str[i] - '0' ) * 100 + (str[i + 1] - '0' ) * 10 + (str[i + 2] - '0' );
if (num % 8 == 0)
return true ;
}
return false ;
}
public static void Main( string [] args)
{
string str = "76952" ;
Console.WriteLine(Check(str) ? "Yes" : "No" );
}
}
|
Javascript
function check(str) {
const n = str.length;
if (n === 0) {
return false ;
}
if (n === 1) {
return parseInt(str) % 8 === 0;
}
if (n === 2) {
return parseInt(str) % 8 === 0;
}
for (let i = 0; i < n - 2; i++) {
const num = parseInt(str.substr(i, 3));
if (num % 8 === 0) {
return true ;
}
}
return false ;
}
const str = "76952" ;
console.log(check(str) ? "Yes" : "No" );
|
In this code, we first check if the length of the string is 0, 1, or 2, and handle those cases separately. Then, we loop through the string and check substrings of length 3. If we find a substring that is divisible by 8, we return true. If we have checked all substrings and none of them is divisible by 8, we return false.
Time Complexity : O(n)
Space Complexity : O(1)
This article is contributed by DANISH_RAZA .
Last Updated :
06 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...