2017年12月23日 星期六

a044: 盈數、虧數和完全數

a044: 盈數、虧數和完全數
'''
對一個正整數 N 而言,將它除了本身以外所有的因數加起來的總和為 S,
如果 S>N,則 N 為盈數,
如果 S<N,則 N 為虧數,
而如果 S=N,則 N 為完全數(Perfect Number)。
例如 10 的因數有 1、2、5、10,1+2+5=8<10,因此10 為虧數,
而 12 的因數有 1、2、3、4、6、12,1+2+3+4+6=16>12,因此 12 為盈數。
至於 6 的因數有 1、2、3、6,1+2+3=6,所以 6 是完全數(它也是第一個完全數)。

現在請你一個正整數,請你判斷它是哪一種。
輸入說明:
請入一個正整數 N。
輸出說明:
若輸入的數為盈數,則輸出 Abundant,
若輸入的數為虧數,則輸出 Deficient,
若輸入的數為完全數,則輸出 Perfect。
範例輸入: 

輸入1:
6
輸入2:
12
範例輸出 :

輸出1:
Perfect

輸出2:
Abundant
'''

def factor(n):
  fac = []
  hf = n//2
  while n>1:
    for i in range(2, hf+1):
      if n%i == 0:
        n //= i;  fac.append(i);  break
  return fac


print("10 的因數 1、2、5、10,1+2+5=8 < 10為虧數")
print("12 的因數 1、2、3、4、6、12,1+2+3+4+6=16 > 12為盈數")
print(" 6 的因數 1、2、3、6,1+2+3 = 6, 6是完全數")
n = int(input("input a number: "))
m=n
result_1=factor(n)
print("因數分解",result_1)

result_2=[]
for i in range (1,m+1):
    if (m % i) == 0 :
        result_2.append(i)

print("所有因數",result_2)

sum1=0
for i in range (0,len(result_2)-1):
    sum1=sum1+result_2[i]
    print(result_2[i],', ',end='')
    
print('=',sum1,end='')

if sum1==m :
    print ("--> 完全數Perfect。")
elif sum1 > m :   
    print ("--> 盈數  Abundant。")
else:
    print ("--> 虧數   Deficient。")

====== ==== RESTART: F:/Python_APSC/a44-2.py ======================
10 的因數 1、2、5、10,1+2+5=8 < 10為虧數
12 的因數 1、2、3、4、6、12,1+2+3+4+6=16 > 12為盈數
 6 的因數 1、2、3、6,1+2+3 = 6, 6是完全數
input a number: 6
因數分解 [2, 3]
所有因數 [1, 2, 3, 6]
1 , 2 , 3 , = 6--> 完全數Perfect。
>>> 
======== === RESTART: F:/Python_APSC/a44-2.py ======================
10 的因數 1、2、5、10,1+2+5=8 < 10為虧數
12 的因數 1、2、3、4、6、12,1+2+3+4+6=16 > 12為盈數
 6 的因數 1、2、3、6,1+2+3 = 6, 6是完全數
input a number: 12
因數分解 [2, 2, 3]
所有因數 [1, 2, 3, 4, 6, 12]
1 , 2 , 3 , 4 , 6 , = 16--> 盈數  Abundant。
>>> 
======= ==== RESTART: F:/Python_APSC/a44-2.py ======================
10 的因數 1、2、5、10,1+2+5=8 < 10為虧數
12 的因數 1、2、3、4、6、12,1+2+3+4+6=16 > 12為盈數
 6 的因數 1、2、3、6,1+2+3 = 6, 6是完全數
input a number: 10
因數分解 [2, 5]
所有因數 [1, 2, 5, 10]
1 , 2 , 5 , = 8--> 虧數   Deficient。
>>>

沒有留言:

張貼留言

2024年4月24日 星期三 Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --2

 2024年4月24日 星期三 Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --2 AngularJS 實例 <!DOCTYPE html> <html> <head> &...