Shortest code challenge
What could be the shortest code to find whether a given number is prime or not?
Perhaps it could be even shorter ;) https://code.sololearn.com/c2N2QeHwmnVv/?ref=app
This is a Python solution, which does not accept 1 as prime. And it's the shortest code posted yet. https://code.sololearn.com/cM6zTF9r4aeK/?ref=app
Here's, shortest in Java: https://code.sololearn.com/cJqN2oluFpuv/?ref=app
using c language : https://code.sololearn.com/c9KgZS7LhEK4/?ref=app
My try with C++: https://code.sololearn.com/c5RrtIP59y1p/?ref=app
@Utkarsh Actually, 1 is not a prime, so.. ;)
@Utkarsh If I understand it correctly, if you have a given is_prime() function, you can give it any argument, let's say: is_prime(int(input())) # checks a user-input number is_prime(int(math.sqrt(625)) # checks the result etc.
@ Kuba https://code.sololearn.com/c2N2QeHwmnVv/?ref=app Great logic But if we give 1 as input it displays True, which is wrong. How can we avoid providing non positive or 1 as the input to the function and still use it to serve our purpose, again in a shortest way
Prime number checker !! https://code.sololearn.com/c5Q1ExJ10i1x/?ref=app
thanks everyone for your posts👍 @kuba siekierzynski @the coding sloth @groompel @chloe i still have this doubt. we all can understand which numbers are prime or composite or neither prime nor composite. but this won't be the case if the function is taking input from output of other function or from the results of some code. Then what could be a possible solution for it?
@Pegasus @Mizo PRO @Barichnel D Sonna @Kartik Krishnan thanks everyone for your code
Ruby, anyone?: > require "prime" > Prime.prime?(23) # It returns true. Code example: https://code.sololearn.com/c1KJ790a4q6J/#rb
Ruby is the shortest
https://code.sololearn.com/cD4Cg2DhGQ0n/?ref=app I accept all downvotes... dont hesitate to do so. But sooner or later programming will highly mean using web apis.
@Pegasus Cool Oh and you can make your code even shorter if you replace 'puts' with 'p'.
This is just all(n % i for i in range(2, n)) dressed up https://code.sololearn.com/cLhIXYL9mkHG/?ref=app (short but not fast)