酷代码 AI
菜单

一些正整数可以由一个或多个连续素数的和来表示。 例如,整数53有两种表示方法: 5+7+11+13+17和53。 整数41有三种表示方法:2+3+5+7+11+13, 11+13+17,41。 整数3只有一种表示方法:3。 但整数20没有这样的表示方法。请注意,必须是连续素数,所以无论是7+13还是3+5+5+7都不是整数20的有效表示方法。 您的任务是编写一个程序,报告给定正整数的表示方法数。 输入 输入是一个正整数序列,每个数都在一个单独的行中。。 正整数的范围在2到10000之间(包含2和10000)。 末尾的0表示输入结束。 输出 输出为给定正整数的表示方法的种数。 每个答案都在一个单独的行中,与输入相对应,0除外。 在输出中不应插入其他字符。 样例输入 复制 2 3 17 41 20 666 12 53 0 样例输出 复制 1 1 2 3 0 0 1 2

```python def sieve_of_eratosthenes(limit): is_prime = [True] * (limit + 1) is_prime[0] = is_prime[1] = False for i in range(2, int(limit0.5) + 1): if is_prime[i]: for j in range(i * i, limit + 1, i): is_prime[j] = False primes = [i for i in range(2, limit + 1) if is_prime[i]] return primes def count_prime_sum_representations(n, primes): count = 0 left = 0 current_sum = 0 for right in range(len(primes)): current_sum += primes[right] while current_sum > n and left <= right: current_sum -= primes[left] left += 1 if current_sum == n: count += 1 return count def main(): primes = sieve_of_eratosthenes(10000) results = [] while True: try: n = int(input().strip()) if n == 0: break results.append(count_prime_sum_representations(n, primes)) except EOFError: break for res in results: print(res) if __name__ == "__main__": main() ``` [2026-01-11 17:41:34 | AI问答 | 294点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多