📜  被大数除以37

📅  最后修改于: 2021-05-04 10:14:33             🧑  作者: Mango

给定一个大的n,我们需要检查它是否可以被37整除。如果可以被37整除,则输出true,否则返回False。
例子:

Input  : 74
Output : True

Input : 73
Output : False

Input : 8955795758 (10 digit number)
Output : True

A R位数m中数字的形式是(AR-1 AR-2 … .a2 A1 A0)为整除37当且仅当一系列的数字(A2 A1 A0)+(A5 A4 A3)+(A8的总和a7 a6)+…可除以37。括号内的数字三元组表示数字形式的3位数。

例子:

Input : 8955795758 (10 digit number)
Output : True
Explanation:
     We express the number in terms of 
     triplets of digits as follows.
     (008)(955)(795)(758)
     Now, 758 + 795 + 955 + 8 = 2516
     For 2516, the triplets will be:
     (002)(516)
     Now 516 + 2 = 518 which is divisible 
     by 37. Hence the number is divisible 
     by 37.

Input : 189710809179199 (15 digit number)
Output : False

一种简单而有效的方法是采用字符串形式的输入(如果需要,通过在数字的左边加0来使其长度为3 * m),然后必须将数字从右到左以3为单位添加,直到它变成一个3位数的数字组成一个系列。计算系列的总和。如果系列的总和中包含3个以上的数字,请再次递归调用此函数。
最后检查结果总和是否可被37整除。
这是检查37除数的程序实现。

C++
// CPP program for checking divisibility by 37
// function divisible37 which returns True if
// number is divisible by 37 otherwise False
#include 
using namespace std;
 
int divisibleby37(string n){
    int l = n.length();
    if (n == "0")
        return 0;
 
    // Append required 0's at the beginning
    if (l % 3 == 1){
        n = "00"+ n;
        l += 2;
    }
    else if (l % 3 == 2){
        n = "0"+ n;
        l += 1;
    }
     
    int gSum = 0;
     
    while (l != 0){
 
    // group saves 3-digit group
    string group = n.substr(l - 3, l);
        l = l - 3;
    int gvalue = (group[0] - '0') * 100 +
                 (group[1] - '0') * 10 +
                 (group[2] - '0') * 1;
                  
    // add the series
    gSum = gSum + gvalue;
    }
     
    // if sum of series gSum has minimum 4
    // digits in it, then again recursive
    // call divisibleby37 function
    if (gSum >= 1000)
        return (divisibleby37(to_string(gSum)));
    else
        return (gSum % 37 == 0);
 
}
 
// drive program to test the above function
int main(){
 
    string s="8955795758";
     
    if (divisibleby37(s))
    cout<<"True";
    else
    cout<<"False";
    return 0;
}
// This code is contributed by Prerna Saini


Java
// Java program for checking
// divisibility by 37
 
class GFG
{
// function divisible37 which
// returns True if number is
// divisible by 37 otherwise False
static int divisibleby37(String n1)
{
    int l = n1.length();
    if (n1 == "0")
        return 0;
 
    // Append required 0's
    // at the beginning
    if (l % 3 == 1)
    {
        n1 = "00"+ n1;
        l += 2;
    }
    else if (l % 3 == 2)
    {
        n1 = "0"+ n1;
        l += 1;
    }
    char[]  n= n1.toCharArray();
    int gSum = 0;
    while (l != 0)
    {
 
    // group saves 3-digit group
    int gvalue;
    if(l == 2)
        gvalue = ((int)n[(l - 2)] - 48) * 100 +
                ((int)n[(l - 1)] - 48) * 10;
    else if(l == 1)
        gvalue = ((int)n[(l - 1)] - 48) * 100;
    else
        gvalue = ((int)n[(l - 3)] - 48) * 100 +
                ((int)n[(l - 2)] - 48) * 10 +
                ((int)n[(l - 1)] - 48) * 1;
    l = l - 3;
     
    // add the series
    gSum = gSum + gvalue;
    }
     
    // if sum of series gSum has minimum 4
    // digits in it, then again recursive
    // call divisibleby37 function
    if (gSum >= 1000)
        return (divisibleby37(String.valueOf(gSum)));
    else
        return (gSum % 37 == 0) ? 1 : 0;
 
}
 
// Driver Code
public static void main(String[] args)
{
    String s="8955795758";
     
    if (divisibleby37(s) == 1)
    System.out.println("True");
    else
    System.out.println("False");
}
}
 
// This code is contributed by mits


Python3
# Python code for checking divisibility by 37
# function divisible37 which returns True if
# number is divisible by 37 otherwise False
def divisibleby37(n):
    l = len(n)
    if (n == 0):
        return True
   
    # Append required 0's at the beginning
    if (l%3 == 1):
        n = "00"+ n
        l += 2
    elif (l%3 == 2):
        n = "0"+ n
        l += 1
 
    gSum = 0
    while (l != 0):
 
        # group saves 3-digit group
        group = int(n[l-3:l])
        l = l-3
 
        # add the series
        gSum = gSum + group
 
    # if sum of series gSum has minimum 4
    # digits in it, then again recursive
    # call divisibleby37 function
    if (gSum >= 1000):
        return(divisibleby37(str(gSum)))
    else:
        return (gSum%37==0)
 
# Driver method to test the above function
print(divisibleby37("8955795758"))


C#
// C# program for checking
// divisibility by 37
using System;
 
class GFG
{
// function divisible37 which
// returns True if number is
// divisible by 37 otherwise False
static int divisibleby37(string n)
{
    int l = n.Length;
    if (n == "0")
        return 0;
 
    // Append required 0's
    // at the beginning
    if (l % 3 == 1)
    {
        n = "00"+ n;
        l += 2;
    }
    else if (l % 3 == 2)
    {
        n = "0"+ n;
        l += 1;
    }
     
    int gSum = 0;
    while (l != 0)
    {
 
    // group saves 3-digit group
    int gvalue;
    if(l == 2)
        gvalue = ((int)n[(l - 2)] - 48) * 100 +
                 ((int)n[(l - 1)] - 48) * 10;
    else if(l == 1)
        gvalue = ((int)n[(l - 1)] - 48) * 100;
    else
        gvalue = ((int)n[(l - 3)] - 48) * 100 +
                 ((int)n[(l - 2)] - 48) * 10 +
                 ((int)n[(l - 1)] - 48) * 1;
    l = l - 3;
     
    // add the series
    gSum = gSum + gvalue;
    }
     
    // if sum of series gSum has minimum 4
    // digits in it, then again recursive
    // call divisibleby37 function
    if (gSum >= 1000)
        return (divisibleby37(gSum.ToString()));
    else
        return (gSum % 37 == 0) ? 1 : 0;
 
}
 
// Driver Code
public static void Main()
{
    string s="8955795758";
     
    if (divisibleby37(s) == 1)
    Console.WriteLine("True");
    else
    Console.WriteLine("False");
}
}
 
// This code is contributed by mits


PHP
= 1000)
        return (divisibleby37((string)($gSum)));
    else
        return ($gSum % 37 == 0);
 
}
 
// Driver code
$s = "8955795758";
 
if (divisibleby37($s))
echo "True";
else
echo "False";
 
// This code is contributed
// by mits
?>


Javascript


输出:

True