Open In App

Check if a number is divisible by 41 or not

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number, the task is to quickly check if the number is divisible by 41 or not. 

Examples: 

Input : x  = 123
Output : Yes

Input : 104413920565933
Output : YES

A solution to the problem is to extract the last digit and subtract 4 times of the last digit from the remaining number and repeat this process until a two-digit number is obtained. If the obtained two-digit number is divisible by 41, then the given number is divisible by 41.

Approach: 

  • Extract the last digit of the number/truncated number every time
  • Subtract 4*(last digit of the previous number) from the truncated number
  • Repeat the above three steps as long as necessary.

Illustrations: 

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

Mathematical Proof : 

Let 

\overline{a b c}       

be any number such that 

\overline{a b c}       

=100a+10b+c . 
Now assume that 

\overline{a b c}       

is divisible by 41. Then 
 
\overline{a b c}\equiv       

0 (mod 41) 
100a+10b+c

\equiv        

0 (mod 41) 
10(10a+b)+c
\equiv       

0 (mod 41) 
10
\overline{a b}       
+c

\equiv       

0 (mod 41)
Now that we have separated the last digit from the number, we have to find a way to use it. 
Make the coefficient of 


\overline{a b}       

1. 
In other words, we have to find an integer n such that 10n

\equiv       

1 mod 41. 
It can be observed that the smallest n which satisfies this property is -4 as -40
\equiv       
1 mod 41. 
Now we can multiply the original equation 10
\overline{a b}       
+c
\equiv       
0 (mod 41) 
by -4 and simplify it: 
-40
\overline{a b}       
-4c
\equiv       
0 (mod 41) 

\overline{a b}        
-4c

\equiv       
0 (mod 41) 
We have found out that if 
\overline{a b c}\equiv       
0 (mod 41) then, 

\overline{a b}       
-4c
\equiv       

0 (mod 41). 
In other words, to check if a 3-digit number is divisible by 41, 
we can just remove the last digit, multiply it by 4, 
and then subtract it from the rest of the two digits. 

Below is the implementation of above approach:

C++




// CPP program to validate above logic
#include <bits/stdc++.h>
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




<?php
// PHP program to validate above logic
 
// Function to check if the number
// is divisible by 41 or not
function 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
$n = 104413920565933;
if (isDivisible($n))
    echo "Yes"."\n";
else
    echo "No"."\n";
 
// This code is contributed
// by ChitraNayal
?>


Javascript




<script>
     
// JavaScript program to validate above logic
 
// Function to check if the number
// is divisible by 41 or not
     
    function isDivisible(n)
    {
        while (Math.floor(n / 100) != 0) {
// Extracting the last digit
            let d =  (n % 10);
  
// Truncating the number
            n = Math.floor(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);
    }
     
    let n = 104413920565933;
        if (isDivisible(n)) {
            document.write("Yes");
        } else {
            document.write("No");
        }
     
 
// This code is contributed by avanitrachhadiya2155
 
</script>


Output: 

Yes

 

Time Complexity: O(log10N)

Auxiliary Space: O(1), since no extra space has been taken.

Note: The above program may not make a lot of sense as could simply do n % 41 to check for divisibility. The idea of this program is to validate the concept. Also, this might be an efficient approach if input number is large and given as string.



Last Updated : 04 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads