两部分问题......
1)试图确定600851475143的最大素数因子,发现这个程序似乎在线工作,问题是我很难弄清楚它是如何工作的(我理解程序正在做什么的基础)...另外,如果您能够了解一些您可能知道找到素数的方法(可能没有测试每个数字)以及您的方法是如何工作的.
我在网上找到的主要因素代码
n = 600851475143
i = 2
while i * i < n:
while n % i == 0:
n = n / i
i = i + 1
print (n)
#takes about ~0.01secs
Run Code Online (Sandbox Code Playgroud)
2)为什么代码比这段代码快得多(代码只是测试速度而没有其他真正的用途)
i = 1
while i < 100:
i += 1
#takes about ~3secs
Run Code Online (Sandbox Code Playgroud) 我正在尝试通过Project Euler工作,我在问题03上遇到障碍.我有一个适用于较小数字的算法,但问题3使用非常非常大的数字.
问题03: 13195的主要因素是5,7,13和29. 600851475143中最大的素数因子是什么?
这是我在C#中的解决方案,它一直在运行,我认为接近一个小时.我不是在寻找答案,因为我确实想自己解决这个问题.主要是寻求一些帮助.
static void Main(string[] args) {
const long n = 600851475143;
//const long n = 13195;
long count, half, largestPrime = 0;
bool IsAPrime;
half = n / 2;
for (long i = half; i > 1 && largestPrime == 0; i--) {
if (n % i == 0) { // these are factors of n
count = 1;
IsAPrime = true;
while (++count < i && IsAPrime) {
if (i % count == 0) …
Run Code Online (Sandbox Code Playgroud) 13195的主要因素是5,7,13和29. 600851475143中最大的素数是多少?
我以自己的方式在Project Euler上解决了这个问题,这很慢,然后我在某人的github帐户上找到了这个解决方案.我无法弄清楚它为何起作用.为什么删除了许多因素,等于索引?任何见解?
def Euler3(n=600851475143):
for i in range(2,100000):
while n % i == 0:
n //= i
if n == 1 or n == i:
return i
Run Code Online (Sandbox Code Playgroud) 我正在尝试用Python解决Project Euler问题3:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
Run Code Online (Sandbox Code Playgroud)
我知道我的程序效率低且超大,但我只是想知道它为什么不起作用?这是代码:
def check(z):
# checks if the given integer is prime
for i in range(2, z):
if z % i == 0:
return False
break
i = i+1
return True
def primegen(y):
# generates a list of prime integers in the given range
tab = []
while y >= 2:
if check(y) == True:
tab.append(y)
i …
Run Code Online (Sandbox Code Playgroud)