- 1
Create an algorithm that detects prime numbers using simple libraries
8 Réponses
+ 2
Show your attempt First
+ 2
write a for loop in range(2,num) if num is divisible by any "i" it means num is "not prime" else "num" is prime..
apply the same math logic which you learned in the school..
+ 2
Well library name is sympy
Example:
import sympy
print('prime') if isprime(int(input() or 2) else print('not prime ')
+ 1
Pallemoni Jyothi
Python Is very very very large, thus, Is posibole, search in Google your ask
0
import math
def is_prime(n):
if n == 2:
return True
if n % 2 == 0 or n <= 1:
return False
sqr = int(math.sqrt(n)) + 1
for divisor in range(3, sqr, 2):
if n % divisor == 0: return False
return True
print(is_prime(12) )
print(is_prime(2) )
0
Gigi ,
you are showing a really nice code to help someone solving a task. we all appreciate this very much!
(this is my personal statement - but it is not to criticize someone !!)
but there is an issue by giving a ready-made solution, when the op has not done and shown his attempt first.
as a part of the community i believe that learning by doing is more helpful, than just using a shared solution. we should act more as a mentor.
it is a very successful way when we are giving hints and help, so that the op is able to solve his task by himself. it would be great if you are going to help us to keep sololearn what it is:
▪︎a unique place to learn (we are a "self" learning platform!)
▪︎an absolutely great community that is willing to help other people
▪︎a great resource of people which has an unbelievable knowledge in coding and a long lasting experience
▪︎a place to meet people and to discuss with them all the possible points of view
thanks for your understanding
0
Dear HrCoder,
I know what you mean but I partially disagree. I started with programming 4 years ago and I know how it feels when you really don't know how to begin. I have learned a lot from solutions at the beginning and then of course with practice. I don't know the level of the person who asked this problem, think is a beginner because I did this when I was trying to learn code, I remember that I solved it with sieve of Eratosthenes (yes, somebody explained it to me).
If it is a crime, I ll stop to share solutions to people who didn't provide an attempt, although I dont agree. Otherwise I ll continue.
0
num=int(input())
for i in range(2,num):
msk=[]
for j in range(1,i+1):
if i%j==0:
msk.append(1)
if sum(msk)==2:
print(i