Skip to content
Related Articles

Related Articles

Improve Article

Check if the large number formed is divisible by 41 or not

  • Difficulty Level : Medium
  • Last Updated : 30 Apr, 2021

Given the first two digits of a large number digit1 and digit2. Also given a number c and the length of the actual large number. The next n-2 digits of the large number are calculated using the formula digit[i] = ( digit[i – 1]*c + digit[i – 2] ) % 10. The task is to check whether the number formed is divisible by 41 or not. 
Examples:
 

Input: digit1 = 1  , digit2 = 2  , c = 1  , n = 3
Output: YES
The number formed is 123
which is divisible by 41

Input: digit1 = 1  , digit2 = 4  , c = 6  , n = 3  
Output: NO

 

A naive approach is to form the number using the given formula. Check if the number formed is divisible by 41 or not using % operator. But since the number is very large, it will not be possible to store such a large number. 
Efficient Approach : All the digits are calculated using the given formula and then the associative property of multiplication and addition is used to check if it is divisible by 41 or not. A number is divisible by 41 or not means (number % 41) equals 0 or not. 
Let X be the large number thus formed, which can be written as. 
 

X = (digit[0] * 10^n-1) + (digit[1] * 10^n-2) + … + (digit[n-1] * 10^0) 
X = ((((digit[0] * 10 + digit[1]) * 10 + digit[2]) * 10 + digit[3]) … ) * 10 + digit[n-1] 
X % 41 = ((((((((digit[0] * 10 + digit[1]) % 41) * 10 + digit[2]) % 41) * 10 + digit[3]) % 41) … ) * 10 + digit[n-1]) % 41

 
Hence after all the digits are calculated, below algorithm is followed: 
 



  1. Initialize the first digit to ans.
  2. Iterate for all n-1 digits.
  3. Compute ans at every ith step by (ans * 10 + digit[i]) % 41 using associative property.
  4. Check for the final value of ans if it divisible by 41 r not.

Below is the implementation of the above approach. 
 

C++




// C++ program to check a large number
// divisible by 41 or not
#include <bits/stdc++.h>
using namespace std;
 
// Check if a number is divisible by 41 or not
bool DivisibleBy41(int first, int second, int c, int n)
{
    // array to store all the digits
    int digit[n];
 
    // base values
    digit[0] = first;
    digit[1] = second;
 
    // calculate remaining digits
    for (int i = 2; i < n; i++)
        digit[i] = (digit[i - 1] * c + digit[i - 2]) % 10;
 
    // calculate answer
    int ans = digit[0];
    for (int i = 1; i < n; i++)
        ans = (ans * 10 + digit[i]) % 41;
 
    // check for divisibility
    if (ans % 41 == 0)
        return true;
    else
        return false;
}
 
// Driver Code
int main()
{
 
    int first = 1, second = 2, c = 1, n = 3;
 
    if (DivisibleBy41(first, second, c, n))
        cout << "YES";
    else
        cout << "NO";
    return 0;
}

Java




// Java program to check
// a large number divisible
// by 41 or not
import java.io.*;
 
class GFG
{
// Check if a number is
// divisible by 41 or not
static boolean DivisibleBy41(int first,
                             int second,
                             int c, int n)
{
    // array to store
    // all the digits
    int digit[] = new int[n];
 
    // base values
    digit[0] = first;
    digit[1] = second;
 
    // calculate remaining
    // digits
    for (int i = 2; i < n; i++)
        digit[i] = (digit[i - 1] * c +
                    digit[i - 2]) % 10;
 
    // calculate answer
    int ans = digit[0];
    for (int i = 1; i < n; i++)
        ans = (ans * 10 +
               digit[i]) % 41;
 
    // check for
    // divisibility
    if (ans % 41 == 0)
        return true;
    else
        return false;
}
 
// Driver Code
public static void main (String[] args)
{
    int first = 1, second = 2, c = 1, n = 3;
 
    if (DivisibleBy41(first, second, c, n))
        System.out.println("YES");
    else
        System.out.println("NO");
}
}
 
// This code is contributed
// by akt_mit

Python3




# Python3 program to check
# a large number divisible
# by 41 or not
 
# Check if a number is
# divisible by 41 or not
def DivisibleBy41(first,
                  second, c, n):
 
    # array to store
    # all the digits
    digit = [0] * n
 
    # base values
    digit[0] = first
    digit[1] = second
 
    # calculate remaining
    # digits
    for i in range(2,n):
        digit[i] = (digit[i - 1] * c +
                    digit[i - 2]) % 10
 
    # calculate answer
    ans = digit[0]
    for i in range(1,n):
        ans = (ans * 10 + digit[i]) % 41
 
    # check for
    # divisibility
    if (ans % 41 == 0):
        return True
    else:
        return False
 
# Driver Code
first = 1
second = 2
c = 1
n = 3
 
if (DivisibleBy41(first,
                  second, c, n)):
    print("YES")
else:
    print("NO")
 
# This code is contributed
# by Smita

C#




// C# program to check
// a large number divisible
// by 41 or not
using System;
 
class GFG
{
     
// Check if a number is
// divisible by 41 or not
static bool DivisibleBy41(int first,
                          int second,
                          int c, int n)
{
    // array to store
    // all the digits
    int []digit = new int[n];
 
    // base values
    digit[0] = first;
    digit[1] = second;
 
    // calculate
    // remaining
    // digits
    for (int i = 2; i < n; i++)
        digit[i] = (digit[i - 1] * c +
                    digit[i - 2]) % 10;
 
    // calculate answer
    int ans = digit[0];
    for (int i = 1; i < n; i++)
        ans = (ans * 10 +
            digit[i]) % 41;
 
    // check for
    // divisibility
    if (ans % 41 == 0)
        return true;
    else
        return false;
}
 
// Driver Code
public static void Main ()
{
    int first = 1,
        second = 2,
        c = 1, n = 3;
 
    if (DivisibleBy41(first, second, c, n))
        Console.Write("YES");
    else
        Console.Write("NO");
}
}
 
// This code is contributed
// by Smita

PHP




<?php
// PHP program to check a
// large number divisible
// by 41 or not
 
// Check if a number is
// divisible by 41 or not
function DivisibleBy41($first, $second, $c, $n)
{
    // array to store
    // all the digits
    $digit[$n] = range(1, $n);
 
    // base values
    $digit[0] = $first;
    $digit[1] = $second;
 
    // calculate remaining digits
    for ($i = 2; $i < $n; $i++)
        $digit[$i] = ($digit[$i - 1] * $c +
                      $digit[$i - 2]) % 10;
 
    // calculate answer
    $ans = $digit[0];
    for ($i = 1; $i < $n; $i++)
        $ans = ($ans * 10 + $digit[$i]) % 41;
 
    // check for divisibility
    if ($ans % 41 == 0)
        return true;
    else
        return false;
}
 
// Driver Code
$first = 1;
$second = 2;
$c = 1;
$n = 3;
 
if (DivisibleBy41($first, $second, $c, $n))
    echo "YES";
else
    echo "NO";
 
// This code is contributed by Mahadev.
?>

Javascript




<script>
 
// Javascript program to check
// a large number divisible
// by 41 or not
 
// Check if a number is
// divisible by 41 or not
function DivisibleBy41(first, second,  c, n)
{
    // array to store
    // all the digits
    let digit = new Array(n).fill(0);
   
    // base values
    digit[0] = first;
    digit[1] = second;
   
    // calculate remaining
    // digits
    for (let i = 2; i < n; i++)
        digit[i] = (digit[i - 1] * c +
                    digit[i - 2]) % 10;
   
    // calculate answer
    let ans = digit[0];
    for (let i = 1; i < n; i++)
        ans = (ans * 10 +
               digit[i]) % 41;
   
    // check for
    // divisibility
    if (ans % 41 == 0)
        return true;
    else
        return false;
}
     
// driver program
     
    let first = 1, second = 2, c = 1, n = 3;
   
    if (DivisibleBy41(first, second, c, n))
        document.write("YES");
    else
        document.write("NO");
  
 // This code is contributed by susmitakundugoaldanga.
</script>
Output: 
YES

 

Time Complexity: O(N) 
Auxiliary Space: O(N)
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :