[英]Better Solution to Find prime numbers in interval [1000,9999] where the sum of first and second
I want to Find prime numbers in interval [1000,9999] where the sum of first and second digits is equal to sum of third and fourth digits of the number. 我想在间隔[1000,9999]中查找质数,其中第一和第二位数字的总和等于该数字的第三和第四位数字的总和。
For example 3517 is prime number and 3 + 5 = 1 + 7 例如3517是质数,3 + 5 = 1 + 7
My solution is the following: 我的解决方案如下:
for (int i = 1001; i <= 9999; i += 2)
{
if (NumberOfDivisorsOf(i) == 2)
{
string x = i.ToString();
int n1 = Convert.ToInt32(x[0]);
int n2 = Convert.ToInt32(x[1]);
int n3 = Convert.ToInt32(x[2]);
int n4 = Convert.ToInt32(x[3]);
if ((n1 + n2) == (n3 + n4))
Console.WriteLine(i);
}
}
NumberOfDivisorsOf method looks like this: NumberOfDivisorsOf方法如下所示:
static int NumberOfDivisorsOf(int a)
{
int count = 2;
int i = 2;
for (; i < a/i ; i++)
{
if (a / i * i == a)
count += 2;
}
if (i * i == a)
count++;
return count;
}
I think NumberOfDivisorsOf method is fine and improvement needs to the solution itself. 我认为NumberOfDivisorsOf方法很好,并且解决方案本身需要改进。
I don't want to use LINQ. 我不想使用LINQ。 I want to get them with simple steps..
我想通过简单的步骤来获得它们。
EDIT: 编辑:
Depending On OMG answer I improve code as shown below: 根据OMG的答案,我改进了代码,如下所示:
for (int i = 1001; i <= 9999; i += 2)
{
if (Isprime(i))
{
int n1 = i / 1000;
int n2 = (i % 1000) / 100;
int n3 = (i % 100) / 10;
int n4 = i % 10;
if ((n1 + n2) == (n3 + n4))
{
Console.WriteLine(i);
}
}
}
I changed NumberOfDivisorOf method to IsPrime method: 我将NumberOfDivisorOf方法更改为IsPrime方法:
static bool Isprime(int n)
{
if (n == 2)
return true;
if (n == 3)
return true;
if ((n % 2) == 0)
return false;
if (n % 3 == 0)
return false;
int i = 5;
int w = 2;
while (i * i <= n)
{
if (n % i == 0)
return false;
i += w;
w = 6 - w;
}
return true;
}
EDIT: 编辑:
Depending on Siye answer I changed my code as shown below (it made executions speed 3x faster) 根据Siye的答案,我如下所示更改了代码(它使执行速度提高了3倍)
for (int i = 1001; i <= 9999; i += 2)
{
int n1 = i / 1000;
int n2 = (i % 1000) / 100;
int n3 = (i % 100) / 10;
int n4 = i % 10;
if ((n1 + n2) == (n3 + n4))
{
if (Isprime(i))
{
Console.WriteLine(i);
}
}
}
To find the prime number efficiently, there exist some good solutions(such as this ). 为了有效地找到素数,存在一些好的解决方案(例如this )。 To better your approach to find the sum, it would be better using
%
or using .ToString()
just once in your code as the overhead of it can effect on the performance in high scale. 为了更好地找到总和,最好在代码中只使用
%
或一次使用.ToString()
,因为这样做的开销会影响大规模性能。
To use %
you can find them by: 要使用
%
您可以通过以下方式找到它们:
int n4 = i % 10;
int n3 = (i % 100) / 10;
int n2 = (i % 1000) / 100;
int n1 = (i % 10000) / 1000;
Also, instead of NumberOfDivisorsOf
it would be better to use is_prime
by using break
in the loop when you found the number is prime. 另外,当您发现数字为素数时,最好使用
is_prime
,而不是NumberOfDivisorsOf
,通过在循环中使用break
来使用is_prime
。
for (int i = 1001; i <= 9999; i += 2)
{
int n1 = i.ToString()[0];
int n2 = i.ToString()[1];
int n3 = i.ToString()[2];
int n4 = i.ToString()[3];
if ((n1 + n2) == (n3 + n4)) {
if (NumberOfDivisorsOf(i) == 2)
Console.WriteLine(i);
i = (i / 10 + 1) * 10; // A little improvement
continue;
}
if ((n1 + n2) > (n3 + n4)) {
i = (i / 10 + 1) * 10;
continue;
}
}
This may make a little improvment. 这可能会有所改进。
By the way, Sieve of Eratosthenes may improve the performance of method NumberOfDivisorsOf. 顺便说一下, Eratosthenes的筛网可以提高NumberOfDivisorsOf方法的性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.