+ 50

Shortest code challenge

What could be the shortest code to find whether a given number is prime or not?

7th Jan 2018, 4:18 PM
Utkarsh
65 Réponses
+ 42
Perhaps it could be even shorter ;) https://code.sololearn.com/c2N2QeHwmnVv/?ref=app
7th Jan 2018, 7:04 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 37
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
7th Jan 2018, 9:10 PM
Tob
Tob - avatar
+ 30
https://code.sololearn.com/c3ECgQg6B8kg/?ref=app
8th Jan 2018, 9:19 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
9th Jan 2018, 8:19 PM
LukArToDo
LukArToDo - avatar
11th Jan 2018, 5:17 AM
S U M A N
S U M A N - avatar
9th Jan 2018, 6:10 AM
Mohammad Dakdouk
Mohammad Dakdouk - avatar
+ 20
https://code.sololearn.com/W5jgG6c8e4uh/?ref=app
7th Jan 2018, 5:42 PM
The Coding Sloth
The Coding Sloth - avatar
+ 20
@Utkarsh Actually, 1 is not a prime, so.. ;)
7th Jan 2018, 11:24 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 19
@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.
8th Jan 2018, 6:35 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
9th Jan 2018, 6:01 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 16
@ 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
7th Jan 2018, 8:53 PM
Utkarsh
9th Jan 2018, 5:47 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 15
https://code.sololearn.com/crB6bP3WP7gq/?ref=app
12th Jan 2018, 2:49 PM
Abhishek Tandon
Abhishek Tandon - avatar
+ 14
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?
8th Jan 2018, 4:16 AM
Utkarsh
+ 14
@Pegasus @Mizo PRO @Barichnel D Sonna @Kartik Krishnan thanks everyone for your code
8th Jan 2018, 5:49 PM
Utkarsh
+ 14
Ruby, anyone?: > require "prime" > Prime.prime?(23) # It returns true. Code example: https://code.sololearn.com/c1KJ790a4q6J/#rb
14th Jan 2018, 10:03 PM
noobcøder
noobcøder - avatar
+ 13
Ruby is the shortest
8th Jan 2018, 9:18 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 13
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.
10th Jan 2018, 11:59 AM
Oma Falk
Oma Falk - avatar
+ 12
@Pegasus Cool Oh and you can make your code even shorter if you replace 'puts' with 'p'.
8th Jan 2018, 3:04 PM
MizoPro
MizoPro - avatar
+ 12
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)
10th Jan 2018, 11:16 AM
David Ashton
David Ashton - avatar