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.
smart but very slow solution...
This is wrong, the loop can go to n.
Test it with "print(i)" with n=7883 for example
7883 is a prime number, this code will test every number until 7883.
the loop actually never goes beyond sqrt(n)
This comment is hidden because it contains spoiler information about the solution
'xrange' is a function in Python 2.x.
In Python 3.x, use range() function instead.
Excellent. Thank you for spelling this out for me.
NameError: name 'xrange' is not defined
'xrange' was renamed to 'range' in Python 3 appearently.
Also I would change n /= i to n //= i to more explicity call integer division, tho I'm not certain it matters since it's only running on modulo 0 stuff in the while loop, but normally it would cause some undesired non-integer results.
The given implementation can be improved by just changing the upperbound of the range to be int(math.sqrt(n))+2
Still probably not the most efficient approach, but there's no point in checking numbers above the sqrt(n)+1.
Edit:
Actually I think it does not matter since the return statement will exit the function before those higher values are tested.
This is a really smart solution, it takes advantage of the fact that the smallest factor of a number must be prime.
Your code is much more efiicient
I literally had the same piece of code but the fcking when I tried it, it would always timeout on me. Idk how it worked for you.
This may not work if the n is super big, the accuracy is limited by the digits of n.
A little improvization on this solution will take care of null factor. Check my solution.
What you said is correct, if the number has a large factor, it will be time consuming.Please refer to my solution.
This comment is hidden because it contains spoiler information about the solution
Loading more items...