What do you think of this algorithm that I came up with to find out in a number is prime? | Sololearn: Learn to code for FREE!
0

What do you think of this algorithm that I came up with to find out in a number is prime?

Let n = any odd positive integer that does not end in 5 1. Find the square root of n. If the square root of n is not a perfect square, then subtract n by 2 and find the square root of the difference. If (n - 2) - 2 is not a perfect square then subtract the difference of (n - 2) - 2 by 2. Keep repeating this process until a perfect square is found. 2. Make a list of all of the odd integers that don't end in 5 from the perfect square found in step 1 to n 3. Divide n by all of the integers in the list in step 2, if none of the quotients are integers, then n is a prime number. However, if one or more of the quotients are integers: then n is not prime.

26th Oct 2020, 6:26 PM
Logan Harrison Crabtree
Logan Harrison Crabtree - avatar
1 Odpowiedź
0
The number of integers in the list you get in step 2 would be O(n - sqrt(n)) = O(n), so the algorithm would be slow for large inputs. Just checking all integers from 1 to int(sqrt(n)) would be much faster. Then you can also drop integers divisible by 2 and 5 (why not 3?).
1st Nov 2020, 9:25 AM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar