📜  检查大数是否可以被6整除

📅  最后修改于: 2021-05-07 00:22:45             🧑  作者: Mango

给定一个数字,任务是检查数字是否可以被6整除。输入数字可能很大,即使我们使用long long int,也可能无法存储。
例子:

Input  : n = 2112
Output : Yes

Input : n = 1124
Output : No

Input  : n = 363588395960667043875487
Output : No

由于输入数字可能很大,因此我们不能使用n%6来检查数字是否可以被8整除,尤其是在C / C++之类的语言中。这个想法是基于以下事实。

A number is divisible by 6 it's divisible by 2 and 3. 
a)  A number is divisible by 2 if its last digit is divisible by 2.
b)  A number is divisible by 3 if sum of digits is divisible by 3.

下面是基于上述步骤的实现。

C++
// C++ program to find if a number is divisible by
// 6 or not
#include
using namespace std;
 
// Function to find that number divisible by 6 or not
bool check(string str)
{
    int n = str.length();
 
    // Return false if number is not divisible by 2.
    if ((str[n-1]-'0')%2 != 0)
       return false;
 
    // If we reach here, number is divisible by 2.
    // Now check for 3.
 
    // Compute sum of digits
    int digitSum = 0;
    for (int i=0; i


Java
// Java program to find if a number is
// divisible by 6 or not
class IsDivisible
{
    // Function to find that number divisible by 6 or not
    static boolean check(String str)
    {
        int n = str.length();
      
        // Return false if number is not divisible by 2.
        if ((str.charAt(n-1) -'0')%2 != 0)
           return false;
      
        // If we reach here, number is divisible by 2.
        // Now check for 3.
      
        // Compute sum of digits
        int digitSum = 0;
        for (int i=0; i


Python3
# Python 3 program to find
# if a number is divisible
# by 6 or not
 
# Function to find that number
# is divisible by 6 or not
def check(st) :
    n = len(st)
     
     
    # Return false if number
    # is not divisible by 2.
    if (((int)(st[n-1])%2) != 0) :
        return False
  
    # If we reach here, number
    # is divisible by 2. Now
    # check for 3.
  
    # Compute sum of digits
    digitSum = 0
    for i in range(0, n) :
        digitSum = digitSum + (int)(st[i])
  
    # Check if sum of digits
    # is divisible by 3
    return (digitSum % 3 == 0)
 
 
# Driver code
st = "1332"
if(check(st)) :
    print("Yes")
else :
    print("No ")
     
# This article is contributed by Nikita Tiwari.


C#
// C# program to find if a number is
// divisible by 6 or not
using System;
 
class GFG {
     
    // Function to find that number
    // divisible by 6 or not
    static bool check(String str)
    {
        int n = str.Length;
     
        // Return false if number is
        // not divisible by 2.
        if ((str[n-1] -'0') % 2 != 0)
            return false;
     
        // If we reach here, number is
        // divisible by 2.
        // Now check for 3.
     
        // Compute sum of digits
        int digitSum = 0;
        for (int i = 0; i < n; i++)
            digitSum += (str[i] - '0');
     
        // Check if sum of digits is
        // divisible by 3
        return (digitSum % 3 == 0);
    }
     
    // main function
    public static void Main ()
    {
        String str = "1332";
         
        if(check(str))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
 
// This code is contributed by parashar.


PHP


Javascript


输出:

Yes