Given a very large number. Check its divisibility by 15.
Examples:
Input: 31
Output: No
Input : num = "156457463274623847239840239
402394085458848462385346236
482374823647643742374523747
264723762374620"
Output: Yes
Given number is divisible by 15
Brute Force Approach:
- Convert the input number to a string and remove any whitespace or special characters from it.
- Initialize a variable sum to 0.
- Iterate over each character in the string and convert it to an integer.
- Add the integer to the sum.
- After iterating over all the characters, check if the sum is divisible by both 3 and 5. If it is, return “Yes”, otherwise return “No”.
Below is implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string check_divisibility_by_15(string num) {
num.erase(remove_if(num.begin(), num.end(), []( char c) { return ! isdigit (c); }), num.end());
int sum = 0;
for ( char c : num) {
sum += ( int )(c - '0' );
}
if (sum % 3 == 0 && sum % 5 == 0) {
return "Yes" ;
} else {
return "No" ;
}
}
int main() {
string num1 = "31" ;
string num2 = "156457463274623847239840239402394085458848462385346236482374823647643742374523747264723762374620" ;
cout << check_divisibility_by_15(num1) << endl;
cout << check_divisibility_by_15(num2) << endl;
return 0;
}
|
Time Complexity: O(n)
Auxiliary Space: O(1)
A number is divisible by 15 if it is divisible by 5 (if the last digit is 5 or 0), and it is divisible by 3 (if sum of digits is divisible by 3).
Here, used accumulate function to sum up all the numbers.
C++
#include <bits/stdc++.h>
using namespace std;
bool isDivisible(string s)
{
int n = s.length();
if (s[n - 1] != '5' and s[n - 1] != '0' )
return false ;
int sum = accumulate(begin(s), end(s), 0) - '0' * n;
return (sum % 3 == 0);
}
int main()
{
string s = "15645746327462384723984023940239" ;
isDivisible(s)? cout << "Yes\n" : cout << "No\n" ;
string s1 = "15645746327462384723984023940235" ;
isDivisible(s1)? cout << "Yes\n" : cout << "No\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static boolean isDivisible(String S)
{
int n = S.length();
if (S.charAt(n - 1 ) != '5' &&
S.charAt(n - 1 ) != '0' )
return false ;
int sum = 0 ;
for ( int i = 0 ; i < S.length(); i++)
sum += ( int )S.charAt(i);
if (sum % 3 == 0 )
return true ;
else
return false ;
}
public static void main (String[] args)
{
String S = "15645746327462384723984023940239" ;
if (isDivisible(S) == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
String S1 = "15645746327462384723984023940235" ;
if (isDivisible(S1) == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def accumulate(s):
acc = 0 ;
for i in range ( len (s)):
acc + = ord (s[i]) - 48 ;
return acc;
def isDivisible(s):
n = len (s);
if (s[n - 1 ] ! = '5' and s[n - 1 ] ! = '0' ):
return False ;
sum = accumulate(s);
return ( sum % 3 = = 0 );
s = "15645746327462384723984023940239" ;
if isDivisible(s):
print ( "Yes" );
else :
print ( "No" );
s = "15645746327462384723984023940235" ;
if isDivisible(s):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
public static bool isDivisible(String S)
{
int n = S.Length;
if (S[n - 1] != '5' &&
S[n - 1] != '0' )
return false ;
int sum = 0;
for ( int i = 0; i < S.Length; i++)
sum += ( int )S[i];
if (sum % 3 == 0)
return true ;
else
return false ;
}
public static void Main()
{
String S = "15645746327462384723984023940239" ;
if (isDivisible(S) == true )
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
String S1 = "15645746327462384723984023940235" ;
if (isDivisible(S1) == true )
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function accumulate( $s )
{
$acc = 0;
for ( $i = 0;
$i < strlen ( $s ); $i ++)
{
$acc += $s [ $i ] - '0' ;
}
return $acc ;
}
function isDivisible( $s )
{
$n = strlen ( $s );
if ( $s [ $n - 1] != '5' &&
$s [ $n - 1] != '0' )
return false;
$sum = accumulate( $s );
return ( $sum % 3 == 0);
}
$s = "15645746327462384723984023940239" ;
isDivisible( $s ) ?
print ( "Yes\n" ) : print ( "No\n" );
$s = "15645746327462384723984023940235" ;
isDivisible( $s ) ?
print ( "Yes\n" ) : print ( "No\n" );
?>
|
Javascript
<script>
function accumulate(s)
{
let acc = 0;
for (let i = 0;
i < s.length; i++)
{
acc += s[i] - '0' ;
}
return acc;
}
function isDivisible(s)
{
let n = s.length;
if (s[n - 1] != '5' &&
s[n - 1] != '0' )
return false ;
let sum = accumulate(s);
return (sum % 3 == 0);
}
let s = "15645746327462384723984023940239" ;
isDivisible(s) ?
document.write( "Yes<br>" ) : document.write( "No<br>" );
s = "15645746327462384723984023940235" ;
isDivisible(s) ?
document.write( "Yes<br>" ) : document.write( "No<br>" );
</script>
|
Time complexity: O(number of digits)
Auxiliary space: O(1)
Method 2: Checking given number is divisible by 15 or not by using the modulo division operator “%”.
C++
#include <iostream>
using namespace std;
int main()
{
long long int n=31;
if (n % 15 == 0)
{
cout << "Yes" ;
}
else
{
cout << "No" ;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
long n = 31 ;
if (n % 15 == 0 ) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
n = 31
if int (n) % 15 = = 0 :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG {
static public void Main()
{
long n = 31;
if (n % 15 == 0) {
Console.Write( "Yes" );
}
else {
Console.Write( "No" );
}
}
}
|
Javascript
<script>
var n = 31
if (n % 15 == 0)
document.write( "Yes" )
else
document.write( "No" )
</script>
|
PHP
<?php
$num =31;
if ( $num % 15 == 0) {
echo "Yes" ;
}
else {
echo "No" ;
}
?>
|
Time complexity: O(1) as it is doing constant operations
Auxiliary Space: O(1) as it is using constant space for variables
This article is contributed by Striver. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...