简体   繁体   English

[Python / Project Euler]问题41

[英][Python/Project Euler]Problem 41

I am trying to solve problem 41 , but i don't know why my code stops at 987654319: 我正在尝试解决问题41 ,但是我不知道为什么我的代码在987654319处停止:

def IsPandigital(No):
 a = sorted(str(No))
 for i in range(1,10):
  if a[i - 1] != str(i):
   return False
 return True
def IsPrime(No):
 i = 2
 while i < No:
  if No % i == 0:
   return False
  i += 1
 return True
i = 987654321
while i > 0:
 print i
 raw_input()
 if IsPrime(i) == True and IsPandigital(i) == True:
  print i
  break
 i -= 1
print i
print "EOP"
raw_input()

PS: I know that i should start from 799999999 because: PS:我知道我应该从799999999开始,因为:

GergS: Every 9 digit and 8 digit pandigital number is divisible by 3. GergS:每9位数和8位数的全数字可被3整除。

Your IsPrime function is very slow. 您的IsPrime功能非常慢。 It quickly calculated that 987654321 is dividable by 3 and 987654320 is dividable by 2. However, 987654319 is prime and it takes very very long to check it against all divisors so it seems as if it stopped. 它很快计算出987654321可被3整除,而987654320可被2整除。但是,987654319是质数,并且将其与所有除数比较都需要非常长的时间,因此好像停止了。

The problem requires more than just simple calculation, as you did. 正如您所做的那样,该问题不仅需要简单的计算。 Calculating prime numbers is a slow process, so you should optimize it, for example: 计算质数是一个缓慢的过程,因此您应该对其进行优化,例如:

  • Test IsPandigital before IsPrime , IsPandigital之前测试IsPrime
  • Even better, create a list of only pandigital numbers and make the prime test. 更好的是,创建仅泛指数字的列表并进行质数测试。 Hint: the pandigital numbers are: [987654321,987654312,987654231,987654213,...] 提示:掌上电脑号码是: [987654321,987654312,987654231,987654213,...]
  • You dont have to make loop while i < No - it is enough to go up to sqrt(No) . while i < No您不必进行循环-足以达到sqrt(No)
  • Numbers ending with digit 0, 2, 4, 5, 6 or 8 are never prime 以数字0、2、4、5、6或8结尾的数字绝不是质数
  • Another option might be to calculate all n-digit prime numbers and then pick the largest pandigital number. 另一个选择可能是计算所有n位素数,然后选择最大的泛数字。

Your other problems: 您的其他问题:

  • You are calculating the largest 9-digit pandigital prime. 您正在计算最大的9位全数字素数。 The problem says "the largest n-digit pandigital prime", so you should be able to calculate any length depending on a parameter 该问题显示为“最大n位全数字素数”,因此您应该能够根据参数计算任何长度
  • You should not start from 799999999, as you wrote. 您不应该像写的那样从799999999开始。 You should simply skip calculations for n=3, n=5, n=6, n=8 and n=9 because none of those is prime. 您应该只跳过对n = 3,n = 5,n = 6,n = 8和n = 9的计算,因为这些都不是质数。

The routine to find primes takes much longer than the pandigital, so switch the order that they are run. 查找素数的例程比泛指法花费的时间长得多,因此请切换它们的运行顺序。 So it should be: 因此应该是:

if IsPandigital(i) == True and IsPrime(i) == True:

Try using itertools.permutations to generate all pan-digital numbers then checked if they were prime. 尝试使用itertools.permutations生成所有泛数字,然后检查它们是否为质数。 It generates all the permutations of the string passed. 它生成传递的字符串的所有排列。

Eg 例如

itertools.permutations('9876543210')

You can narrow your search as explain above, 9876543210 is just an example. 您可以按照上面的说明缩小搜索范围,其中9876543210只是一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM