In this article, we will discuss the Polydivisible Number in C++ with its algorithm, pseudocode, and examples.
What is the Polydivisible Number in C++?
A number is said to be polydivisible if certain divisibility requirements are met by its prefixes. The first digit of a whole number N with k digits must be divisible by 1, the first two by 2, the first three by 3, and so on. As an illustration, 123 is polydivisible since1÷1=1, 12÷2=6, and 123÷3=41. By repeatedly extracting a number's prefixes and confirming that they are divisible by their lengths, we can check this in C++ .
A number is considered polydivisible if each of its prefixes meets a special divisibility property. The following occurs when a number is polydivisible:
- The first number, which is the most significant, is divisible by 1.
- The first two numbers added together are divisible by two.
- The first three numbers added together are divisible by three.
- The procedure keeps going until the entire number is confirmed.
In other words, for a number N with digits d 1, d 2,…, d k, the prefix created by the first i digits, For i=1,2,…,k, P i =d 1 d 2…d i must be divisible by i
For Example:
1: 1÷1=1 (valid).
12: 1÷1=1, 12÷2=6 (valid).
123: 1÷1=1, 12÷2=6, 123÷3=41 (valid).
1234: 1÷1=1, 12÷2=6, 123÷3=4, 1234÷4=308.5 (invalid).
Input: N=768
Output: 768 is a polydivisible number.
For example, if p>1 and p<=number of digits in the number, then each p digit is divisible by p.
Input: 42587
Output: There is no polydivisible integer equal to 42587.
The explanation is that p=3 is not a polydivisible number because it does not satisfy the necessary conditions. Using the first three digits of the number, 425 is a number that cannot be divided by 3.
Algorithm:
- All of the digits of the specified number N will be removed and stored.
- In order to store each digit, we'll utilize an array . Using modulo function, we will extract the digits and divide each number by 10.
- Since the array's stored digits will be in reverse order, we will use the reverse function to flip the array.
- Check whether each p digit is divisible by p by iterating through for loop. A number is considered polydivisible if it holds true for all values of p up until p equals the number of digits in the provided number.
- Using for loop, we will now determine whether or not the number is polydivisible.
Pseudo code:
function isPolydivisible(number)
convert number to string numStr
set length to size of numStr
for i from1 to length do
set prefix to the first i characters of numStr, converted to integer
if prefix mod i ≠ 0 then
return false
end if
end for
return true
end function
main
input number
call isPolydivisible(number) and store result
if result is true then
print "The number is polydivisible."
else
print "The number is not polydivisible."
end if
end main
Example:
Let us take an example to illustrate the polydivisible number in C++.
#include <iostream>
#include <string>
using namespace std;
// Function to check if a number is polydivisible
bool isPolydivisible(long long number) {
string numStr = to_string(number);
int length = numStr.size();
for (int i = 1; i <= length; ++i) {
// Extract the prefix of length i
long long prefix = stoll(numStr.substr(0, i));
// Check if the prefix is divisible by i
if (prefix % i != 0) {
return false; // Not polydivisible
}
}
return true; // Polydivisible
}
int main() {
long long number;
cout << "Enter a number to check if it is polydivisible: ";
cin >> number;
if (isPolydivisible(number)) {
cout << number << " is a polydivisible number." << endl;
} else {
cout << number << " is not a polydivisible number." << endl;
}
return 0;
}
Output:
Enter a number to check if it is polydivisible: 34
34 is a polydivisible number.
Enter a number to check if it is polydivisible: 42587
42587 is not a polydivisible number.
Explanation:
- Input Conversion: In order to make prefix extraction simple, the integer is transformed into a string.
- Prefix extraction: Prefix extraction is the process of extracting prefixes of increasing lengths using substr.
- Divisibility Check: After each prefix has been transformed back into an integer, its length is compared to its divisibility.
- Result: The number is polydivisible if every prefix satisfies the divisibility criterion.
Conclusion:
In conclusion, polydivisible numbers are an interesting mathematical concept that combines knowledge of number structure and divisibility. These numbers present a unique challenge for mathematical analysis and programming, providing an interesting chance to study prefix-based divisibility laws. By iterating through a number's prefixes and verifying the divisibility requirements, one can easily determine a number's polydivisibility using C++. In addition to showing how programming can be used to solve mathematical problems, this concept broadens our understanding of number properties and divisibility restrictions. Researching such topics fosters the development of algorithmic problem-solving skills and logical reasoning.