---
title: "哪两个素数相加等于998"
author: "saber7"
date: "2010-12-01T22:43:35Z"
tags: ["Python", "Ruby"]
categories: ["编程语言"]
slug: "998"
draft: false
lastmod: "2010-12-01T22:43:35Z"
id: 830936
url: "https://saber7.iteye.com/blog/830936"
---

朋友在学ruby，刚才碰到这个问题“哪两个素数相加等于998”，刚好我在学python，就试着用python写了一下，其实很简单。

 

```
def isPrime(d):
    for i in range(2,d-1):
        if d%i == 0:
            return False
        else:
            pass
    return True

for i in xrange(2,499):
    if isPrime(i) and isPrime(998-i):
        print i,998 - i
```

 

开始我求出的结果是：

 

7 991  
31 967  
61 937  
79 919  
139 859  
211 787  
229 769  
241 757  
271 727  
307 691  
337 661  
367 631  
379 619  
397 601  
421 577  
457 541  
499 499  
541 457  
577 421  
601 397  
619 379  
631 367  
661 337  
691 307  
727 271  
757 241  
769 229  
787 211  
859 139  
919 79  
937 61  
967 31  
991 7

 

但是朋友说从499 499开始后面就重复了，嗯，我的理解是 a(7) +b(991) = 998 跟 a(991) +b(7) = 998 的概念是不一样的，不过既然说重复了，就写成2到499就行了。

```
for i in xrange(2,499):
```