Simple prime number generator in Python

There are some problems: Why do you print out count when it didn’t divide by x? It doesn’t mean it’s prime, it means only that this particular x doesn’t divide it continue moves to the next loop iteration – but you really want to stop it using break Here’s your code with a few fixes, … Read more

Check if number is prime number

var number; Console.WriteLine(“Accept number:”); number = Convert.ToInt32(Console.ReadLine()); if (IsPrime(number)) { Console.WriteLine(“It is prime”); } else { Console.WriteLine(“It is not prime”); } public static bool IsPrime(int number) { if (number <= 1) return false; if (number == 2) return true; if (number % 2 == 0) return false; var boundary = (int)Math.Floor(Math.Sqrt(number)); for (int i = … Read more

How to create the most compact mapping n → isprime(n) up to a limit N?

The fastest algorithm for general prime testing is AKS. The Wikipedia article describes it at lengths and links to the original paper. If you want to find big numbers, look into primes that have special forms like Mersenne primes. The algorithm I usually implement (easy to understand and code) is as follows (in Python): def … Read more

Is a list (potentially) divisible by another?

Build bipartite graph structure – connect a[i] with all its divisors from b[]. Then find maximum matching and check whether it is perfect matching (number of edges in matching is equal to the number of pairs (if graph is directed) or to doubled number). Arbitrary chosen Kuhn algorithm implementation here. Upd: @Eric Duminil made great … Read more

C – determine if a number is prime

OK, so forget about C. Suppose I give you a number and ask you to determine if it’s prime. How do you do it? Write down the steps clearly, then worry about translating them into code. Once you have the algorithm determined, it will be much easier for you to figure out how to write … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)