189k views
5 votes
Write a Python function that finds the first n terms of a sequence, where each term is the sum of the prime numbers smaller than or equal to that term, and the sum itself is also a prime number.

Your function should have the following signature:
def ultimate_prime_summation_sequence(n: int) -> List[int]:
pass_______________________________________________
Input: An integer n (1 <= n <= 1000) representing the number of terms required in the sequence.
Output: A list of the first n terms of the sequence.
Example:
ultimate_prime_summation_sequence(5) # Output: [5, 23, 53, 853, 1097]
Make sure the program run perfectly, and your code must be well commented for my understanding. Will upvote if answer is correct.

User Ayca
by
8.5k points

1 Answer

6 votes

Final answer:

The student is seeking a Python function to generate a sequence where each term is the sum of prime numbers less than or equal to that term, and the sum is also prime. The provided Python code includes two functions: one to check for primes and the main function to generate the sequence according to the conditions specified.

Step-by-step explanation:

The student is asking for a Python function that generates a special sequence. Each term of the sequence is the sum of prime numbers less than or equal to the term itself, and this sum must also be prime. Below is a sample Python function to find the first n terms of this sequence:

from typing import List

# Check if a number is prime
def is_prime(num: int) -> bool:
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True

# Function to generate the sequence
def ultimate_prime_summation_sequence(n: int) -> List[int]:
sequence = [] # list to store the sequence
term = 2 # starting term
while len(sequence) < n:
prime_sum = sum([i for i in range(term + 1) if is_prime(i)])
if is_prime(prime_sum):
sequence.append(term)
term += 1
return sequence

The function ultimate_prime_summation_sequence returns the required sequence. The is_prime function helps to determine if a given number is a prime number or not. The main function iterates through numbers, summing the prime numbers less than or equal to each number and checking if the sum is prime before adding the term to the resultant list.

User Kusal Kithmal
by
7.5k points