📜  通过999检查任何大数的除数

📅  最后修改于: 2021-05-04 09:32:56             🧑  作者: Mango

给您一个n位数字的大数,您必须检查它是否可以被999整除,而不用除以999或找到数字的模。
例子:

Input : 235764 
Output : Yes

Input : 23576 
Output : No

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

解决方案基于以下事实。

插图:

Input : 235764 
Output : Yes
Explanation : Step I - read input : 235, 764
              Step II - 235 + 764 = 999
              As result is 999 then we can 
              conclude that it is divisible by 999.

Input : 1244633121
Output : Yes
Explanation : Step I - read input : 1, 244, 633, 121
              Step II - 001 + 244 + 633 + 121 = 999
              As result is 999 then we can conclude 
              that it is divisible by 999.

Input : 999999999
Output : Yes
Explanation : Step I - read input : 999, 999, 999
              Step II - 999 + 999 + 999 = 2997
              Step III - 997 + 002 = 999
              As result is 999 then we can conclude  
              that it is divisible by 999.

这是如何运作的?

Let us consider 235764, we can write it as
235764 = 2*105 + 3*104 + 5*103 + 
         7*102 + 6*10 + 4

The idea is based on below observation:
Remainder of 103 divided by 999 is 1
For i > 3, 10i % 999 = 10i-3 % 999 

Let us see how we use above fact.
Remainder of 2*105 + 3*104 + 5*103 + 
7*102 + 6*10 + 4
Remainder with 999 can be written as : 
2*100 + 3*10 + 5*1 + 7*100 + 6*10 + 4 
The above expression is basically sum of
groups of size 3.

Since the sum is divisible by 999, answer is yes.

一种简单有效的方法是采用字符串形式的输入(如果需要,可以通过在数字的左边加0来使其长度为3 * m形式),然后必须将数字从右到左以3为单位添加,直到它变成3位数字,如果结果为999,则可以说该数字可被999整除。
与“除数为9”的情况一样,我们检查所有数字的总和是否可以除以9,在除数为999的情况下也是如此。我们将所有3位数字组从右向左求和,然后检查最终结果是否为999。

C++
// CPP for divisibility of number by 999
#include
using namespace std;
 
// function to check divisibility
bool isDivisible999(string num)
{
    int n = num.length();
    if (n == 0 && num[0] == '0')
       return true;
 
    // Append required 0s at the beginning.
    if (n % 3 == 1)
       num = "00" + num;
    if (n % 3 == 2)
       num = "0" + num;
 
    // add digits in group of three in gSum
    int gSum = 0;
    for (int i = 0; i 1000)
    {
        num = to_string(gSum);
        n = num.length();
        gSum = isDivisible999(num);
    }
    return (gSum == 999);
}
 
// driver program
int main()
{
    string num = "1998";
    int n = num.length();
    if (isDivisible999(num))
        cout << "Divisible";
    else
        cout << "Not divisible";
    return 0;
}


Java
//Java for divisibility of number by 999
 
class Test
{   
    // Method to check divisibility
    static boolean isDivisible999(String num)
    {
        int n = num.length();
        if (n == 0 && num.charAt(0) == '0')
           return true;
      
        // Append required 0s at the beginning.
        if (n % 3 == 1)
           num = "00" + num;
        if (n % 3 == 2)
           num = "0" + num;
      
        // add digits in group of three in gSum
        int gSum = 0;
        for (int i = 0; i 1000)
        {
            num = Integer.toString(gSum);
            n = num.length();
            gSum = isDivisible999(num) ? 1 : 0;
        }
        return (gSum == 999);
    }
     
    // Driver method
    public static void main(String args[])
    {
        String num = "1998";
      
        System.out.println(isDivisible999(num) ? "Divisible" : "Not divisible");
    }
}


Python 3
# Python3 program for divisibility
# of number by 999
 
# function to check divisibility
def isDivisible999(num):
    n = len(num);
    if(n == 0 or num[0] == '0'):
        return true
 
    # Append required 0s at the beginning.
    if((n % 3) == 1):
        num = "00" + num
    if((n % 3) == 2):
        num = "0" + num
 
    # add digits in group of three in gSum    
    gSum = 0
    for i in range(0, n, 3):
         
        # group saves 3-digit group
        group = 0
        group += (ord(num[i]) - 48) * 100
        group += (ord(num[i + 1]) - 48) * 10
        group += (ord(num[i + 2]) - 48)
        gSum += group
 
    # calculate result till 3 digit sum    
    if(gSum > 1000):
        num = str(gSum)
        n = len(num)
        gSum = isDivisible999(num)
    return (gSum == 999)
 
# Driver code
if __name__=="__main__":
    num = "1998"
    n = len(num)
    if(isDivisible999(num)):
        print("Divisible")
    else:
        print("Not divisible")
         
# This code is contributed
# by Sairahul Jella


C#
// C# code for divisibility of number by 999
 
using System;
class Test
{
    // Method to check divisibility
    static bool isDivisible999(String num)
    {
        int n = num.Length;
        if (n == 0 && num[0] == '0')
        return true;
     
        // Append required 0s at the beginning.
        if (n % 3 == 1)
        num = "00" + num;
        if (n % 3 == 2)
        num = "0" + num;
     
        // add digits in group of three in gSum
        int gSum = 0;
        for (int i = 0; i 1000)
        {
            num = Convert.ToString(gSum);
            n = num.Length ;
            gSum = isDivisible999(num) ? 1 : 0;
        }
        return (gSum == 999);
    }
     
    // Driver method
    public static void Main()
    {
        String num = "1998";
     
        Console.WriteLine(isDivisible999(num) ? "Divisible" : "Not divisible");
    }
    // This code is contributed by Ryuga
}


PHP
 1000)
    {
        $num = strval($gSum);
        $n = strlen($num);
        $gSum = isDivisible999($num);
    }
    return ($gSum == 999);
}
 
// Driver Code
$num = "1998";
if (isDivisible999($num))
    echo "Divisible";
else
    echo "Not divisible";
 
// This code is contributed by mits
?>


Javascript


输出:

Divisible