gpt4 book ai didi

Python挑战: distinct combination of prime numbers in a given range

转载 作者:行者123 更新时间:2023-12-01 05:25:56 28 4
gpt4 key购买 nike

考虑这个挑战:

Given two numbers A and B, in how many ways you can select B distinct primes, where each of the primes should be less than or equal to A (<= A). Since the number can be large, provide your answer mod 65537.

例如

如果A = 7B = 2 ,那么:
所有素数 <= A{2, 3, 5, 7} ,答案将是 6 :{2, 3} {2, 5} {2, 7} {3, 5} {3, 7} {5, 7}

我创建了这个解决方案:

from math import factorial
from math import fmod

def nPk(n,k):
return int( factorial(n)/factorial(n- k))

def is_prime(a):
for i in range(2,a):
if a % i ==0:
return False
return True

def distinct_primes(A):
primes = []
for i in range(2,A):
if is_prime(i):
primes.append(i)
return len(primes)

def fct(A, B):
return nPk(distinct_primes(A),B)

#print fct(59,5)% 65537
print fmod(fct(69,8), 65537)

但是,我没有得到正确的答案!我在这里缺少什么?

最佳答案

Ionut 关于包容性问题的说法是正确的!
除此之外,您应该将 nPk 定义更改为:

def nPk(n,k):
return int( factorial(n)/(factorial(n- k) * factorial(k)))

关于Python挑战: distinct combination of prime numbers in a given range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21344492/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com