Problem:


Problem B: 完全数

  

    ある整数 N に対し，その数自身を除く約数の和を S
    とする．
    N = S のとき
    N は完全数 (perfect number)，
    N > S のとき
    N は不足数 (deficient number)，
    N < S のとき
    N は過剰数 (abundant number) と呼ばれる．
    与えられた整数が，完全数・不足数・過剰数のどれであるかを
    判定するプログラムを作成せよ．
  

  

    プログラムの実行時間が制限時間を越えないように注意すること．
  

  
Input

  

    入力はデータセットの並びからなる．
    データセットの数は 100 以下である．
  

  

    各データセットは整数 N
    (0 < N ≤ 100000000) のみを含む1行からなる．
  

  

    最後のデータセットの後に，入力の終わりを示す 0 と書かれた1行がある．
  

  
Output

  

    各データセットに対し，
    整数 N が完全数ならば &ldquo;perfect number&rdquo;，
    不足数ならば &ldquo;deficient number&rdquo;，
    過剰数ならば &ldquo;abundant number&rdquo; という文字列を
    1行に出力せよ．
  

  
Sample Input

  

1
2
3
4
6
12
16
28
33550336
99999998
99999999
100000000
0

  
Output for the Sample Input

  

deficient number
deficient number
deficient number
deficient number
perfect number
abundant number
deficient number
perfect number
perfect number
deficient number
deficient number
abundant number



Sample input:
1
2
3
4
6
12
16
28
33550336
99999998
99999999
100000000
0


Sample output:
deficient number
deficient number
deficient number
deficient number
perfect number
abundant number
deficient number
perfect number
perfect number
deficient number
deficient number
abundant number

Submission status: Wrong Answer

Solution code:
from math import sqrt
P = "perfect number"
D = "deficient number"
A = "abundant number"
while True:
    n = input()
    if n==0:
        break
    if n==1:
        print D
        continue
    su = 1
    temp = n
    for i in xrange(2, int(sqrt(n))+1):
        cnt = 0
        while temp%i==0:
            temp /= i
            cnt += 1
        if cnt:
            su *= (i**(cnt+1)-1)/(i-1)
        if temp==1:
            su -= n
            break
    if temp!=1:
        su *= (temp**2-1)/(temp-1)
        su -= n
    print su, temp

    print P if n==su else (D if su<n else A)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env/ python

Submission status: Time Limit Exceeded

Solution code:
prm=[2,3]
for n in range(5,50000000,2):
	for x in prm:
		if n%x==0 and x*x<=n:
			break
	else:
		prm.append(n)

def sum_of_factors(n):
	global prm
	if n in prm:
		return n
	else:
		for i in prm:
			if n%i==0:
				return i+sum_of_factors(n//i)

while 1:
	n=int(input())
	if n==0:
		break
	if n in prm or n==1:
		print("deficient number")
	elif n==sum_of_factors(n):
		print("perfect number")
	else:
		print("abundant number")

Submission status: Time Limit Exceeded

Solution code:
def f(p):
    return sum([n for n in range(1,p) if p%n==0])

while 1:
    n=int(input())
    if n==0:break
    m=f(n)
    if n==m:print('perfect number')
    else: print('deficient number' if n>m else 'abundant number')

Submission status: Time Limit Exceeded

Solution code:
def f(p):
    ans=0
    for n in range(1,p):
        if p%n==0:ans+=n
    return ans

while 1:
    n=int(input())
    if n==0:break
    m=f(n)
    if n==m:print('perfect number')
    else: print('deficient number' if n>m else 'abundant number')

Submission status: Time Limit Exceeded

Solution code:
def f(p):
    ans=0
    if p<=5: return ans
    for n in range(1,int(p/2)+1):
        if p%n==0:ans+=n
    return ans

while 1:
    n=int(input())
    if n==0:break
    m=f(n)
    if n==m:print('perfect number')
    else: print('deficient number' if n>m else 'abundant number')

Submission status: Time Limit Exceeded

Solution code:
while 1:
    n = int(input())
    if n == 0:break
    a, m = 1, n
    i = 1
    s = 1
    while 1:
        if m % 2 == 0:
            m //= 2
            s += 2 ** i
            i += 1
        else:break
    a *= s
    j = 3
    while m > 1:
        i = 1
        s = 1
        while 1:
            if m % j == 0:
                m //= j
                s += j ** i
                i += 1
            else:break
        a *= s
        j += 2
    a -= n
    if a < n:print("deficient number")
    elif a > n:print("abundant number")
    else:print("perfect number")

Submission status: Time Limit Exceeded

Solution code:
while 1:
    n = int(input())
    if n == 0:break
    if n < 6:print("deficient number");continue
    a, m = 1, n
    i = 1
    s = 1
    while 1:
        if m % 2 == 0:
            m //= 2
            s += 2 ** i
            i += 1
        else:break
    a *= s
    j = 3
    while m > 1:
        i = 1
        s = 1
        while 1:
            if m % j == 0:
                m //= j
                s += j ** i
                i += 1
            else:break
        a *= s
        if m * a > 2 * n:print("abundant number");break
        j += 2
    else:
        a -= n
        if a < n:print("deficient number")
        elif a > n:print("abundant number")
        else:print("perfect number")

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if n == 0:break
  solvers = set()
  for i in range(1, int(n ** (1 / 2)) + 2):
    if n % i == 0:
      solvers.add(i)
      solvers.add(n // i)
  score = sum(solvers) - n
  if n < score:
    print("abundant number")
  elif n == score:
    print("perfect number")
  else:
    print("deficient number")

Submission status: Accepted

Solution code:
from math import sqrt
P = "perfect number"
D = "deficient number"
A = "abundant number"
while True:
    n = input()
    if n==0:
        break
    if n==1:
        print D
        continue
    su = 1
    temp = n
    for i in xrange(2, int(sqrt(n))+1):
        cnt = 0
        while temp%i==0:
            temp /= i
            cnt += 1
        if cnt:
            su *= (i**(cnt+1)-1)/(i-1)
        if temp==1:
            su -= n
            break
    if temp!=1:
        su *= (temp**2-1)/(temp-1)
        su -= n
    print P if n==su else (D if su<n else A)

Submission status: Accepted

Solution code:
P = "perfect number"
D = "deficient number"
A = "abundant number"
while True:
    n = int(raw_input())
    if n==0:
        break
    if n==1:
        print D
        continue
    su = 1
    t = n
    i = 2
    while i**2<=t:
        if t%i==0:
            cnt = 0
            while t%i==0:
                t /= i
                cnt += 1
            su *= (i**(cnt+1)-1)/(i-1)
        i += 1
    if t>1:
        su *= (t**2-1)/(t-1)
    su -= n
    print P if n==su else (D if su<n else A)

Submission status: Accepted

Solution code:
def f(p):
    ans=1
    if p<=5: return 0
    for n in range(2,int(p**0.5)+1):
        if p%n==0:
            if n!=p//n:ans+=n+p//n
            else:ans+=n
    return ans

while 1:
    n=int(input())
    if n==0:break
    m=f(n)
    if n==m:print('perfect number')
    else: print('deficient number' if n>m else 'abundant number')

Submission status: Accepted

Solution code:
import sys

p="perfect number"
d="deficient number"
a="abundant number"





number_list=[]


while True:
    N=int(input())
    if N==0:
        break
    else:
        number_list.append(N)


maximum=int(max(number_list)**0.5)



prime_table=[i for i in range(maximum+1)]
prime_table[1]=0

for i in range(2,int(maximum**0.5)):
    if prime_table[i]==i:
        k=2
        while k*i<=maximum:
            prime_table[k*i]==0
            k+=1
for N in number_list:
    N_origin=N
    devide_list=[]
    for i in prime_table:
        if i!=0:
            count=0
            while N%i==0:
                count+=1
                N=N/i
            devide_list.append([i,count])
    if N!=1:
        devide_list.append([N,1])

    S=1
    for i in devide_list:
        S*=(i[0]**(i[1]+1)-1)/(i[0]-1)
    S-=N_origin
    if N_origin==S:
        print(p)
    elif N_origin>S:
        print(d)
    else:
        print(a)

Submission status: Accepted

Solution code:
def div(n):
	ls = []
	i = 2
	while i*i <= n:
		c = 0
		if n%i==0:
			while n%i==0:
				n /= i
				c += 1
		if c > 0:
			ls.append([i,c])
		i += 1
	if n > 1:
		ls.append([n,1])
	ans = 1
	for b,p in ls:
		ans *= (b**(p+1)-1)/(b-1)
	return ans
	
while 1:
	n = input()
	if n == 0: break
	d = div(n) - n
	if d == n:
		print "perfect number"
	elif d < n:
		print "deficient number"
	else:
		print "abundant number"

Submission status: Accepted

Solution code:
def SumOfDiv(n):
	i = 2
	ans = 1
	while i*i <= n:
		c = 0
		while n%i==0:
			n /= i
			c += 1
		if c > 0:
			ans *= (i**(c+1)-1)/(i-1)
		i += 1
	if n > 1:
		ans *= (1+n)
	return ans
	
while 1:
	n = input()
	if n == 0: break
	d = SumOfDiv(n) - n
	if d == n:
		print "perfect number"
	elif d < n:
		print "deficient number"
	else:
		print "abundant number"

Submission status: Accepted

Solution code:
def make_division(n:int):
	divisors = []
	for i in range(1,int(n**0.5)+1):
		if n % i == 0:
			divisors.append(i)
			if i != n // i:
				divisors.append(n//i)
	divisors.sort()
	divisors.pop(-1)

	return sum(divisors)

if __name__ == '__main__':

	while True:
		n = int(input())
		if n == 0:
			break
		m = make_division(n)

		if n == m:
			print("perfect number")
		elif n > m:
			print("deficient number")
		else:
			print("abundant number")

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    if n==0:break
    s=[]
    for i in range(1,int(n**0.5)+1):
        if n%i==0:
            s.append(i)
            s.append(n//i)
    s=list(set(s))
    s.remove(n)
    if sum(s)==n:
        print("perfect number")
    elif sum(s)<n:
        print("deficient number")
    elif sum(s)>n:
        print("abundant number")

Submission status: Accepted

Solution code:
import math
while True:
    N = int(input())
    if N == 0:
        break
    elif N == 1:
        print("deficient number")
    else:
        sum = 1
        for i in range(2, int(math.sqrt(N)) + 1):
            if N % i == 0 and i**2 !=N:
               sum += N / i + i
            elif i**2 ==N:
                sum += i
        if sum == N:
            print("perfect number")
        elif sum > N:
            print("abundant number")
        else:
            print("deficient number")

Submission status: Accepted

Solution code:
while 1:
    m=n=int(input())
    if n==0:break
    i=2
    a=1
    while i*i<=n:
        c=0
        while n%i==0:
            n//=i
            c+=1
        if c:
            a*=(i**(c+1)-1)//(i-1)
        i+=1
    if n>1:
        a*=1+n
    if a==2*m:
        print("perfect number")
    elif a<2*m:
        print("deficient number")
    else:
        print("abundant number")