aboutsummaryrefslogtreecommitdiff
path: root/challenge-009/zapwai/c/ch-1.c
blob: f5cd77486fd5ecc0452618d16d251e1dc875e972 (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
38
39
40
41
#include <stdio.h>
#include <stdbool.h>

bool has_distinct_digits(int num) {
  int x = num;
  int digit[50] = {0};
  int num_length = 0;
  digit[0] = x % 10;
  do {
    num_length++;
    x /= 10;
    digit[num_length] = x % 10;
  } while (x > 0);
  int freq[10] = {0};
  for (int i = 0; i < num_length; i++)
    freq[digit[i]]++;
  for (int i = 0; i < num_length; i++)
    if (freq[digit[i]] > 1)
      return false;
  return true;
}

int numlength(int num) {
  int cnt = 0;
  do {
    num /= 10;
    cnt++;
  } while (num > 0);
  return cnt;
}

int main() {
  for (int i = 0; i < 150; i++) {
    int sqnum = i*i;
    int num_length = 5;
    if (has_distinct_digits(sqnum) && (numlength(sqnum) == 5)) {
      printf("%d -> %d\n", i, sqnum);
      break;
    }
  }
}