-
Notifications
You must be signed in to change notification settings - Fork 0
/
10042 - Smith Numbers.cpp
101 lines (93 loc) · 1.45 KB
/
10042 - Smith Numbers.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <bits/stdc++.h>
#define maxN 100001
using namespace std;
bool isprime[maxN];
int primes[9592];
int len;
void sieve()
{
memset(isprime, true, sizeof(isprime));
isprime[0] = isprime[1] = false;
for(int i = 4; i < maxN; i += 2)
{
isprime[i] = false;
}
int s = sqrt(maxN);
for(int i = 3; i < s; i += 2)
{
if(isprime[i])
{
for(int j = i*i; j < maxN; j+= 2*i)
{
isprime[j] = false;
}
}
}
len = -1;
primes[++len] = 2;
for(int i = 3; i < maxN; i += 2)
{
if(isprime[i])
{
primes[++len] = i;
}
}
}
bool itsprime(unsigned long long N)
{
if(N < maxN) return isprime[N];
int s = sqrt(N);
for(int i = 0; i <= len && primes[i] <= s; i++)
{
if(N % primes[i] == 0) return false;
}
return true;
}
int sumdigits(unsigned long long N)
{
int sum = 0;
while(N)
{
sum += N%10;
N /= 10;
}
return sum;
}
unsigned long long smith(int N)
{
unsigned long long smith = N + 1;
while(true)
{
if(!itsprime(smith))
{
int sumoffactors = 0;
unsigned long long int s = sqrt(smith), n = smith;
for(int i = 0; i <= len && primes[i] <= s; i++)
{
while(n%primes[i] == 0)
{
sumoffactors += sumdigits(primes[i]);
n /= primes[i];
}
}
if(n > 1) sumoffactors += sumdigits(n);
if(sumoffactors == sumdigits(smith))
{
return smith;
}
}
smith++;
}
}
int main()
{
sieve();
int cases, N;
scanf("%d", &cases);
while(cases--)
{
scanf("%d", &N);
printf("%llu\n", smith(N));
}
return 0;
}