Created
February 2, 2018 02:03
-
-
Save junichiro/2bac573d485f5af3092b6550d1dce98c to your computer and use it in GitHub Desktop.
会社で少し盛り上がった Project Euler をやってみる 010 ref: https://qiita.com/junichiro/items/8503e979d0314e7f2486
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10以下の素数の和は 2 + 3 + 5 + 7 = 17 である. | |
200万以下の全ての素数の和を求めよ. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
class Problem10: | |
PRIME = [] | |
def main(self, limit): | |
for i in range(2, limit): | |
if self.isPrime(i): | |
self.PRIME.append(i) | |
print(sum(self.PRIME)) | |
def isPrime(self, num): | |
for i in self.PRIME: | |
if i ** 2 > num: | |
return True | |
if num % i == 0: | |
return False | |
return True | |
if __name__ == '__main__': | |
limit = 2000000 | |
if len(sys.argv) == 2: | |
limit = sys.argv[1] | |
p = Problem10() | |
p.main(int(limit)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment