File size: 1,065 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
  >>~~ UVa Online Judge ACM Problem Solution ~~<<

  ID: 10042
  Name: Smith Numbers
  Problem: https://onlinejudge.org/external/100/10042.pdf
  Language: C++

  Author: Arash Shakery
  Email: arash.shakery@gmail.com
*/

#include <math.h>
#include <stdio.h>
#include <iostream>
using namespace std;

int dsum(int x) {
    if (x<10)
        return x;
    else if (x<100)
        return (x/10) + (x%10);

    int sum = 0;
    for(; x; x/=10)
        sum += x%10;
    return sum;
}

bool isSmith(int x) {
    int n = x;
    int ds = dsum(x);
    int sq = sqrt(double(x));
    for (int i=2; i<=sq; i++)
        if (x%i == 0) {
            int ds0 = dsum(i);
            do {
                ds -= ds0;
                if (ds<0) return 0;
                x /= i;
            } while (x%i == 0);
            sq = sqrt(double(x));
        }

    if (x == n) return 0;
    if (x > 1) ds -= dsum(x);
    return !ds;
}


int main(){
    int T, n, x;
    cin>>T;
    while (T--) {
        cin >> n;
        for (x=n+1; !isSmith(x); x++);
        cout << x << endl;
    }
}