Euler Problem 7

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6^(th) prime is 13.

What is the 10001^(st) prime number?

Just a simple prime number generator.

public class Euler7
{
    public static void main(String[] args)
    {
        System.out.print(“Problem 7:\n”);
        Euler7 e = new Euler7();
        System.out.print(“Answer = ” + e.Problem7()+ “\n”);
    }
    public String Problem7 ()
    {
        int x = 2;
        int y = 0;
        int c = 1;
        int answer = 0;
        while (c <= 10001)
        {
            if (x % 2 != 0 || x == 2)
            {
                for (y = 2; y <= x/2; y++)
                {
                    if (x % y == 0)
                    {
                        break;
                    }
                }
                if (y > x/2)
                {
                    System.out.print(c + “: ” + x + “\n”);
                    answer = x;
                    c++;
                }
            }
            x++;
        }
        return String.valueOf(answer);
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s