반응형
[EP 049] 네자리 소수가 만드는 등차수열 에서 나온 문제입니다.
접근하기 쉽지 않은 문제였지만, 해답을 보면서 큰 도움이 되었습니다.
직접 실행해본 결과 조건 하나가 맞지 않는다고 판단해서 다시 적용해 보았습니다.
코드가 좀 지저분해진거 같은데, 잘못된 점 찾아주시면 정말 감사하겠습니다.
접근하기 쉽지 않은 문제였지만, 해답을 보면서 큰 도움이 되었습니다.
직접 실행해본 결과 조건 하나가 맞지 않는다고 판단해서 다시 적용해 보았습니다.
코드가 좀 지저분해진거 같은데, 잘못된 점 찾아주시면 정말 감사하겠습니다.
if set(str(p)) == set(str(q)): # this condition isn't quite exact
def process(num):
primes = [2, 3]
cnt = 4
result = []
while cnt < num:
isPrime = True
for prime in primes:
if prime * prime > cnt:
break
if cnt % prime == 0:
isPrime = False
if isPrime:
primes.append(cnt)
cnt += 1
for p1 in primes :
if p1 < 1000:
continue
for p2 in primes:
if p2 < 1000:
continue
if p1 <= p2:
break
# sort two primes.
l1 = list(str(p1)); l1.sort()
l2 = list(str(p2)); l2.sort()
if l1 == l2: # compare them.
p3 = (p1+p2)/2
l3 = list(str(p3))
l3.sort()
if p3 in primes and l1 == l3:
ln = list(str(p3))
ln.sort()
p = int(''.join(map(str,ln)))
if p in result and ln == l1:
continue
print '%9d %9d %9d' %( p1,p2, p3)
result.append(int(''.join(map(str,l1))))
print result
def main():
process(9999)
if __name__ == '__main__':
main()
'Develop' 카테고리의 다른 글
| [Problem 8] 연속되는 5자리의 곱중 최대값 (0) | 2011.01.09 |
|---|---|
| [Problem 6] 합의 제곱과 제곱의 합 차이 (0) | 2011.01.08 |
| .htaccess 설정파일. (0) | 2008.01.27 |
| CVS 설정하기 (0) | 2008.01.05 |
| Ubuntu LAMP Server Guide - Configure Apache, mySQL, and cgi-bin (0) | 2007.12.20 |