【每日一题20220630】找到限定范围内满足条件的合数

:mage:‍ 给定一个质数列表primesL 和一个整数 limit ,尝试找到在limit数字范围内所有满足质数列表primesL 组合的合数的个数,以及最大的合数

例子

primesL = [2, 5, 7]
limit = 500
List of Numbers Under 500          Prime Factorization
___________________________________________________________
           70                         [2, 5, 7]
          140                         [2, 2, 5, 7]
          280                         [2, 2, 2, 5, 7]
          350                         [2, 5, 5, 7]
          490                         [2, 5, 7, 7]

下面有5 个合数是满足的,最大的一个是490

您必须创建函数count_find_num() ,它接受两个参数:primesLlimit ,并返回满足要求的数字数量,以及limit 下的最大数字。

场景1:

primesL = [2, 5, 7]
limit = 500
count_find_num(primesL, val) == [5, 490]

场景2:

primesL = [2, 3, 47]
limit = 200
count_find_num(primesL, limit) == []

场景3:

primesL = [2, 3, 47]
limit = 282
count_find_num(primesL, limit) == [1, 282]

题目难度:中等

题目来源: https://www.codewars.com/kata/58f9f9f58b33d1b9cf00019d

def count_find_num(primesL:  list, limit: int) -> list:
    # your code here
    return []

assert count_find_num( [2, 5, 7], 500) == [5, 490]
assert count_find_num( [2, 3, 47], 200) == []
assert count_find_num( [2, 3, 47], 282) == [1, 282]
assert count_find_num( [2, 3, 5], 1000) == [19, 960]
def count_find_num1(primesL:list,limit):
    rl=[]
    sum1=1
    ll=sorted(primesL)
    #求list中元素的乘积
    for i in ll:
        sum1=sum1*i
    rl.append(sum1)
    if sum1>limit:
        return []
    else:
        #把sum与每一个元素乘n次得到一个初始列表rl,补充列表中各个元素成绩
        n1=ll[0]*ll[1]
        n2=ll[0]*ll[2]
        n3=ll[1]*ll[2]
        n4=ll[0]*ll[1]*ll[2]
        ll=ll+[n1,n2,n3,n4]
        for i in ll:
            sum2=sum1
            while sum2<limit:
                sum2=sum2*i
                if sum2<limit:
                    rl.append(sum2)
        #把初始列表rl中的元素与primesL相乘得到最终列表
        for j in rl:
            sum3=j
            for n in ll:
                while sum3<limit:
                    sum3=sum3*n
                    if sum3 in rl:
                        continue
                    else:
                        if sum3<limit:
                            rl.append(sum3)
    result=sorted(rl)
    # print(result)

    return [len(result),sorted(result)[-1]]


assert count_find_num1( [2, 5, 7], 500) == [5, 490]
assert count_find_num1( [2, 3, 47], 200) == []
assert count_find_num1( [2, 3, 47], 282) == [1, 282]
assert count_find_num1( [2, 3, 5], 1000) == [19, 960]

编写的时候 使用三个 "`"符号把代码框起来

按要求更新了 :grinning:

def count_find_num(primesL, limit):
    # your code here
    primesL_sort = sorted(primesL)
    mul1 = 1
    for i in primesL_sort:
        mul1 *= i
    if mul1 > limit:
        return []
    else:
        mul_list = []
        mul_list.append(mul1)
        for i in range(len(primesL_sort)):
            mul2 = mul1
            while mul2<=limit:
                mul2 *= primesL_sort[i]
                if mul2<=limit:
                    mul_list.append(mul2)
        for j in mul_list:
            for k in primesL_sort:
                mul3 = j
                while mul3<=limit:
                    mul3 = mul3*k
                    if mul3 in mul_list:
                        continue
                    else:
                        if mul3<=limit:
                            mul_list.append(mul3)
        return [len(mul_list),max(mul_list)]
                

assert count_find_num( [2, 5, 7], 500) == [5, 490]
assert count_find_num( [2, 3, 47], 200) == []
assert count_find_num( [2, 3, 47], 282) == [1, 282]
assert count_find_num( [2, 3, 5], 1000) == [19, 960]
def count_find_num(primesL: list, limit: int) -> list:
    # your code here
    s = 1
    for i in primesL:
        s *= i
    if s > limit:
        return []
    c = [s]
    for i in primesL:
        for j in list(set(c)):
            s = j
            while True:
                s *= i
                if s <= limit:
                    c.append(s)
                    continue
                break
    return [len(c), max(c)]


assert count_find_num([2, 5, 7], 500) == [5, 490]
assert count_find_num([2, 3, 47], 200) == []
assert count_find_num([2, 3, 47], 282) == [1, 282]
assert count_find_num([2, 3, 5], 1000) == [19, 960]
1 个赞
def count_find_num(primesL: list, limit: int) -> list:
    res_list = []
    res = 1
    for i in primesL:
        res *= i
    if res > limit:
        return []
    else:
        res_list.append(res)
        for i in primesL:
            for s in set(res_list):
                res = s
                while True:
                    res *= i
                    if res <= limit:
                        res_list.append(res)
                    else:
                        break
        return [len(res_list), max(res_list)]


assert count_find_num([2, 5, 7], 500) == [5, 490]
assert count_find_num([2, 3, 47], 200) == []
assert count_find_num([2, 3, 47], 282) == [1, 282]
assert count_find_num([2, 3, 5], 1000) == [19, 960]