# 110706 Smith Numbers

import java.util.*;
import static java.lang.Math.*;

public class Main {
private static List<Long> s_Primes;

static
{
long limit = (long)sqrt(2000000000L) + 1;
for (long i = 3L; i <= limit; i += 2L)
{
long mid = (long)sqrt(i);
Iterator<Long> iter = s_Primes.iterator();

boolean isPrime = true;
while (iter.hasNext())
{
long x = iter.next();
if (x > mid)
break;

if ((i % x) == 0)
{
isPrime = false;
break;
}
}

if (isPrime)
}
}

private static List<Long> GetAllPrimeElements(long number)
{
Iterator<Long> iter = s_Primes.iterator();
boolean canTryNext = true;
long mid = (long)sqrt(number);
long i = 0;
while (number > 1)
{
if (canTryNext)
{
if (!iter.hasNext())
{
return result;
}

i = iter.next();
if (i > mid)
{
return result;
}
}

if ((number % i) == 0)
{
canTryNext = false;
number /= i;
mid = (long)sqrt(number);
}
else
canTryNext = true;
}

return result;
}

private static long GetSum(long number)
{
long sum = 0;
while (number > 0)
{
sum += number % 10;
number /= 10;
}
return sum;
}

private static long GetSum(List<Long> elements)
{
if ((elements == null) ||
(elements.size() <= 1))
return -1;

long sum = 0;
for (long i : elements)
sum += GetSum(i);

return sum;
}

private static boolean IsSmith(long number)
{
if (number <= 3)
return false;

List<Long> elements = GetAllPrimeElements(number);

long numberSum = GetSum(number);
long elementsSum = GetSum(elements);

return (numberSum == elementsSum);
}

private static void Handle(long n)
{
long number = n + 1;
while (!IsSmith(number))
++number;
System.out.println(number);
}

public static void main(String[] args)
{
Scanner inScanner = new Scanner(System.in);
long cnt = inScanner.nextLong();
for (int i = 1; i <= cnt; ++i)
{
long n = inScanner.nextLong();
if (n <= 3L)
System.out.println(4);
else
Handle(n);
}
}

}


• 本文已收录于以下专栏：

举报原因： 您举报文章：110706 Smith Numbers 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)