/* file : specialSquares.c */ /* author : Oscar MirĂ³ (o.miro.i.lopez.feliu@student.rug.nl) */ /* Description: * Find the number of special squares between two numbers. Special square is defined as * a perfect square with no repeated digits. */ #include // func to check if a number has no repeating digits int notRepeated (long input) { // array for every number 0-9 int frequency[10] = {0}; // removes the last number, adds 1 to its index in the array while (input > 0) { frequency[input % 10]++; input /= 10; } // check if any index has more than a 1, which would mean a number is repeated for (int i = 0; i < 10; i++) { if (frequency[i] > 1) { return 0; } } return 1; } int main (int argc, char *argv[]) { // declaring, long because we may be dealing with very large numbers long min, max, current = 0, aux = 0, count = 0; // scanning scanf("%ld %ld", &min, &max); // works because: 1 (square), 1 + (1+2) == 4 (square), 4 + (1+2+2) = 9 (square) // 9 + (1+2+2+2) == 16 (square) while (current < max) { current += aux*2 +1; aux++; // if the new number exceeds the upper bound, it stops if (current > max) { break; } // if the number is above the lower bound and has no repeating digits, adds to count else if (current >= min && notRepeated(current)) { count++; } } // prints count printf("%ld\n", count); }