📜  检查数字是否可被41整除

📅  最后修改于: 2021-04-28 17:58:31             🧑  作者: Mango

给定一个数字,任务是快速检查数字是否可被41整除。

例子:

Input : x  = 123
Output : Yes

Input : 104413920565933
Output : YES

解决该问题的方法是提取最后一位数字,并从剩余数字中减去最后一位数字的4倍,然后重复此过程,直到获得两位数字为止。如果获得的两位数可被41整除,则给定数字可被41整除。
方法:

  • 每次提取数字/截断数字的最后一位
  • 从截断的数字中减去4 *(前一个数字的最后一位)
  • 视需要重复上述三个步骤。

插图:

Illustration 1:
30873-->3087-4*3=3075-->307-4*5=287-->28-4*7=0
As the remainder is zero, 30873 is divisible by 41

Illustration 2:
104413920565933 --> 10441392056593 - 4*3= 10441392056581
10441392056581 --> 1044139205658 - 4*1 = 1044139205654
1044139205654 --> 104413920565 - 4*4 = 104413920549
104413920549 --> 10441392054 - 4*9 = 10441392018
10441392018 --> 1044139201 - 4*8 = 1044139169
1044139169 --> 104413916 - 4*9 = 104413880
104413880 --> 10441388 - 4*0 = 10441380
10441388 --> 1044138 - 4*8 = 1044106
1044106 --> 104410 - 4*6 = 104386
104386 --> 10438 - 4*6 = 10414
10414 --> 1041 - 4*4 = 1025
1025 --> 102 - 4*5 =82
Now, 82%41 = 0 --> 82 is divisible by 41 and hence, 104413920565933 is divisible by 41


下面是上述方法的实现:

C++
// CPP program to validate above logic
#include 
using namespace std;
 
// Function to check if the number
// is divisible by 41 or not
bool isDivisible(long long int n)
{
    while (n / 100)
    {
        // Extracting the last digit
        int d = n % 10;
 
        // Truncating the number
        n /= 10;
 
        // Subtracting the four times
        // the last digit from the
        // remaining number
        n -= d * 4;
    }
 
    // return true if number is divisible by 41
    return (n % 41 == 0);
}
 
int main()
{
    long long int n = 104413920565933;
    if (isDivisible(n))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}


Java
// Java program to validate above logic
 
class GFG {
 
// Function to check if the number
// is divisible by 41 or not
    static boolean isDivisible(long n) {
        while (n / 100 != 0) {
// Extracting the last digit
            int d = (int) (n % 10);
 
// Truncating the number
            n /= 10;
 
// Subtracting the four times
// the last digit from the
// remaining number
            n -= d * 4;
        }
 
// return true if number
// is divisible by 41
        return (n % 41 == 0);
    }
 
    public static void main(String[] args) {
        long n = 104413920565933L;
        if (isDivisible(n)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
 
    }
}
// This code is contributed by RAJPUT-JI


Python3
# Python3 Program to validate above logic
 
# Function to check if the number
# is divisible by 41 or not
def isDivisible(n) :
 
    while n // 100 :
 
        # Extracting the last digit
        d = n % 10
 
        # Truncating the number
        n //= 10
 
        # Subtracting the four times
        # the last digit from the 
        # remaining number
        n -= d * 4
 
    # return true if number is divisible by 41
    return n % 41 == 0
     
 
     
# Driver Code
if __name__ == "__main__" :
 
    n = 104413920565933
     
    if isDivisible(n) :
        print("Yes")
 
    else :
        print("No")
         
# This code is contributed by ANKITRAI1


C#
// C# program to validate above logic
using System;
 
class GFG
{
// Function to check if the number
// is divisible by 41 or not
static bool isDivisible(long n)
{
    while (n / 100 != 0)
    {
        // Extracting the last digit
        int d = (int)(n % 10);
 
        // Truncating the number
        n /= 10;
 
        // Subtracting the four times
        // the last digit from the
        // remaining number
        n -= d * 4;
    }
 
    // return true if number
    // is divisible by 41
    return (n % 41 == 0);
}
 
// Driver Code
static public void Main ()
{
    long n = 104413920565933;
    if (isDivisible(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Raj


PHP


输出:
Yes



请注意,上面的程序可能没有多大意义,因为可以简单地执行n%41来检查可除性。该程序的目的是验证该概念。另外,如果输入数字很大并指定为字符串,那么这可能是一种有效的方法。