我在这里找不到逻辑中断但是在运行时我获得了3125的输出,因为它应该是最大的素数因子。 3125显然不是素数,也不是最大的非素因子。这不是设计成找到素因子的有效方法,只是试图解决这个问题。
long long modulo= 99999;
long long LrgPrimeFactor=0;
long long currentfactor=0;
long long tempmodulo=0;
int break1 =0;
int break2 =0;
while (modulo>0&&break2==0)
{
if ((100000%modulo) ==0)
{
currentfactor=modulo;
for (tempmodulo=currentfactor-1;tempmodulo>0;tempmodulo--)
{
if (currentfactor%tempmodulo==0)
{
LrgPFactor=currentfactor;
break1=1;
break;
}
else if(break1==1)
{
break2=1;
break;
}
}
}
else
{
modulo-=2;
}
}
答案 0 :(得分:2)
如果我要使用你的方法,那么我将如何对其进行编码,这种方法的开始时间比你想要的数字少一些。我确认这可以在CodePad.org中使用。
#include <stdio.h>
int main()
{
int numberToFactor = 100000;
int largestPrimeFactorCandidate = numberToFactor;
int testDivisor = 0;
int stillLookingForPrime = 1;
int stillCheckingPrimeness = 1;
while (largestPrimeFactorCandidate >= 1 && stillLookingForPrime)
{
if (numberToFactor != largestPrimeFactorCandidate)
{
// First we need a factor of the number. The first time
// around, though, we skip this, because the number itself
// could be prime.
while (numberToFactor % largestPrimeFactorCandidate != 0)
{
largestPrimeFactorCandidate--;
}
}
// Now that we have a factor, we check to see if it's prime.
testDivisor = largestPrimeFactorCandidate - 1;
stillCheckingPrimeness = 1;
while (stillCheckingPrimeness)
{
if (largestPrimeFactorCandidate % testDivisor != 0)
{
testDivisor--;
}
else
{
stillCheckingPrimeness = 0;
}
}
if (testDivisor != 1)
{
// It's not prime, so we keep looking.
largestPrimeFactorCandidate--;
}
else
{
// Largest factor besides itself is 1, so it's prime.
stillLookingForPrime = 0;
}
}
printf("%d", largestPrimeFactorCandidate);
// At this point, largestPrimeFactorCandidate is 5, which is your answer.
return 0;
}
答案 1 :(得分:2)
我对您的代码进行了一些更改,现在它给出了正确的结果。希望这会有所帮助。
long long modulo = 99999;
long long lrgPFactor = 0;
long long currentfactor = 0;
long long tempmodulo = 0;
while (modulo > 0)
{
if (100000 % modulo == 0)
{
for (tempmodulo = modulo - 1; tempmodulo > 1; --tempmodulo)
{
if (modulo % tempmodulo == 0)
break;
}
if (tempmodulo == 1)
{
lrgPFactor = modulo;
break;
}
}
modulo -= 2;
}
答案 2 :(得分:2)
我认为你应该从2开始向上工作,而不是从N向下工作。此代码实现了评注中概述的算法(评注中粗略概述):
#include <stdio.h>
#include <inttypes.h>
typedef unsigned long long Number;
#define PRI_uNumber "llu"
static Number largest_prime_factor(Number n)
{
if (n <= 1)
return n;
while (n % 2 == 0)
{
n /= 2;
if (n == 1)
return 2;
}
/* When f is composite, its factors have already been eliminated from n */
for (Number f = 3; f < n; f += 2)
{
while (n % f == 0)
{
n /= f;
if (n == 1)
return f;
}
}
return n;
}
int main(void)
{
Number numbers[] =
{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
15, 19, 21, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
100, 101, 102, 103, 100000, 100001, 100003, 100007, 100009,
};
for (size_t i = 0; i < sizeof(numbers) / sizeof(numbers[0]); i++)
printf("%6" PRI_uNumber ": LPF = %" PRI_uNumber "\n",
numbers[i], largest_prime_factor(numbers[i]));
return 0;
}
输出:
1: LPF = 1
2: LPF = 2
3: LPF = 3
4: LPF = 2
5: LPF = 5
6: LPF = 3
7: LPF = 7
8: LPF = 2
9: LPF = 3
10: LPF = 5
11: LPF = 11
12: LPF = 3
13: LPF = 13
15: LPF = 5
19: LPF = 19
21: LPF = 7
90: LPF = 5
91: LPF = 13
92: LPF = 23
93: LPF = 31
94: LPF = 47
95: LPF = 19
96: LPF = 3
97: LPF = 97
98: LPF = 7
99: LPF = 11
100: LPF = 5
101: LPF = 101
102: LPF = 17
103: LPF = 103
100000: LPF = 5
100001: LPF = 9091
100003: LPF = 100003
100007: LPF = 1031
100009: LPF = 157
请注意PRI_uNumber
小心地避免为<inttypes.h>
标题保留名称空间:
7.31.5整数类型
的格式转换<inttypes.h>
¶1以
PRI
或SCN
开头且以小写字母或X开头的宏可能是 添加到<inttypes.h>
标题中定义的宏。
下划线表示它是安全的。