Solve this code coach... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Solve this code coach...

Generators Finding prime numbers is a common coding interview task. The given code defines a function isPrime(x), which returns True if x is prime. You need to create a generator function primeGenerator(), that will take two numbers as arguments, and use the isPrime() function to output the prime numbers in the given range (between the two arguments). Sample Input 10 20 Sample Output [11, 13, 17, 19] The given code takes the two arguments as input and passes them to the generator function, outputting the result as a list. Code....👇 def isPrime(x): if x < 2: return False elif x == 2: return True for n in range(2, x): if x % n ==0: return False return True def primeGenerator(a, b): #your code goes here f = int(input()) t = int(input()) print(list(primeGenerator(f, t)))

16th Sep 2023, 3:16 AM
Musharaf Nazir
Musharaf Nazir - avatar
10 Answers
+ 8
I already have Musharaf Nazir but you haven't. Please show me as well as the community your attempt So that we can help you.
16th Sep 2023, 3:29 AM
BroFar
BroFar - avatar
+ 7
Musharaf Nazir I see you posted the code as given without showing what you attempted... def primeGenerator(a,b): ???????????????????????
16th Sep 2023, 3:25 AM
BroFar
BroFar - avatar
+ 6
This is not the place where you get the readymade codes. Try yourself, and ask your doubts in the community.
16th Sep 2023, 3:33 AM
JAY
JAY - avatar
+ 6
Musharaf Nazir a and b are parameters of the primeGenerator function. These parameters are used to define a range of numbers for which you want to generate prime numbers.
16th Sep 2023, 3:41 AM
JAY
JAY - avatar
+ 6
Musharaf Nazir The inputs will be similar to rewriting without inputs as ... print(list(primeGenerator(10,20))) But when testing f & t will be different numbers
16th Sep 2023, 3:41 AM
BroFar
BroFar - avatar
+ 2
Riya My doubts are What are a and b in this code
16th Sep 2023, 3:35 AM
Musharaf Nazir
Musharaf Nazir - avatar
+ 2
def primeGenerator(a, b):     for i in range(a, b+1):         if isPrime(i):             yield i The primeGenerator() function takes two arguments, a and b, which are the lower and upper bounds of the range of numbers to be checked for primality. The function uses the isPrime() function to check whether each number in the range is prime or not. If a number is prime, it is yielded by the generator. To use the primeGenerator() function to output the prime numbers between two given numbers, you can simply pass those numbers as arguments to the function and convert the generator object returned by the function to a list using the list() function. Here’s how you can do it: f = int(input()) t = int(input()) print(list(primeGenerator(f, t))) For example, if you input 10 and 20 as the lower and upper bounds respectively, you will get the following output: Output [11, 13, 17, 19]
16th Sep 2023, 7:55 PM
Aya Love
Aya Love - avatar
+ 1
BroFar solve this code coach. It is present in Python Developer in generetors topic... Go ahead and see
16th Sep 2023, 3:27 AM
Musharaf Nazir
Musharaf Nazir - avatar
0
f = 10 t = 20
16th Sep 2023, 3:33 AM
Musharaf Nazir
Musharaf Nazir - avatar
0
To test for a number to see if it is prime or not one has to divide it by every single prime number that is until it reaches the one that is the closest square root for the same number that you are testing. Ex.: let's test for the number 1403 to see if it is prime or not. First, we have to find its square root which is 37,45664160... so 37 is the closest hole number to that square root. Then we have to test every single prime number until we reach that mark (37) To do that we have to test for 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, and 37 to see if 1403 is prime which means we've to divide 1403 for every single number in that list and see if it is a multiple of that prime or not. If 1403 is NOT evenly divisible by any of these numbers then, therefore, it is a prime number, plain and simple. In case you are wondering 1403 is evenly divisible by 23 (23 x 61 = 1403) so it is NOT a prime.
18th Sep 2023, 1:31 AM
Rob_SJ.26
Rob_SJ.26 - avatar