algorithm/problem solving

acmicpc.net 소수 관련. 1978,2960,4948,6588,3896,11502

qkqhxla1 2016. 9. 22. 12:41

https://www.acmicpc.net/problem/1978

# -*- encoding: cp949 -*-
def prime_number(max_n):
    p = [0 for i in xrange(max_n+1)]
    for i in xrange(2,max_n+1):
        for j in xrange(i,max_n+1,i):
            if j!=i:
                p[j] = 1
    prime = [i for i in xrange(2,max_n+1) if p[i]==0]
    return prime

input()
n = map(int,raw_input().split())
print len([i for i in n if i in set(prime_number(1000))])

https://www.acmicpc.net/problem/2960

# -*- encoding: cp949 -*-
def prime_number(max_n, k):
    p = [0 for i in xrange(max_n+1)]
    c = 0
    for i in xrange(2,max_n+1):
        for j in xrange(i,max_n+1,i):
            if p[j]==0:
                #print j
                p[j] = 1
                c += 1
                if c==k:
                    return j

n,k = map(int,raw_input().split())
print prime_number(n,k)

https://www.acmicpc.net/problem/4948

# -*- encoding: cp949 -*-
def prime_number(max_n):
    p = [0 for i in xrange(max_n+1)]
    for i in xrange(2,max_n+1):
        for j in xrange(i,max_n+1,i):
            if j!=i:
                p[j] = 1
    prime = [i for i in xrange(2,max_n+1) if p[i]==0]
    return prime

p = prime_number(300000)
while True:
    n = input()
    if n==0: break
    print len([i for i in p if n<i and i<=2*n])

https://www.acmicpc.net/problem/6588

# -*- encoding: cp949 -*-
def prime_number(max_n):
    p = [0 for i in xrange(max_n+1)]
    for i in xrange(2,max_n+1):
        for j in xrange(i,max_n+1,i):
            if j!=i:
                p[j] = 1
    prime = [i for i in xrange(2,max_n+1) if p[i]==0]
    return prime

p = set(prime_number(1000000))
while True:
    n = input()
    if n==0:break
    flag = False
    for i in xrange(2,n/2+1):
        if i in p and n-i in p:
            print '{} = {} + {}'.format(n,i,n-i)
            flag = True
            break
    if not flag:
        print "Goldbach's conjecture is wrong."

https://www.acmicpc.net/problem/3896

# -*- encoding: cp949 -*-
import bisect
def prime_number(max_n):
    p = [0 for i in xrange(max_n+1)]
    for i in xrange(2,max_n+1):
        for j in xrange(i,max_n+1,i):
            if j!=i:
                p[j] = 1
    prime = [i for i in xrange(2,max_n+1) if p[i]==0]
    return prime

p = prime_number(1299750)
s_p = set(p)
for t in xrange(input()):
    n = input()
    if n in s_p:
        print 0
    else:
        index = bisect.bisect(p,n)
        print p[index] - p[index-1]

https://www.acmicpc.net/problem/11502

# -*- encoding: cp949 -*-
def prime_number(max_n):
    p = [0 for i in xrange(max_n+1)]
    for i in xrange(2,max_n+1):
        for j in xrange(i,max_n+1,i):
            if j!=i:
                p[j] = 1
    prime = [i for i in xrange(2,max_n+1) if p[i]==0]
    return prime

p = prime_number(1000)
for t in xrange(input()):
    l=input()
    flag = False
    for i in p:
        if i<l/2+1:
            for j in p:
                if j<l/2+1:
                    for k in p:
                        if k<l/2+1:
                            if i+j+k==l:
                                print i,j,k
                                flag = True
                                break
                if flag: break
        if flag: break
    if not flag: 0