Archive for the 'Mathematics' Category

Finding Prime Numbers

Here’s my prime number algorithm implementation that I have written in C#.


        public static bool IsPrimeNumber(double x)
        {
            if (x <= 1)
            {
                return false;
            }
            else if (!(x % 1 != 0))
            {
                double i = 2;

                bool r = true;
                while (i <= Math.Sqrt(x))
                {
                    if (!((x / i) % 1 != 0))
                    {
                        r = false;
                        break;
                    }
                    i++;
                }
                return r;
            }
            else
            {
                return false;
            }
        } 

Quadratic equations

In this post I will write about the ways of solving an quadratic equation. To solve them you also need to know how to solve an equation. If you don’t I may write a tutorial soon. Now let’s begin!

A quadratic equation is an equation written in this form:

ax² + bx + c = 0

This is an example of a quadratic equation:

x² + 10x + 2 = 0

This is the so-called “normal form” where the x² is free (times 1, not more or less). An equation of this kind has two solutions.There are a several ways to solve an quadratic equation depending on how it is written. Here are the three different types of states you are going to look for:

  1. When the product is zero (0).
  2. When it is missing the px term.
  3. When the product is not zero.

Method 1

You can use the following method when you try to solve an equation that is equal to zero and that misses a q-term.

x² + 5x = 0

Now you are going to factorize this equation into:

x(x+5)= 0

x times x is x², x times 5 is 5x and if we add those two we will get 0.

We can now find the solutions by knowing that everything is going to be equal to 0. Take a look inside the parantheses, we have x and adds 5. If we say that x is -5 and we add 5 then the expression will be equal to 0. We have found the first solution -5. And then the last x (the one outside paranthesis) will be zero because the other expression is zero.

The solutions are:

1: -5
2: 0

Method 2

It is very easy to get the solution from an equation thats misses the px-term. First you should re-order the terms (as always if needed). In this case: the x²-term on the left side and your q on the right of the equality operator.

x² - 25 = 0 x² = 25

Then you can take the square root out of the right-side term and voilà you got the solution. Well there are two actually, a negative and a positive.

1: 5
2: -5

Method 3

When you got all terms you can use the following formula, however I will not tell how this formula works (or maybe I do it in a future blogpost).

The “pq-formula”

Make sure you have the “normal form” and replace the terms in this formula with your numbers. The whole thing about this form is that you can use this form to find the solutions. Depending on if you use plus or minus (that is what +/- means) you get a solution.

That is all for now.


Pages

Categories

RSS MSDN Highlights

  • An error has occurred; the feed is probably down. Try again later.