📜  大量除数可被12整除

📅  最后修改于: 2021-04-26 08:11:42             🧑  作者: Mango

给定一个大数字,任务是检查数字是否可以被12整除。

例子 :

Input : 12244824607284961224
Output : Yes

Input : 92387493287593874594898678979792
Output : No

这是非常简单的方法。如果数字可以被4和3整除,那么数字可以被12整除。
点1 。如果数字的最后两位可被4整除,那么数字可被4整除。有关大数,请参见4的可除性。
点2 。如果数字的所有数字之和除以3,则数字可被3整除。有关大数,请参见3的除数。

CPP
// C++ Program to check if
// number is divisible by 12
#include 
using namespace std;
  
bool isDvisibleBy12(string num)
{
    // if number greater then 3
    if (num.length() >= 3) { 
  
        // fiind last digit
        int d1 = (int)num[num.length() - 1];
  
        // no is odd
        if (d1 % 2 != 0)
            return (0);
  
        // find second last digit
        int d2 = (int)num[num.length() - 2];
  
        // find sum of all digits
        int sum = 0;
        for (int i = 0; i < num.length(); i++)
            sum += num[i];            
          
        return (sum % 3 == 0 && (d2 * 10 + d1) % 4 == 0);            
    }
      
    else {
          
        // if number is less then
        // or equal to 100
        int number = stoi(num);
        return (number % 12 == 0);
    }
}
  
// Driver function
int main()
{
    string num = "12244824607284961224";  
    if (isDvisibleBy12(num))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}


Java
// Java Program to check if
// number is divisible by 12
  
import java.io.*;
  
class GFG {
static boolean isDvisibleBy12(String num)
{
    // if number greater then 3
    if (num.length() >= 3) { 
   
        // fiind last digit
        int d1 = (int)num.charAt(num.length() - 1);
   
        // no is odd
        if (d1 % 2 != 0)
            return false;
   
        // find second last digit
        int d2 = (int)num.charAt(num.length() - 2);
   
        // find sum of all digits
        int sum = 0;
        for (int i = 0; i < num.length(); i++)
            sum += num.charAt(i);            
           
        return (sum % 3 == 0 &&
               (d2 * 10 + d1) % 4 == 0);            
    }
       
    else {
           
        // if number is less then
        // or equal to 100
        int number = Integer.parseInt(num);
        return (number % 12 == 0);
    }
  
// driver function
}
    public static void main (String[] args) {
  
    String num = "12244824607284961224";  
    if (isDvisibleBy12(num))
        System.out.print("Yes");
    else
        System.out.print("No");
          
    }
}
  
// This code is contributed by Gitanjali.


Python3
# Python Program to check if
# number is divisible by 12 
  
import math 
  
def isDvisibleBy12( num):
  
    # if number greater then 3
    if (len(num) >= 3):
   
        # fiind last digit
        d1 = int(num[len(num) - 1]);
   
        # no is odd
        if (d1 % 2 != 0):
            return False
   
        # find second last digit
        d2 = int(num[len(num) - 2])
   
        # find sum of all digits
        sum = 0
        for  i in range(0, len(num) ):
            sum += int(num[i])           
           
        return (sum % 3 == 0 and
               (d2 * 10 + d1) % 4 == 0)            
  
       
    else :
           
        # f number is less then
        # r equal to 100
        number = int(num)
        return (number % 12 == 0)
      
  
num = "12244824607284961224"  
if(isDvisibleBy12(num)):
       print("Yes")
else:
       print("No")
  
# This code is contributed by Gitanjali.


C#
// C# Program to check if
// number is divisible by 12
using System;
  
class GFG 
{
static bool isDvisibleBy12(string num)
{
    // if number greater then 3
    if (num.Length >= 3) { 
  
        // find last digit
        int d1 = (int)num[num.Length - 1];
  
        // no is odd
        if (d1 % 2 != 0)
            return false;
  
        // find second last digit
        int d2 = (int)num[num.Length - 2];
  
        // find sum of all digits
        int sum = 0;
        for (int i = 0; i < num.Length; i++)
            sum += num[i];         
          
        return (sum % 3 == 0 &&
            (d2 * 10 + d1) % 4 == 0);         
    }
      
    else {
          
        // if number is less then
        // or equal to 100
        int number = int.Parse(num);
        return (number % 12 == 0);
    }
}
  
    // Driver function
    public static void Main () 
    {
       String num = "12244824607284961224"; 
       if (isDvisibleBy12(num))
          Console.Write("Yes");
       else
          Console.Write("No");
    }
}
  
// This code is contributed by nitin mittal.


PHP
= 3) 
    { 
  
        // find last digit
        $d1 = (int)$num[strlen($num) - 1];
  
        // no is odd
        if ($d1 % 2 != 0)
            return (0);
  
        // find second last digit
        $d2 = (int)$num[strlen($num) - 2];
  
        // find sum of all digits
        $sum = 0;
        for ($i = 0; $i < strlen($num); $i++)
            $sum += $num[$i];     
          
        return ($sum % 3 == 0 && 
               ($d2 * 10 + $d1) % 4 == 0);         
    }
      
    else {
          
        // if number is less then
        // or equal to 100
        $number = stoi($num);
        return ($number % 12 == 0);
    }
}
  
// Driver Code
$num = "12244824607284961224"; 
if (isDvisibleBy12($num))
    echo("Yes");
else
    echo("No");
  
// This code is contributed by Ajit.
?>


输出:

Yes