Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@yopya

Project Euler 58「螺旋素数」

More than 3 years have passed since last update.

いつものようにジーッと眺めて法則性を探す。

Problem 58 「螺旋素数」

1から始めて, 以下のように反時計回りに数字を並べていくと, 辺の長さが7の渦巻きが形成される.

``````def hoge(num):
ratio = {True: 0, False: 1}
n, m = 1, 2
while True:
for _ in range(4):
n += m
ratio[is_prime(n)] += 1
if ratio[True] / sum(ratio.values()) * 100 < num:
break
m += 2
return int(n ** 0.5)

def is_prime(n):
if n < 2: return False
if n == 2: return True
if n % 2 == 0: return False
return all(n % i != 0 for i in range(3, int(n ** 0.5) + 1, 2))

print(hoge(10))
``````

法則性

1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away