Open In App

Check whether a given number is Polydivisible or Not

Last Updated : 23 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer n, find whether n is a Polydivisible or not. In mathematics, a number is called Polydivisible if it follows some unique properties. The number should not have any leading zeroes. The number formed by first i digits of the input number should be divisible by i, where i > 1 ~and ~i <= number ~of ~digits ~in ~the ~input ~number    . If any number follow these properties then it is called Polydivisible number

Examples: 

Input: 345654
Output: 345654 is Polydivisible number.
Explanation: 
The first digit of the number is non-zero. 
The number formed by the first 2 digits(34) 
is divisible by 2. The number formed by the
first 3 digits(345) is divisible by 3. 
The number formed by the first 4 digits(3456)
is divisible by 4. The number formed by the
first 5 digits(34565) is divisible by 5. 
The number formed by the first 6 digits(345654)
is divisible by 6.     

Input: 130
Output: 130 is Not Polydivisible number.

Input: 129
Output: 129 is Polydivisible number.    

Approach: The idea is very simple.  

  1. Extract all the digits of the array and store them in an array.
  2. Pick first 2 digits and form a number and check if it is divisible by 2.
  3. Pick ith digit and append to the existing number and check if the number is divisible by i.
  4. If all the above conditions are satisfied until all the digits are exhausted,then the given number is Polydivisible.

Below is the implementation of the above approach as follows:  

C++

// CPP program to check whether
// a number is polydivisible or not
#include <bits/stdc++.h>
using namespace std;
 
// function to check polydivisible
// number
void check_polydivisible(int n)
{
    int N = n;
    vector<int> digit;
 
    // digit extraction of input number
    while (n > 0) {
 
        // store the digits in an array
        digit.push_back(n % 10);
        n /= 10;
    }
    reverse(digit.begin(), digit.end());
 
    bool flag = true;
    n = digit[0];
    for (int i = 1; i < digit.size(); i++) {
 
        // n contains first i digits
        n = n * 10 + digit[i];
 
        // n should be divisible by i
        if (n % (i + 1) != 0) {
            flag = false;
            break;
        }
    }
    if (flag)
        cout << N << " is Polydivisible number.";
    else
        cout << N << " is Not Polydivisible number.";
}
 
int main()
{
    int n = 345654;
    check_polydivisible(n);
}

                    

Java

// Java program to check whether
// a number is polydivisible or not
import java.util.*;
import java.io.*;
 
class GFG {
     
    // function to check polydivisible
    // number
    static void check_polydivisible(int n)
    {
        int N = n;
        Vector<Integer> digit = new Vector<Integer>();
     
        // digit extraction of input number
        while (n > 0) {
     
            // store the digits in an array
            digit.add(new Integer(n % 10));
            n /= 10;
        }
        Collections.reverse(digit);
     
        boolean flag = true;
        n = digit.get(0);
        for (int i = 1; i < digit.size(); i++) {
     
            // n contains first i digits
            n = n * 10 + digit.get(i);
     
            // n should be divisible by i
            if (n % (i + 1) != 0) {
                flag = false;
                break;
            }
        }
        if (flag)
            System.out.println(N + " is Polydivisible number.");
        else
            System.out.println(N + " is Not Polydivisible number.");
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 345654;
        check_polydivisible(n);
    }
}

                    

Python3

# Python 3 program to check whether
# a number is polydivisible or not
 
# function to check polydivisible
# number
def check_polydivisible(n):
    N = n
    digit = []
  
    # digit extraction of input number
    while (n > 0):
          
        # store the digits in an array
        digit.append(n % 10)
        n //= 10
  
    digit = digit[::-1]
  
    flag = True
    n = digit[0]
    for i in range(1, len(digit), 1):
          
        # n contains first i digits
        n = n * 10 + digit[i]
  
        # n should be divisible by i
        if (n % (i + 1) != 0):
            flag = False
            break
      
    if (flag):
        print(N, "is Polydivisible number.")
    else:
        print(N, "is Not Polydivisible number.")
 
# Driver Code
if __name__ == '__main__':
    n = 345654
    check_polydivisible(n)
 
     
# This code is contributed by
# Sahil_Shelangia
# Improved by Madhushree Sannigrahi

                    

C#

// C# program to check whether
// a number is polydivisible or not
using System;
using System.Collections.Generic;
 
class GFG
{
     
    // function to check polydivisible
    // number
    static void check_polydivisible(int n)
    {
        int N = n;
        List<int> digit = new List<int>();
     
        // digit extraction of input number
        while (n > 0)
        {
     
            // store the digits in an array
            digit.Add((int)n % 10);
            n /= 10;
        }
        digit.Reverse();
     
        bool flag = true;
        n = digit[0];
        for (int i = 1; i < digit.Count; i++)
        {
     
            // n contains first i digits
            n = n * 10 + digit[i];
     
            // n should be divisible by i
            if (n % (i + 1) != 0)
            {
                flag = false;
                break;
            }
        }
        if (flag)
            Console.WriteLine(N +
                    " is Polydivisible number.");
        else
            Console.WriteLine(N +
                    " is Not Polydivisible number.");
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int n = 345654;
        check_polydivisible(n);
    }
}
 
// This code is contributed by Rajput-Ji

                    

Javascript

<script>
    // Javascript program to check whether
    // a number is polydivisible or not
     
    // function to check polydivisible
    // number
    function check_polydivisible(n)
    {
        let N = n;
        let digit = [];
       
        // digit extraction of input number
        while (n > 0)
        {
       
            // store the digits in an array
            digit.push(n % 10);
            n = parseInt(n / 10, 10);
        }
        digit.reverse();
       
        let flag = true;
        n = digit[0];
        for (let i = 1; i < digit.length; i++)
        {
       
            // n contains first i digits
            n = n * 10 + digit[i];
       
            // n should be divisible by i
            if (n % (i + 1) != 0)
            {
                flag = false;
                break;
            }
        }
        if (flag)
            document.write(N + " is Polydivisible number." + "</br>");
        else
            document.write(N + " is Not Polydivisible number.");
    }
     
    let n = 345654;
      check_polydivisible(n);
     
</script>

                    

Output: 
345654 is Polydivisible number.

 

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads