aboutsummaryrefslogtreecommitdiff
path: root/challenge-009/paulo-custodio/cpp/ch-1.cpp
blob: a647b3cfd7831127382815f1e88544191f529030 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
Challenge 009

Challenge #1
Write a script that finds the first square number that has at least 5 distinct
digits.This was proposed by Laurent Rosenfeld.
*/

#include <iostream>
using namespace std;

int num_diff_digits(int n) {
    bool digits[10] = {0};
    int count = 0;
    while (n > 0) {
        int digit = n % 10;
        n /= 10;
        if (!digits[digit]) {
            digits[digit] = true;
            count++;
        }
    }
    return count;
}

int main(int argc, char* argv[]) {
    int num = 0;
    if (argc == 2)
        num = atoi(argv[1]);
    if (num == 0)
        num = 5;

    int n = 1;
    while (num_diff_digits(n * n) < num)
        n++;
    cout << n * n << endl;
}