Posted on by Kalkicode
Code Mathematics

# Check whether a given number is an Polydivisible number or not

Polydivisible numbers are a unique set of numbers that have a fascinating mathematical property. A polydivisible number is one that has a specific divisibility property for each of its digits. This property makes polydivisible numbers interesting to explore and identify. In this article, we'll delve into the concept of polydivisible numbers, understand the problem statement, and present an algorithmic approach to determine whether a given number is polydivisible or not.

## Problem Statement

A polydivisible number is a positive integer with the following property: if you read the digits from left to right, the number formed by the first digit, the number formed by the first two digits, the number formed by the first three digits, and so on, up to the entire number, are all divisible by their respective lengths.

For Example :

1. `98`:
• `currentNumber = 9` (first digit)
• `9 % 1 == 0` (divisible)
• `98 % 2 == 0` (divisible)
• Result: Polydivisible
2. `161`:
• `currentNumber = 1` (first digit)
• `1 % 1 == 0` (divisible)
• `16 % 2 == 0` (divisible)
• `161 % 3 != 0` (not divisible)
• Result: Not Polydivisible
3. `33`:
• `currentNumber = 3` (first digit)
• `3 % 1 == 0` (divisible)
• `33 % 2 != 0` (not divisible)
• Result: Not Polydivisible
4. `78`:
• `currentNumber = 7` (first digit)
• `7 % 1 == 0` (divisible)
• `78 % 2 == 0` (divisible)
• Result: Polydivisible
5. `180`:
• `currentNumber = 1` (first digit)
• `1 % 1 == 0` (divisible)
• `18 % 2 == 0` (divisible)
• `180 % 3 == 0` (divisible)
• Result: Polydivisible

## Idea to Solve

To determine whether a given number is polydivisible, we need to check the divisibility property for each digit sequence. We can break down the process into the following steps:

1. Convert the number into its individual digits and store them in an array.
2. Start with the first digit and progressively build the numbers formed by the first two digits, the first three digits, and so on.
3. Check if each of these numbers is divisible by its respective length.

## Pseudocode

``````function isPolydivisible(number):
if number <= 0:
return false

length = calculateNumberOfDigits(number)
digits = getDigits(number)
currentNumber = digits[0]

for i from 1 to length:
currentNumber = currentNumber * 10 + digits[i]
if currentNumber % (i + 1) != 0:
return false

return true``````

## Algorithm Explanation

1. Calculate the number of digits in the given number and store it in `length`.
2. Extract the individual digits of the number and store them in the array `digits`.
3. Initialize `currentNumber` with the value of the first digit.
4. Iterate from index 1 to `length - 1`:
• Update `currentNumber` by appending the next digit to it.
• Check if `currentNumber` is divisible by `i + 1` (current sequence length). If not, return `false`.
5. If the loop completes without returning `false`, return `true`.

## Program Solution

``````// C program for
// Check whether a given number is an Polydivisible number or not
#include <stdio.h>

void isPolydivisible(int number)
{
/*
Properties of polydivisible number
──────────────────────────────────
➀ Its first digit a is not 0.
➁ The number formed by its first two digits ab is a multiple of 2.
➂ The number formed by its first three digits abc is a multiple of 3.
➃ The number formed by its first four digits abcd is a multiple of 4.
➄ etc.
───────────────────────────────────
*/
int status = 0;
if (number > 0)
{
int n = number;
int length = 0;
// Count the number of digits in given number
while (n > 0)
{
n /= 10;
length++;
}
if (n == 1)
{
// When its single digit
status = 1;
}
else
{
// Auxiliary space to collecting digits
int data[length];
int i = length - 1;
int num = 0;
n = number;
// Collect number digit
while (n > 0)
{
data[i] = n % 10;
n /= 10;
i--;
}
// Get first digit
num = data[0];
i = 1;
if (num != 0)
{
// First case
status = 1;
}
// Check if digit is divisible by its size for 2...length-1
while (i < length && status == 1)
{
// Combine digit
num = (num *10) + data[i];
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = 0;
}
i++;
}
}
}
if (status == 1)
{
printf("\n Number %d is Polydivisible", number);
}
else
{
printf("\n Number %d is not Polydivisible", number);
}
}
int main(int argc, char const *argv[])
{
// Test Case of Base 10
isPolydivisible(98);
isPolydivisible(161);
isPolydivisible(33);
isPolydivisible(78);
isPolydivisible(180);
return 0;
}``````

#### input

`````` Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible``````
``````/*
Java Program for
Check whether a given number is an Polydivisible number or not
*/
public class Polydivisible
{
public void isPolydivisible(int number)
{
// Result indicator
boolean status = false;

//  Properties of polydivisible number
//  ──────────────────────────────────
//  ➀ Its first digit a is not 0.
//  ➁ The number formed by its first two digits ab is a multiple of 2.
//  ➂ The number formed by its first three digits abc is a multiple of 3.
//  ➃ The number formed by its first four digits abcd is a multiple of 4.
//  ➄ etc.
//  ───────────────────────────────────

if (number > 0)
{
int n = number;
int length = 0;
// Count the number of digits in given number
while (n > 0)
{
n /= 10;
length++;
}
if (n == 1)
{
// When its single digit
status = true;
}
else
{
// Auxiliary space to collecting digits
int[] data = new int[length];
int i = length - 1;
int num = 0;
n = number;
// Collect number digit
while (n > 0)
{
data[i] = n % 10;
n /= 10;
i--;
}
// Get first digit
num = data[0];
i = 1;
if (num != 0)
{
// First case
status = true;
}
// Check if digit is divisible by its size for 2...length-1
while (i < length && status)
{
// Combine digit
num = (num * 10) + data[i];
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = false;
}
i++;
}
}
}
if (status)
{
System.out.println(" Number " + number + " is Polydivisible");
}
else
{
System.out.println(" Number " + number + " is not Polydivisible");
}
}
public static void main(String[] args)
{
// Test Case of Base 10
}
}``````

#### input

`````` Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program for
Check whether a given number is an Polydivisible number or not
*/
class Polydivisible
{
public: void isPolydivisible(int number)
{
// Result indicator
bool status = false;
//  Properties of polydivisible number
//  ──────────────────────────────────
//  ➀ Its first digit a is not 0.
//  ➁ The number formed by its first two digits ab is a multiple of 2.
//  ➂ The number formed by its first three digits abc is a multiple of 3.
//  ➃ The number formed by its first four digits abcd is a multiple of 4.
//  ➄ etc.
//  ───────────────────────────────────
if (number > 0)
{
int n = number;
int length = 0;
// Count the number of digits in given number
while (n > 0)
{
n /= 10;
length++;
}
if (n == 1)
{
// When its single digit
status = true;
}
else
{
// Auxiliary space to collecting digits
int *data = new int[length];
int i = length - 1;
int num = 0;
n = number;
// Collect number digit
while (n > 0)
{
data[i] = n % 10;
n /= 10;
i--;
}
// Get first digit
num = data[0];
i = 1;
if (num != 0)
{
// First case
status = true;
}
// Check if digit is divisible by its size for 2...length-1
while (i < length && status)
{
// Combine digit
num = (num *10) + data[i];
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = false;
}
i++;
}
free(data);
}
}
if (status)
{
cout << " Number " << number << " is Polydivisible" << endl;
}
else
{
cout << " Number " << number << " is not Polydivisible" << endl;
}
}
};
int main()
{
// Test Case of Base 10
return 0;
}``````

#### input

`````` Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible``````
``````// Include namespace system
using System;
/*
Csharp Program for
Check whether a given number is an Polydivisible number or not
*/
public class Polydivisible
{
public void isPolydivisible(int number)
{
// Result indicator
Boolean status = false;
//  Properties of polydivisible number
//  ──────────────────────────────────
//  ➀ Its first digit a is not 0.
//  ➁ The number formed by its first two digits ab is a multiple of 2.
//  ➂ The number formed by its first three digits abc is a multiple of 3.
//  ➃ The number formed by its first four digits abcd is a multiple of 4.
//  ➄ etc.
//  ───────────────────────────────────
if (number > 0)
{
int n = number;
int length = 0;
// Count the number of digits in given number
while (n > 0)
{
n /= 10;
length++;
}
if (n == 1)
{
// When its single digit
status = true;
}
else
{
// Auxiliary space to collecting digits
int[] data = new int[length];
int i = length - 1;
int num = 0;
n = number;
// Collect number digit
while (n > 0)
{
data[i] = n % 10;
n /= 10;
i--;
}
// Get first digit
num = data[0];
i = 1;
if (num != 0)
{
// First case
status = true;
}
// Check if digit is divisible by its size for 2...length-1
while (i < length && status)
{
// Combine digit
num = (num * 10) + data[i];
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = false;
}
i++;
}
}
}
if (status)
{
Console.WriteLine(" Number " + number + " is Polydivisible");
}
else
{
Console.WriteLine(" Number " + number + " is not Polydivisible");
}
}
public static void Main(String[] args)
{
// Test Case of Base 10
}
}``````

#### input

`````` Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible``````
``````<?php
/*
Php Program for
Check whether a given number is an Polydivisible number or not
*/
class Polydivisible
{
public	function isPolydivisible(\$number)
{
// Result indicator
\$status = false;
//  Properties of polydivisible number
//  ──────────────────────────────────
//  ➀ Its first digit a is not 0.
//  ➁ The number formed by its first two digits ab is a multiple of 2.
//  ➂ The number formed by its first three digits abc is a multiple of 3.
//  ➃ The number formed by its first four digits abcd is a multiple of 4.
//  ➄ etc.
//  ───────────────────────────────────
if (\$number > 0)
{
\$n = \$number;
\$length = 0;
// Count the number of digits in given number
while (\$n > 0)
{
\$n = (int)(\$n / 10);
\$length++;
}
if (\$n == 1)
{
// When its single digit
\$status = true;
}
else
{
// Auxiliary space to collecting digits
\$data = array_fill(0, \$length, 0);
\$i = \$length - 1;
\$num = 0;
\$n = \$number;
// Collect number digit
while (\$n > 0)
{
\$data[\$i] = \$n % 10;
\$n = (int)(\$n / 10);
\$i--;
}
// Get first digit
\$num = \$data[0];
\$i = 1;
if (\$num != 0)
{
// First case
\$status = true;
}
// Check if digit is divisible by its size for 2...length-1
while (\$i < \$length && \$status)
{
// Combine digit
\$num = (\$num * 10) + \$data[\$i];
if ((\$num % (\$i + 1)) != 0)
{
// When n digit is not divisible by its size
\$status = false;
}
\$i++;
}
}
}
if (\$status)
{
echo " Number ".\$number.
" is Polydivisible".
"\n";
}
else
{
echo " Number ".\$number.
" is not Polydivisible".
"\n";
}
}
}

function main()
{
// Test Case of Base 10
}
main();``````

#### input

`````` Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible``````
``````/*
Node JS Program for
Check whether a given number is an Polydivisible number or not
*/
class Polydivisible
{
isPolydivisible(number)
{
// Result indicator
var status = false;
//  Properties of polydivisible number
//  ──────────────────────────────────
//  ➀ Its first digit a is not 0.
//  ➁ The number formed by its first two digits ab is a multiple of 2.
//  ➂ The number formed by its first three digits abc is a multiple of 3.
//  ➃ The number formed by its first four digits abcd is a multiple of 4.
//  ➄ etc.
//  ───────────────────────────────────
if (number > 0)
{
var n = number;
var length = 0;
// Count the number of digits in given number
while (n > 0)
{
n = parseInt(n / 10);
length++;
}
if (n == 1)
{
// When its single digit
status = true;
}
else
{
// Auxiliary space to collecting digits
var data = Array(length).fill(0);
var i = length - 1;
var num = 0;
n = number;
// Collect number digit
while (n > 0)
{
data[i] = n % 10;
n = parseInt(n / 10);
i--;
}
// Get first digit
num = data[0];
i = 1;
if (num != 0)
{
// First case
status = true;
}
// Check if digit is divisible by its size for 2...length-1
while (i < length && status)
{
// Combine digit
num = (num * 10) + data[i];
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = false;
}
i++;
}
}
}
if (status)
{
console.log(" Number " + number + " is Polydivisible");
}
else
{
console.log(" Number " + number + " is not Polydivisible");
}
}
}

function main()
{
// Test Case of Base 10
}
main();``````

#### input

`````` Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible``````
``````#  Python 3 Program for
#  Check whether a given number is an Polydivisible number or not
class Polydivisible :
def isPolydivisible(self, number) :
status = False
#   Properties of polydivisible number
#   ──────────────────────────────────
#   ➀ Its first digit a is not 0.
#   ➁ The number formed by its first two digits ab is a multiple of 2.
#   ➂ The number formed by its first three digits abc is a multiple of 3.
#   ➃ The number formed by its first four digits abcd is a multiple of 4.
#   ➄ etc.
#   ───────────────────────────────────
if (number > 0) :
n = number
length = 0
#  Count the number of digits in given number
while (n > 0) :
n = int(n / 10)
length += 1

if (n == 1) :
#  When its single digit
status = True
else :
data = [0] * (length)
i = length - 1
num = 0
n = number
#  Collect number digit
while (n > 0) :
data[i] = n % 10
n = int(n / 10)
i -= 1

#  Get first digit
num = data[0]
i = 1
if (num != 0) :
#  First case
status = True

#  Check if digit is divisible by its size for 2...length-1
while (i < length and status) :
#  Combine digit
num = (num * 10) + data[i]
if ((num % (i + 1)) != 0) :
#  When n digit is not divisible by its size
status = False

i += 1

if (status) :
print(" Number ", number ," is Polydivisible")
else :
print(" Number ", number ," is not Polydivisible")

def main() :
#  Test Case of Base 10

if __name__ == "__main__": main()``````

#### input

`````` Number  98  is Polydivisible
Number  161  is not Polydivisible
Number  33  is not Polydivisible
Number  78  is Polydivisible
Number  180  is Polydivisible``````
``````#  Ruby Program for
#  Check whether a given number is an Polydivisible number or not
class Polydivisible
def isPolydivisible(number)
#  Result indicator
status = false
#   Properties of polydivisible number
#   ──────────────────────────────────
#   ➀ Its first digit a is not 0.
#   ➁ The number formed by its first two digits ab is a multiple of 2.
#   ➂ The number formed by its first three digits abc is a multiple of 3.
#   ➃ The number formed by its first four digits abcd is a multiple of 4.
#   ➄ etc.
#   ───────────────────────────────────
if (number > 0)
n = number
length = 0
#  Count the number of digits in given number
while (n > 0)
n = n / 10
length += 1
end

if (n == 1)
#  When its single digit
status = true
else
#  Auxiliary space to collecting digits
data = Array.new(length) {0}
i = length - 1
num = 0
n = number
#  Collect number digit
while (n > 0)
data[i] = n % 10
n = n / 10
i -= 1
end

#  Get first digit
num = data[0]
i = 1
if (num != 0)
#  First case
status = true
end

#  Check if digit is divisible by its size for 2...length-1
while (i < length && status)
#  Combine digit
num = (num * 10) + data[i]
if ((num % (i + 1)) != 0)
#  When n digit is not divisible by its size
status = false
end

i += 1
end

end

end

if (status)
print(" Number ", number ," is Polydivisible", "\n")
else
print(" Number ", number ," is not Polydivisible", "\n")
end

end

end

def main()
#  Test Case of Base 10
end

main()``````

#### input

`````` Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible
``````
``````/*
Scala Program for
Check whether a given number is an Polydivisible number or not
*/
class Polydivisible()
{
def isPolydivisible(number: Int): Unit = {
// Result indicator
var status: Boolean = false;
//  Properties of polydivisible number
//  ──────────────────────────────────
//  ➀ Its first digit a is not 0.
//  ➁ The number formed by its first two digits ab is a multiple of 2.
//  ➂ The number formed by its first three digits abc is a multiple of 3.
//  ➃ The number formed by its first four digits abcd is a multiple of 4.
//  ➄ etc.
//  ───────────────────────────────────
if (number > 0)
{
var n: Int = number;
var length: Int = 0;
// Count the number of digits in given number
while (n > 0)
{
n = (n / 10).toInt;
length += 1;
}
if (n == 1)
{
// When its single digit
status = true;
}
else
{
// Auxiliary space to collecting digits
var data: Array[Int] = Array.fill[Int](length)(0);
var i: Int = length - 1;
var num: Int = 0;
n = number;
// Collect number digit
while (n > 0)
{
data(i) = n % 10;
n = (n / 10).toInt;
i -= 1;
}
// Get first digit
num = data(0);
i = 1;
if (num != 0)
{
// First case
status = true;
}
// Check if digit is divisible by its size for 2...length-1
while (i < length && status)
{
// Combine digit
num = (num * 10) + data(i);
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = false;
}
i += 1;
}
}
}
if (status)
{
println(" Number " + number + " is Polydivisible");
}
else
{
println(" Number " + number + " is not Polydivisible");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Polydivisible = new Polydivisible();
// Test Case of Base 10
}
}``````

#### input

`````` Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible``````
``````/*
Swift 4 Program for
Check whether a given number is an Polydivisible number or not
*/
class Polydivisible
{
func isPolydivisible(_ number: Int)
{
// Result indicator
var status: Bool = false;
//  Properties of polydivisible number
//  ──────────────────────────────────
//  ➀ Its first digit a is not 0.
//  ➁ The number formed by its first two digits ab is a multiple of 2.
//  ➂ The number formed by its first three digits abc is a multiple of 3.
//  ➃ The number formed by its first four digits abcd is a multiple of 4.
//  ➄ etc.
//  ───────────────────────────────────
if (number > 0)
{
var n: Int = number;
var length: Int = 0;
// Count the number of digits in given number
while (n > 0)
{
n = n / 10;
length += 1;
}
if (n == 1)
{
// When its single digit
status = true;
}
else
{
// Auxiliary space to collecting digits
var data: [Int] = Array(repeating: 0, count: length);
var i: Int = length - 1;
var num: Int = 0;
n = number;
// Collect number digit
while (n > 0)
{
data[i] = n % 10;
n = n / 10;
i -= 1;
}
// Get first digit
num = data[0];
i = 1;
if (num  != 0)
{
// First case
status = true;
}
// Check if digit is divisible by its size for 2...length-1
while (i < length && status)
{
// Combine digit
num = (num * 10) + data[i];
if ((num % (i + 1))  != 0)
{
// When n digit is not divisible by its size
status = false;
}
i += 1;
}
}
}
if (status)
{
print(" Number ", number ," is Polydivisible");
}
else
{
print(" Number ", number ," is not Polydivisible");
}
}
}
func main()
{
// Test Case of Base 10
}
main();``````

#### input

`````` Number  98  is Polydivisible
Number  161  is not Polydivisible
Number  33  is not Polydivisible
Number  78  is Polydivisible
Number  180  is Polydivisible``````
``````/*
Kotlin Program for
Check whether a given number is an Polydivisible number or not
*/
class Polydivisible
{
fun isPolydivisible(number: Int): Unit
{
// Result indicator
var status: Boolean = false;
//  Properties of polydivisible number
//  ──────────────────────────────────
//  ➀ Its first digit a is not 0.
//  ➁ The number formed by its first two digits ab is a multiple of 2.
//  ➂ The number formed by its first three digits abc is a multiple of 3.
//  ➃ The number formed by its first four digits abcd is a multiple of 4.
//  ➄ etc.
//  ───────────────────────────────────
if (number > 0)
{
var n: Int = number;
var length: Int = 0;
while (n > 0)
{
n = n / 10;
length += 1;
}
if (n == 1)
{
// When its single digit
status = true;
}
else
{
// Auxiliary space to collecting digits
val data: Array < Int > = Array(length)
{
0
};
var i: Int = length - 1;
var num: Int ;
n = number;
while (n > 0)
{
data[i] = n % 10;
n = n / 10;
i -= 1;
}
// Get first digit
num = data[0];
i = 1;
if (num != 0)
{
// First case
status = true;
}
while (i < length && status)
{
// Combine digit
num = (num * 10) + data[i];
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = false;
}
i += 1;
}
}
}
if (status)
{
println(" Number " + number + " is Polydivisible");
}
else
{
println(" Number " + number + " is not Polydivisible");
}
}
}
fun main(args: Array < String > ): Unit
{
// Test Case of Base 10
}``````

#### input

`````` Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible``````

## Time Complexity

The time complexity of this algorithm is primarily determined by the length of the given number, `n`, where `n` is the number of digits in the input number. In the worst case, we iterate through each digit once, resulting in a time complexity of O(n).

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post