Finding prime numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Finding prime numbers

Can anyone help me with the error am making in this code? https://code.sololearn.com/cDJC4I9ci6jK/?ref=app

14th Apr 2019, 7:25 PM
xpan
xpan - avatar
13 Answers
+ 3
Anna, I wanted a code that would check if a number is prime, not one that loops and prints prime numbers in a range. Anyways, with your help, Anna, I have finally found how, so the new code now. https://code.sololearn.com/cDJC4I9ci6jK/?ref=app
15th Apr 2019, 8:30 AM
xpan
xpan - avatar
+ 6
Thanks so much guys. Luis, such a nice help.
15th Apr 2019, 12:29 AM
xpan
xpan - avatar
+ 4
But, still not working
15th Apr 2019, 12:38 AM
xpan
xpan - avatar
+ 3
Luis SepĂșlveda That would not check if a number is prime or not.
14th Apr 2019, 7:55 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Thanks, Luis. Thanks everyone
15th Apr 2019, 12:57 AM
xpan
xpan - avatar
15th Apr 2019, 4:35 AM
Anna
Anna - avatar
+ 3
Nice. Looks a bit like the code that I gave you
15th Apr 2019, 8:32 AM
Anna
Anna - avatar
+ 3
Yes, thanks a lot
15th Apr 2019, 12:46 PM
xpan
xpan - avatar
+ 3
Anna, I still need your help with something
15th Apr 2019, 12:47 PM
xpan
xpan - avatar
+ 2
I'm not going to lie, this is an awkward way to check for prime numbers. The prime generator function doesn't do what its name says. And it shouldn't be necessary to pass "prime" as a parameter to "findPrime". But it seems to work def prime(num): i=2 while i<num: yield i i+=1 def findPrime(func,num): p = func(num) for d in p: if num%d==0: return "Is not prime" return "prime" print(findPrime(prime,5))
14th Apr 2019, 8:04 PM
Anna
Anna - avatar
+ 1
Denise Roßberg Oh, I don't know why I looked at the code and my thoughts completely changed.. Oopsie!
14th Apr 2019, 10:16 PM
Luis SepĂșlveda
Luis SepĂșlveda - avatar
+ 1
https://code.sololearn.com/cMlWZQb13V4G/?ref=app I made this code a few weeks back, but I don't know if it is functional in its entirety. (I tried to fix it a bit a minute ago) Hope it helps! (Theres a bit of explanation on it)
15th Apr 2019, 12:46 AM
Luis SepĂșlveda
Luis SepĂșlveda - avatar
- 1
All you gotta do is change num%i > 0 to num%2 == 0 Mod 2 only returns 2 numbers: 0 or 1 0 for even numbers and 1 for odd numbers. I wish you success! EDIT: Woops, this is not an even/odd check
14th Apr 2019, 7:48 PM
Luis SepĂșlveda
Luis SepĂșlveda - avatar