숫자 1부터 시작해서 우측으로부터 시계방향으로 감아 5×5 행렬을 만들면 아래와 같이 됩니다.
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
여기서 대각선상의 숫자를 모두 더한 값은 101 입니다.
같은 방식으로 1001×1001 행렬을 만들었을 때, 대각선상의 숫자를 더하면 얼마가 됩니까?
예전에 28번문제 풀었던 풀이를 재사용...
막간을 이용한 한문제 풀이 ㅋㅋ
This file contains hidden or 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
def isPrime(n): | |
n = abs(int(n)) | |
if n < 2: | |
return False | |
if n == 2: | |
return True | |
if not n & 1: | |
return False | |
for x in range(3, int(n**0.5)+1, 2): | |
if n % x == 0: | |
return False | |
return True | |
total=1 | |
rows=1 | |
sumPrime=0 | |
n=3 | |
while True: | |
up_r = n**2 | |
up_l = up_r - (n-1) | |
dn_l = up_l - (n-1) | |
dn_r = dn_l - (n-1) | |
total += 4 | |
rows+=2 | |
if isPrime(up_r): | |
sumPrime+=1 | |
if isPrime(up_l): | |
sumPrime+=1 | |
if isPrime(dn_l): | |
sumPrime+=1 | |
if isPrime(dn_r): | |
sumPrime+=1 | |
#print(total, sumPrime) | |
if (sumPrime / total)*100 < 10: | |
print(rows) | |
break | |
n+=2 |