鱼C论坛

 找回密码
 立即注册
查看: 589|回复: 6

[已解决]分解质因数一道题求解

[复制链接]
发表于 2020-4-12 17:08:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
SOS
最佳答案
2020-4-12 17:17:02
这些题我昨天刚解决
  1. import math
  2. def is_prime(number):
  3.     if number > 1:
  4.         if number == 2:
  5.             return True
  6.         if number % 2 == 0:
  7.             return False
  8.         for each in range(3,int(math.sqrt(number) + 1),2):
  9.             if number % each == 0:
  10.                 return False
  11.         return True
  12.     return False
  13. def get_primes(number):
  14.     while True:
  15.         if is_prime(number):
  16.             yield number
  17.         number += 1
  18. def solve(count):
  19.     clist = [2]
  20.     for next_prime in get_primes(3):
  21.         if next_prime < count:
  22.             clist.append(next_prime)
  23.         else:
  24.             return clist
  25.             break
  26. def mincommul(num,nlist):
  27.     for i in nlist:
  28.         if not num % i:
  29.             return [i,num // i]
  30. def main():
  31.     try:
  32.         count = int(input('count = '))
  33.         clist1 = solve(count)
  34.         clist2 = []
  35.         while True:
  36.             clist2.append(mincommul(count,clist1)[0])
  37.             count = mincommul(count,clist1)[1]
  38.             if count == 1:
  39.                 break
  40.         print(clist2)
  41.     except Exception as error:
  42.         print('An error has occurred:' + str(error))
  43. if __name__ == '__main__':
  44.     main()
复制代码
QQ图片20200412170840.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-12 17:17:02 | 显示全部楼层    本楼为最佳答案   
这些题我昨天刚解决
  1. import math
  2. def is_prime(number):
  3.     if number > 1:
  4.         if number == 2:
  5.             return True
  6.         if number % 2 == 0:
  7.             return False
  8.         for each in range(3,int(math.sqrt(number) + 1),2):
  9.             if number % each == 0:
  10.                 return False
  11.         return True
  12.     return False
  13. def get_primes(number):
  14.     while True:
  15.         if is_prime(number):
  16.             yield number
  17.         number += 1
  18. def solve(count):
  19.     clist = [2]
  20.     for next_prime in get_primes(3):
  21.         if next_prime < count:
  22.             clist.append(next_prime)
  23.         else:
  24.             return clist
  25.             break
  26. def mincommul(num,nlist):
  27.     for i in nlist:
  28.         if not num % i:
  29.             return [i,num // i]
  30. def main():
  31.     try:
  32.         count = int(input('count = '))
  33.         clist1 = solve(count)
  34.         clist2 = []
  35.         while True:
  36.             clist2.append(mincommul(count,clist1)[0])
  37.             count = mincommul(count,clist1)[1]
  38.             if count == 1:
  39.                 break
  40.         print(clist2)
  41.     except Exception as error:
  42.         print('An error has occurred:' + str(error))
  43. if __name__ == '__main__':
  44.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-12 17:20:14 | 显示全部楼层
关键是要看懂我的代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-12 17:27:42 | 显示全部楼层
wuqramy 发表于 2020-4-12 17:20
关键是要看懂我的代码

看不懂讲解下大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-12 17:41:22 | 显示全部楼层
  1. def f(n: int, /) -> list:
  2.     res=[]

  3.     while not n&1:
  4.         n>>=1
  5.         res.append(2)
  6.    
  7.     temp=3
  8.     maximum=int(n**0.5)

  9.     while temp<=maximum:
  10.         if not n%temp:
  11.             while not n%temp:
  12.                 n//=temp
  13.                 res.append(temp)

  14.             maximum=int(n**0.5)

  15.         temp+=2

  16.     if n!=1:
  17.         res.append(n)

  18.     return res

  19. print(f(int(input())))
复制代码
我能说一句吗,你好菜啊
不光菜,更是连思考都不思考,上来就问。
这样能学好吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-12 17:42:59 | 显示全部楼层
永恒的蓝色梦想 发表于 2020-4-12 17:41
我能说一句吗,你好菜啊
不光菜,更是连思考都不思考,上来就问。
这样能学好吗?

我错了 我已经写过了然后才问你们看看有别的方法吗!!1
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-12 17:59:58 From FishC Mobile | 显示全部楼层
永恒的蓝色梦想 发表于 2020-4-12 17:41
我能说一句吗,你好菜啊
不光菜,更是连思考都不思考,上来就问。
这样能学好吗?


可能他没学过分解质因数
(满满的不服)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-16 00:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表