[英]Largest Prime Number with big numbers
I did a Project Euler question - here我做了一个 Project Euler 问题 -这里
I am at the end of my wits.我已经无计可施了。 I am getting wrong solution.
我得到了错误的解决方案。 I am perplexed as to "why" particularly this code is not working.
我对“为什么”感到困惑,尤其是这段代码不起作用。
This was my generated answer.这是我生成的答案。
long long x; // The max prime factor
long long n; // The number to be factored
while (n % 2 == 0)
{
n /= 2;
}
x = 2;
while (n % 3 == 0)
{
n /= 3;
}
x = 3;
for (int i = 5; i <= sqrt(n); i += 2)
{
while (n % i == 0)
{
n /= i;
}
x = i;
}
std::cout << x;
n = 600851475143 answer = 6857>! I am getting x = 1471
At least this for loop至少这个for循环
for (int i = 5; i <= sqrt(n); i += 2)
{
while (n % i == 0)
{
n /= i;
}
x = i;
}
is wrong, The variable x
gets the value of the last i that is less than or equal to sqrt( n )
.是错误的,变量
x
获取小于或等于sqrt( n )
的最后一个 i 的值。
Consider for example n equal to 22. After dividing it by 2 you will get n equal to 11.考虑例如 n 等于 22。除以 2 后,您将得到 n 等于 11。
After this code snippet在这段代码之后
while (n % 3 == 0)
{
n /= 3;
}
x = 3;
x
will be equal to 3
and the for loop will be skipped due to its condition that evaluates to false. x
将等于3
并且 for 循环将由于其计算结果为 false 的条件而被跳过。
The code can look for example the following way代码可以通过以下方式查找示例
long long int n = 600851475143;
long long int prime_factor = 0;
if (n % 2 == 0)
{
prime_factor = 2;
while (n % 2 == 0 ) n /= 2;
}
for (long long int i = 3; i <= n / i; i += 2)
{
if (n % i == 0)
{
prime_factor = i;
while (n % i == 0) n /= i;
}
}
if (n != 1) prime_factor = n;
std::cout << "prime factor = " << prime_factor << '\n';
Also you should use the unsigned type unsigned long long int
instead of the signed type long long int
.此外,您应该使用无符号类型
unsigned long long int
而不是有符号类型long long int
。 Otherwise you will need to write code that will take into account the sign of the source number.否则,您将需要编写考虑源编号符号的代码。
You could write a separate function as shown in the demonstration program below您可以编写一个单独的 function 如下面的演示程序所示
#include <iostream>
unsigned long long max_prime_factor( unsigned long long int n )
{
unsigned long long int prime_factor = 0;
if (not ( n < 2 ))
{
if (n % 2 == 0)
{
prime_factor = 2;
while (n % 2 == 0) n /= 2;
}
for (unsigned long long int i = 3; i <= n / i; i += 2)
{
if (n % i == 0)
{
prime_factor = i;
while (n % i == 0) n /= i;
}
}
}
return n < 2 ? prime_factor : n;
}
int main()
{
for (unsigned int i = 0; i < 20; i++)
{
std::cout << i << " -> " << max_prime_factor( i ) << '\n';
}
}
The program output is程序 output 是
0 -> 0
1 -> 0
2 -> 2
3 -> 3
4 -> 2
5 -> 5
6 -> 3
7 -> 7
8 -> 2
9 -> 3
10 -> 5
11 -> 11
12 -> 3
13 -> 13
14 -> 7
15 -> 5
16 -> 2
17 -> 17
18 -> 3
19 -> 19
int main() {
long long x = 0; // The max prime factor
long long n = 600851475143; // The number to be factored
long long a = sqrt(n);
while (n % 2 == 0) {
n /= 2;
}
x = 2;
while (n % 3 == 0) {
n /= 3;
}
x = 3;
for (int i = 5; i <= a; i += 2) {
while (n % i == 0) {
n /= i;
x = i;
}
}
std::cout << x << std::endl;
}
Here it works, the sqrt(n) stoped the for loop too soon so i put it in a long long, and the x = i;在这里它起作用了,sqrt(n) 太快停止了 for 循环,所以我把它放在 long long 中,而 x = i; was in the wrong place
在错误的地方
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.