Given a very large number in the form of a string, the task is to check if the number is divisible by 75 or not.
Examples:
Input: N = 175
Output: No
Input: N = 100000000000000000754586672150
Output: Yes
Approach 1: A number is divisible by 75 only if it is divisible by 3(if the sum of the digit is divisible by 3) and divisible by 25 (if the last two digits is divisible by 25) both.
Below is the implementation to check whether the given number is divisible by 75 or not.
C++
#include <bits/stdc++.h>
using namespace std;
bool divisibleBy3(string number)
{
int sumOfDigit = 0;
for ( int i = 0; i < number.length(); i++)
sumOfDigit += number[i] - '0' ;
if (sumOfDigit % 3 == 0)
return true ;
return false ;
}
bool divisibleBy25(string number)
{
if (number.length() < 2)
return false ;
int length = number.length();
int lastTwo = (number[length - 2] - '0' ) * 10
+ (number[length - 1] - '0' );
if (lastTwo % 25 == 0)
return true ;
return false ;
}
bool divisibleBy75(string number)
{
if (divisibleBy3(number) && divisibleBy25(number))
return true ;
return false ;
}
int main()
{
string number = "754586672150" ;
bool divisible = divisibleBy75(number);
if (divisible)
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean divisibleBy3(String number)
{
int sumOfDigit = 0 ;
for ( int i = 0 ; i < number.length(); i++)
sumOfDigit += number.charAt(i) - '0' ;
if (sumOfDigit % 3 == 0 )
return true ;
return false ;
}
static boolean divisibleBy25(String number)
{
if (number.length() < 2 )
return false ;
int length = number.length();
int lastTwo = (number.charAt(length - 2 ) - '0' ) * 10
+ (number.charAt(length - 1 ) - '0' );
if (lastTwo % 25 == 0 )
return true ;
return false ;
}
static boolean divisibleBy75(String number)
{
if (divisibleBy3(number) && divisibleBy25(number))
return true ;
return false ;
}
public static void main (String[] args) {
String number = "754586672150" ;
boolean divisible = divisibleBy75(number);
if (divisible)
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def divisibleBy3(number):
sumOfDigit = 0
for i in range ( 0 , len (number), 1 ):
sumOfDigit + = ord (number[i]) - ord ( '0' )
if (sumOfDigit % 3 = = 0 ):
return True
return False
def divisibleBy25(number):
if ( len (number) < 2 ):
return False
length = len (number)
lastTwo = (( ord (number[length - 2 ]) -
ord ( '0' )) * 10 +
( ord (number[length - 1 ]) - ord ( '0' )))
if (lastTwo % 25 = = 0 ):
return True
return False
def divisibleBy75(number):
if (divisibleBy3(number) and
divisibleBy25(number)):
return True
return False
if __name__ = = '__main__' :
number = "754586672150"
divisible = divisibleBy75(number)
if (divisible):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool divisibleBy3( string number)
{
int sumOfDigit = 0;
for ( int i = 0; i < number.Length; i++)
sumOfDigit += number[i] - '0' ;
if (sumOfDigit % 3 == 0)
return true ;
return false ;
}
static bool divisibleBy25( string number)
{
if (number.Length < 2)
return false ;
int length = number.Length;
int lastTwo = (number[length - 2] - '0' ) * 10 +
(number[length - 1] - '0' );
if (lastTwo % 25 == 0)
return true ;
return false ;
}
static bool divisibleBy75( string number)
{
if (divisibleBy3(number) && divisibleBy25(number))
return true ;
return false ;
}
public static void Main ()
{
string number = "754586672150" ;
bool divisible = divisibleBy75(number);
if (divisible)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function divisibleBy3( $number )
{
$sumOfDigit = 0;
for ( $i = 0;
$i < strlen ( $number ); $i ++)
$sumOfDigit += $number [ $i ] - '0' ;
if ( $sumOfDigit % 3 == 0)
return true;
return false;
}
function divisibleBy25( $number )
{
if ( strlen ( $number ) < 2)
return false;
$length = strlen ( $number );
$lastTwo = ( $number [ $length - 2] - '0' ) * 10 +
( $number [ $length - 1] - '0' );
if ( $lastTwo % 25 == 0)
return true;
return false;
}
function divisibleBy75( $number )
{
if (divisibleBy3( $number ) &&
divisibleBy25( $number ))
return true;
return false;
}
$number = "754586672150" ;
$divisible = divisibleBy75( $number );
if ( $divisible )
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function divisibleBy3(number)
{
let sumOfDigit = 0;
for (let i = 0; i < number.length; i++)
sumOfDigit += number[i] - '0' ;
if (sumOfDigit % 3 == 0)
return true ;
return false ;
}
function divisibleBy25(number)
{
if (number.length < 2)
return false ;
let length = number.length;
let lastTwo = (number[length - 2] - '0' ) * 10
+ (number[length - 1] - '0' );
if (lastTwo % 25 == 0)
return true ;
return false ;
}
function divisibleBy75(number)
{
if (divisibleBy3(number) && divisibleBy25(number))
return true ;
return false ;
}
let number = "754586672150" ;
let divisible = divisibleBy75(number);
if (divisible)
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(length(number))
Auxiliary Space: O(1)
Approach 2: Using Sum of Digits Method
Here is steps to this approach:
- In this implementation, we first check if the number has at least two digits, because any number less than 75 can’t be divisible by 75.
- Then, we calculate the last two digits of the number and check if they are divisible by 25. If not, the number can’t be divisible by 75.
- Next, we calculate the sum of all the digits of the number and check if it is divisible by 3. If not, the number can’t be divisible by 75. Finally, if both conditions are satisfied, the number is divisible by 75.
Here is the code below:
C++
#include <bits/stdc++.h>
using namespace std;
bool isDivisibleBy75(string number) {
int n = number.length();
if (n < 2) {
return false ;
}
int lastTwoDigits = (number[n-2]- '0' )*10 + (number[n-1]- '0' );
if (lastTwoDigits % 25 != 0) {
return false ;
}
int sumOfDigits = 0;
for ( int i = 0; i < n; i++) {
sumOfDigits += number[i]- '0' ;
}
if (sumOfDigits % 3 != 0) {
return false ;
}
return true ;
}
int main() {
string number = "754586672150" ;
if (isDivisibleBy75(number)) {
cout << "Yes\n" ;
} else {
cout << "No\n" ;
}
return 0;
}
|
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String number = "754586672150" ;
if (isDivisibleBy75(number)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
private static boolean isDivisibleBy75(String number) {
int n = number.length();
if (n < 2 ) {
return false ;
}
int lastTwoDigits = (number.charAt(n - 2 ) - '0' ) * 10 + (number.charAt(n - 1 ) - '0' );
if (lastTwoDigits % 25 != 0 ) {
return false ;
}
int sumOfDigits = 0 ;
for ( int i = 0 ; i < n; i++) {
sumOfDigits += number.charAt(i) - '0' ;
}
if (sumOfDigits % 3 != 0 ) {
return false ;
}
return true ;
}
}
|
Python3
def isDivisibleBy75(number):
n = len (number)
if n < 2 :
return False
lastTwoDigits = int (number[n - 2 :])
if lastTwoDigits % 25 ! = 0 :
return False
sumOfDigits = 0
for digit in number:
sumOfDigits + = int (digit)
if sumOfDigits % 3 ! = 0 :
return False
return True
number = "754586672150"
if isDivisibleBy75(number):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class MainClass
{
private static bool IsDivisibleBy75( string number)
{
int n = number.Length;
if (n < 2)
{
return false ;
}
int lastTwoDigits = (number[n - 2] - '0' ) * 10 + (number[n - 1] - '0' );
if (lastTwoDigits % 25 != 0)
{
return false ;
}
int sumOfDigits = 0;
for ( int i = 0; i < n; i++)
{
sumOfDigits += number[i] - '0' ;
}
if (sumOfDigits % 3 != 0)
{
return false ;
}
return true ;
}
public static void Main( string [] args)
{
string number = "754586672150" ;
if (IsDivisibleBy75(number))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
function isDivisibleBy75(number) {
let n = number.length;
if (n < 2) {
return false ;
}
let lastTwoDigits = (number[n-2]-'0 ')*10 + (number[n-1]-' 0 ');
// if last two digits are not divisible by 25, the number can' t be divisible by 75
if (lastTwoDigits % 25 != 0) {
return false ;
}
let sumOfDigits = 0;
for (let i = 0; i < n; i++) {
sumOfDigits += number[i]- '0' ;
}
if (sumOfDigits % 3 != 0) {
return false ;
}
return true ;
}
let number = "754586672150" ;
if (isDivisibleBy75(number)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity: O(N) where n is the length of the input string.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!