This code checks if number is pandigital (from 1 to 9). I have a few question to ask:
1. This code relies on theory that:
*if number digits product equals factorial of number length and
*if number digits sum equals summation of consecutive integers to length
then the number is pandigital.
Is there any proof for this?
Also, is it possible to somehow change the code so it considers number a pandigital even if one of it's digits is 0?
#include <iostream>
usingnamespace std;
bool check(int number, int length) {
int i=0;
int grid[length];
while (number>0) {
grid[i]=number%10;
number=number/10;
i++;
}
int summation=0, factorial=1;
int summation1=0, factorial1=1;
for (int i=0; i<=length-1; i++) {
factorial=factorial*grid[i];//counts product of digits and factorial of length
factorial1=factorial1*(i+1);
summation=summation+grid[i];//counts sum of digits and summation of integers to length
summation1=summation1+(i+1);
}
if ((factorial==factorial1) and (summation==summation1) and (factorial!=0)) returntrue;
elsereturnfalse;
}
int main()
{
for (int i=1; i<=999999999; i++) {
// counts how many digits there is
int temp=i;
int a=0;
while (temp>0) {
temp=temp/10;
a++;
}
//checks if number is pandigital
if (check(i, a)==true) {cout << i << endl;}
}
}