Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
just change num to num ** 0.5 + 1, it's the same calculation but way faster for larger inputs.
my method is fast a half of time than your, but also slow
This one timed out. FAIL.
Are you saying it's better to perform
O(sqrt(n))
times more operations to save up on one roughlyO(1)
operation?True, but a power or square root operation is a very expensive operation.
Slow, you're iterating through way too many 'n's checking divisibility
this code will timeout if num is like 100000
range(2,int(num/2)) maybe quicker
the function any() ,its my first time see it~ learning~
this code should not range to num,just to the square root of num,when the num is big or there is not only a num,it's slow
... range(2,int(num ** 0.5) + 1) - it is faster
A prime number is a natural number greater than 1
nmadsen, that is not true:
all
will return as soon as it hits a false value. And this will happen at the same entry as thenot any
variant, since the expression is opposite.From a logical standpoint, you are correct. However, not any() is faster than all() in this case for any non-prime number because any() will return true as soon as it hits a true value (i.e. it doesn't have to go through the whole range) whereas all() has to check the entire range before outputting its result.
If the number is negative, it is not a prime by definition. Using the absolute value would result in incorrect responses.
the question also prompted for negative inputs. Of course mathematically we define for positive integers.
Loading more items...