检查是否有可被36整除的较大数字

给定一个数字,检查给定的数字是否可以被36整除。数字可能非常大,并且可能不适合任何数字(int、long int、float等)数据类型。 例如:

null
Input : 72Output : YesInput : 244Output : NoInput : 11322134Output : NoInput : 92567812197966231384Output : Yes

如果一个数字可以被4和9整除,那么它可以被36整除

  1. 如果一个数的最后两位数字可以被4整除,则该数可以被4整除
  2. 如果一个数的位数之和可以被9整除,那么这个数可以被9整除

下面是基于上述思想的实现。

C++

// C++ implementation to check divisibility by 36
#include <bits/stdc++.h>
using namespace std;
// Function to check whether a number
// is divisible by 36 or not
string divisibleBy36(string num)
{
int l = num.length();
// null number cannot
// be divisible by 36
if (l == 0)
return "No" ;
// single digit number other than
// 0 is not divisible by 36
if (l == 1 && num[0] != '0' )
return "No" ;
// number formed by the last 2 digits
int two_digit_num = (num[l-2] - '0' )*10 +
(num[l-1] - '0' ) ;
// if number is not divisible by 4
if (two_digit_num%4 != 0)
return "No" ;
// number is divisible by 4 calculate
// sum of digits
int sum = 0;
for ( int i=0; i<l; i++)
sum += (num[i] - '0' );
// sum of digits is not divisible by 9
if (sum%9 != 0)
return "No" ;
// number is divisible by 4 and 9
// hence, number is divisible by 36
return "Yes" ;
}
// Driver program
int main()
{
string num = "92567812197966231384" ;
cout << divisibleBy36(num);
return 0;
}


JAVA

// Java program to find if a number is
// divisible by 36 or not
class IsDivisible
{
// Function to check whether a number
// is divisible by 36 or not
static boolean divisibleBy36(String num)
{
int l = num.length();
// null number cannot
// be divisible by 36
if (l == 0 )
return false ;
// single digit number other than
// 0 is not divisible by 36
if (l == 1 && num.charAt( 0 ) != '0' )
return false ;
// number formed by the last 2 digits
int two_digit_num = (num.charAt(l- 2 ) - '0' )* 10 +
(num.charAt(l- 1 ) - '0' ) ;
// if number is not divisible by 4
if (two_digit_num% 4 != 0 )
return false ;
// number is divisible by 4 calculate
// sum of digits
int sum = 0 ;
for ( int i= 0 ; i<l; i++)
sum += (num.charAt(i) - '0' );
// sum of digits is not divisible by 9
if (sum% 9 != 0 )
return false ;
// number is divisible by 4 and 9
// hence, number is divisible by 36
return true ;
}
// main function
public static void main (String[] args)
{
String num = "92567812197966231384" ;
if (divisibleBy36(num))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}


Python3

# Python 3 implementation to
# check divisibility by 36
# Function to check whether a
# number is divisible by
# 36 or not
def divisibleBy36(num) :
l = len (num)
# null number cannot
# be divisible by 36
if (l = = 0 ) :
return ( "No" )
# single digit number other
# than 0 is not divisible
# by 36
if (l = = 1 and num[ 0 ] ! = '0' ) :
return ( "No" )
# number formed by the
# last 2 digits
two_digit_num = ((( int )(num[l - 2 ])) *
10 + ( int )(num[l - 1 ]))
# if number is not
# divisible by 4
if (two_digit_num % 4 ! = 0 ) :
return "No"
# number is divisible
# by 4 calculate sum
# of digits
sm = 0
for i in range ( 0 ,l) :
sm = sm + ( int )(num[i])
# sum of digits is not
# divisible by 9
if (sm % 9 ! = 0 ) :
return ( "No" )
# Number is divisible
# by 4 and 9 hence,
# number is divisible
# by 36
return ( "Yes" )
# Driver program
num = "92567812197966231384"
print (divisibleBy36(num))
# This code is contributed by Nikita Tiwari.


C#

// C# program to find if a number is
// divisible by 36 or not
using System;
class GFG {
// Function to check whether
// a number is divisible by
// 36 or not
static bool divisibleBy36(String num)
{
int l = num.Length;
// null number cannot
// be divisible by 36
if (l == 0)
return false ;
// single digit number other than
// 0 is not divisible by 36
if (l == 1 && num[0] != '0' )
return false ;
// number formed by the last
// 2 digits
int two_digit_num = (num[l-2] - '0' ) * 10
+ (num[l-1] - '0' ) ;
// if number is not divisible by 4
if (two_digit_num % 4 != 0)
return false ;
// number is divisible by 4 calculate
// sum of digits
int sum = 0;
for ( int i = 0; i < l; i++)
sum += (num[i] - '0' );
// sum of digits is not divisible by 9
if (sum % 9 != 0)
return false ;
// number is divisible by 4 and 9
// hence, number is divisible by 36
return true ;
}
// main function
public static void Main ()
{
String num = "92567812197966231384" ;
if (divisibleBy36(num))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
// This code is contributed by parashar.


PHP

<?php
// PHP implementation to
// check divisibility by 36
// Function to check whether a number
// is divisible by 36 or not
function divisibleBy36( $num )
{
$l = strlen ( $num );
// null number cannot
// be divisible by 36
if ( $l == 0)
return "No" ;
// single digit number other than
// 0 is not divisible by 36
if ( $l == 1 && $num [0] != '0' )
return "No" ;
// number formed by the
// last 2 digits
$two_digit_num = ( $num [ $l - 2] - '0' ) * 10 +
( $num [ $l - 1] - '0' ) ;
// if number is not
// divisible by 4
if ( $two_digit_num %4 != 0)
return "No" ;
// number is divisible by 4
// calculate sum of digits
$sum = 0;
for ( $i = 0; $i < $l ; $i ++)
$sum += ( $num [ $i ] - '0' );
// sum of digits is not
// divisible by 9
if ( $sum % 9 != 0)
return "No" ;
// number is divisible by 4 and 9
// hence, number is divisible by 36
return "Yes" ;
}
// Driver Code
$num = "92567812197966231384" ;
echo (divisibleBy36( $num ));
// This code is contributed by Ajit.
?>


Javascript

<script>
// Javascript implementation to
// check divisibility by 36
// Function to check whether a number
// is divisible by 36 or not
function divisibleBy36(num)
{
let l = num.length;
// null number cannot
// be divisible by 36
if (l == 0)
return "No" ;
// single digit number other than
// 0 is not divisible by 36
if (l == 1 && num[0] != '0' )
return "No" ;
// number formed by the
// last 2 digits
let two_digit_num = (num[l - 2] - '0' ) * 10 +
(num[l - 1] - '0' ) ;
// if number is not
// divisible by 4
if (two_digit_num%4 != 0)
return "No" ;
// number is divisible by 4
// calculate sum of digits
let sum = 0;
for (let i = 0; i < l; i++)
sum += (num[i] - '0' );
// sum of digits is not
// divisible by 9
if (sum % 9 != 0)
return "No" ;
// number is divisible by 4 and 9
// hence, number is divisible by 36
return "Yes" ;
}
// Driver Code
let num = "92567812197966231384" ;
document.write(divisibleBy36(num));
// This code is contributed by _saurabh_jaiswal.
</script>


输出:

Yes

时间复杂度:O(n) 本文由 阿尤什·焦哈里 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享