2016年8月24日 星期三

python 程式設計50題測試範例-16

#16、找出所有小於5,000之正整數,其值等於其各個數字之階乘和。
#   例:4!+0!+5!+8!+5!
#        = 24+1+120+40320+120
#        = 40585
#  輸出範例一 ==> 1,2,145,40585


程式: 
#coding:UTF-8
#16、找出所有小於5,000之正整數,其值等於其各個數字之階乘和。
#   例:4!+0!+5!+8!+5!
#        = 24+1+120+40320+120
#        = 40585
#  輸出範例一 ==> 1,2,145,40585


print("16、找出所有小於5,000之正整數,其值等於其各個數字之階乘和。")
print("   例:  4!+0!+5!+8!+5!")
print("          = 24+1+120+40320+120")
print("          = 40585")

print("=============  python 程式設計50題測試範例-16 ==========")
print(" 找出所有小於5,000之正整數,其值等於其各個數字之階乘和  ")
print(" 這些數字有:",end='')

i=1

while (i<=50000):
  n=int(i)
  sum1=0
  while(n!=0):
    j=int(n%10)
    n=int(n/10)
    sum2=1
    k=1
    while(k<=j):
      sum2=int(sum2*k)
      k=k+1
      
    sum1=int(sum1+sum2)

  if (i==sum1):
    print(i,end=',')
    
  i=i+1

print()

結果:

================= RESTART: D:/程式語言 Python 入門/50題/Ex50-16.py =================
16、找出所有小於5,000之正整數,其值等於其各個數字之階乘和。
   例:  4!+0!+5!+8!+5!
          = 24+1+120+40320+120
          = 40585
=============  python 程式設計50題測試範例-16 ==========
 找出所有小於5,000之正整數,其值等於其各個數字之階乘和  
 這些數字有:1,2,145,40585,
>>> 

沒有留言:

張貼留言

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> &...