34.1k views
4 votes
Python.

Write full program and implement a boolean function named is_Primed(x) to check for a prime number. The function is accepting N integer number and returns true or false.

In the main function generate 20 random integer numbers, pass them to the functions above and show the results.

Find the summation and the count of prim numbers.

User Mouffette
by
8.1k points

1 Answer

6 votes

Final answer:

To check if a number is prime, iterate through numbers from 2 to the square root of the number and check for divisibility. Implement a program to generate 20 random integers, check for primality, and find the count and sum of prime numbers.

Step-by-step explanation:

In order to check if a number is prime, we need to iterate through all numbers from 2 to sqrt(x) and check if x is divisible by any of these numbers. If x is divisible by any number within this range, then it is not prime. If x is not divisible by any number within this range, then it is prime. Here is an example implementation:

import math
import random


def is_prime(x):
if x < 2:
return False
for i in range(2, int(math.sqrt(x)) + 1):
if x % i == 0:
return False
return True


def main():
primes = []
count = 0
summation = 0
for _ in range(20):
num = random.randint(1, 1000)
if is_prime(num):
primes.append(num)
count += 1
summation += num
print(f'Prime numbers: {primes}')
print(f'Count of prime numbers: {count}')
print(f'Summation of prime numbers: {summation}')


main()

User Sebastien Peek
by
7.7k points