2012-03-05 8 views
0

私は見つかった素数を数えて結果をWolfram Alphaの結果と比較しようとしました。PythonのSPOJ PRIME1に間違った答えがありました

エラーなく正常に動作しているようです。

私はこの解決策をSPOJに提出すると、「間違った答え」というエラーメッセージが表示されます。

私は最終的なプリントのend =を ''(空文字列)に変更しようとしましたが、依然として「間違った答え」があります。

シーブアルゴリズムや出力エラーに問題があるかどうかはわかりません。

**編集:ここでは、問題のリンクhttp://www.spoj.pl/problems/PRIME1/

は私PRIME1のソースコードで、誰かが私のせいを指摘することができると思います。どうもありがとう。

誰かがこのようなプログラムにテストを書く方法を教えてくれることを願っています。まだ学習していますが、プログラムに自動化されたテストを行う方法はわかりません。

def getPrimeInRange(minNum, maxNum): 
     #just a variation with skipping step of the Sieve of E's 
     processingRange = list(range(minNum, maxNum+1)) 
     #prefix, due to 1 is not a prime 
     if minNum == 1: 
      processingRange[0] = 0 
     sqrtOfMaxNum = int(maxNum ** 0.5) + 1 
     primesUnderSqrt = list(range(sqrtOfMaxNum)) 
     #prefix, due to 1 is not a prime 
     primesUnderSqrt[1] = 0 

     #my strategy is flip all numbers that is not a prime to zero, which equals to False. 
     #for Sieve of E's, all the primes under sqrt of max num are the only needed numbers to sieve primes out. 
     #so here is a smaller Sieve of E's for numbers under sqrt 
     for n in primesUnderSqrt: 
      if n: #which equals to "if n != 0" 
       nowIndex = n + n 
       while True: 
        try: 
         primesUnderSqrt[nowIndex] = 0 
         nowIndex += n 
        except IndexError: 
         break 

     #full aspect sieve 
     for n in primesUnderSqrt: 
      if n: 
       #for easier flip processing, I need the offset number for the flipping. 
       nMultipleMostCloseToMin = n * (minNum // n) 
       if nMultipleMostCloseToMin == minNum: 
        nowIndex = 0 
       elif sqrtOfMaxNum <= minNum: 
        nowIndex = nMultipleMostCloseToMin + n - minNum 
       elif sqrtOfMaxNum > minNum: 
        nowIndex = nMultipleMostCloseToMin + n - minNum + n 

       #happy flippin' 
       while True: 
        try: 
         processingRange[nowIndex] = 0 
         nowIndex += n 
        except IndexError: 
         break 

     return processingRange 

    def main(): 
     todoTimes = int(input()) 
     todoNums = list(range(todoTimes)) 
     stringOutput = '' 
     for i in range(todoTimes): 
      todoNums[i] = input().split() 
      todoNums[i][0] = int(todoNums[i][0]) 
      todoNums[i][1] = int(todoNums[i][1]) 
     for [minNum, maxNum] in todoNums: 
      #countedNum = 0 #for algo debugging 
      for n in getPrimeInRange(minNum, maxNum): 
       if n: 
        stringOutput += str(n) 
        #countedNum += 1 #for algo debugging 
        stringOutput += '\n' 
      stringOutput += '\n' 
      #stringOutput += str(countedNum) #for algo debugging 
     stringOutput = stringOutput.rstrip('\n') 
     print(stringOutput) 


    ifMainSucceed = main() 

答えて

0

if nMultipleMostCloseToMin == minNum: 
    nowIndex = 0 
elif sqrtOfMaxNum <= minNum: 
    nowIndex = nMultipleMostCloseToMin + n - minNum 
elif sqrtOfMaxNum > minNum: 
    nowIndex = nMultipleMostCloseToMin + n - minNum + n 

が間違っている、あなたのロジックのこの部分。あなたのelif - 条件はあまり意味がありません。 nminNumの除数でない場合、minNum以上でnの最小倍数nMultipleMostCloseToMin + nであるにかかわらずかどうかsqrtOfMaxNumminNumかより大きい。あなたがここで意図した条件は、素数自体を横切らないように、n <= minNumでした。

+0

ありがとうございます!私の脳が捻られた、笑 – Shem

+0

elifの状態をn <= minNumに変更した後、2番目のelifをelseに変更した後も、間違った答えを得ました.... – Shem

+0

素数私はコードを変更した前と同じように見えますが、どこが間違っているのか分かりません。SPOJは私にチェックしても間違った答えを与えません.... – Shem

関連する問題