L1 - 006连续因子

思路:(1)根据输入格式可知最多可以到13!但是13!会达到最大,所以采用达到12。

(2)因为n^(1/2) * (n^(1/2)+1) 会大于我们输入的正整数,所以我们只需要遍历到n^(1/2),并且题目要求是不要求1的所以我们从2开始遍历。

(3)在实际考虑中我们需要分成质数和非质数。如果是质数,会直接输出我们的输入的正整数。

(4)我们采用取余数的方法判断是否为质数即 num % 2 == 0。

(5)符合我们将2存入列表中,2+1,继续判断num % 3 == 0,若符合继续存入。

(6)最长因子个数,如果是质数,直接输出1,不是由templen 判断

import math #导入数学计算相关的包
maxLen = 0 #最长连续因子个数
resultList = []#连续因子
isPrimary = True #标识是否为质数

num = int(input())

for i in range(2,int(math.sqrt(num))+1):#从2开始依次遍历至最大因子
    tempNum = num
    tempList=[]
    start = i
    while tempNum%start==0:#一轮遍历
        isPrimary=False
        tempNum = tempNum//start
        tempList.append(str(start))
        start+=1
    tempLen=start-i       #算出长度
    if tempLen > maxLen :#更新数据
        resultList=tempList
        maxLen=tempLen

if isPrimary == True:#输出结果
    print(1)
    print(num)
else:
    print(maxLen)
print("*".join(resultList))

 代码及思路参考于:https://blog.csdn.net/qq_41912858/article/details/119464647?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164173274616780274196339%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164173274616780274196339&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-5-119464647.first_rank_v2_pc_rank_v29&utm_term=python%E8%BF%9E%E7%BB%AD%E5%9B%A0%E5%AD%90&spm=1018.2226.3001.4187

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值