primes

    -7熱度

    1回答

    我一直在努力解決這個問題http://www.spoj.com/problems/PRIME1/,如果任何人都可以幫我找到我的代碼中的錯誤。我使用了eratosthenes的分段篩,我也瀏覽了大量的在線資源,但不知何故,我得到了關於spoj的運行時錯誤。 感謝 #include <iostream> #include <cstdio> #include <algorithm>

    3熱度

    2回答

    作爲Python的第一個練習,我試圖編寫一個使用循環來查找素數的程序。一切工作與for循環,所以我想使用一個while循環。這有效,但程序返回一些不正確的數字。 import math # looking for all primes below this number max_num = int(input("max number?: ")) primes = [2] # start w

    4熱度

    2回答

    我對Mathematica很陌生,我試圖找到只能使用數字0,1,2和3寫入的大素數,而且這些數字中的一半以上必須是0 。例如100003就符合要求。 我正在考慮如果使用Prime [n]函數,如果「If」語句,但我想知道是否有更有效的方法來解決這個問題。提前致謝。

    2熱度

    2回答

    的篩非詳盡的模式我想使用埃拉托色尼的篩的這段代碼從該頁面:http://en.literateprograms.org/Sieve_of_Eratosthenes_(Haskell)#chunk DEF:primes_naive 只有一點修改,所以它只顯示了素數高達數: primes :: Integral a => a -> [a] primes m = sieve [2..m] w

    1熱度

    1回答

    我想產生這樣的素數: generatePrimeNumbersBelowN = (n) -> for i in [2..n-1] isPrime = true for j in [2..i-1] isPrime = false if i % j == 0 break if not isPrime console.log(i, "is

    -1熱度

    1回答

    您好,我一直在試圖回答一個問題,該問題涉及檢查數字是否是質數。我想出了下面這段代碼: isitprime :: Int->Bool isitprime n | n<=1 = False | otherwise = isitprime2 n (n-1) isitprime2 :: Int->Int->Bool isitprime2 x y | y > 1 &&

    0熱度

    1回答

    我試圖解決初學者的問題'找到一個給定的數字後的下一個素數'。我在網上看到了這個代碼,它完美的工作,但我似乎無法理解爲什麼,在findNextPrime方法中,我們需要找到'num',sqt的平方根,並在for循環中使用它。有人能向我解釋背後的數學和原因嗎? import java.util.Scanner; public class NextPrime { public stati

    -1熱度

    1回答

    我已經編寫代碼來生成範圍(使用Eratosthenes的修改過的篩)中的素數作爲競爭性編程。該代碼在我的Visual Studio上正常工作,但在網站上提供了SISGEV。我用這個, static bool *prime = new bool[1000000001]; 要聲明內存。無法理解SISGEV背後的原因。 以下是該功能,其參數爲the lower limit m和the upper l

    1熱度

    1回答

    我正在編寫一個查找素數的程序。 prime = [2] for k in range(3,100) if k%prime != 0 prime.append(k) print(prime) 當我運行程序出現錯誤: TypeError: unsupported operand type(s) for %: 'int' and 'list' 我想,當我試圖通過列表來劃

    3熱度

    1回答

    我是相當新的編程,我決定做一些練習來提高我的能力。我堅持做一個練習:「找出所有素數低於200萬的總和。」我的代碼太慢了。 起初,我試圖解決作爲一個正常的主要問題,並結束了與此: sum = 2 + 3 for i in range (5, 2000000, 2): for j in range (3, i, 2): if i%j == 0: break