Self-hatred

Project Euler Problem #7

The answer is a bit of a trick, and there is actually a hint within the question: Start your prime count at 2, not at 1, or 0, like me.

using System;

public class Seven
{
	static void Main()
	{
		int a = 2;
		int b = 0;
		int c = 10001;
		
		while (b < = c)
		{	
			if (check(a))				
				b++;
			if (b < c)
				a++;
		}
		Console.WriteLine("{0}", a);
	}

	static bool check(int n)
	{
		bool prime = true;

		for (int i = 2; i <= n/2; i++)
			if (n % i == 0)
			{
				prime = false;
				break;
			}

		return prime;
	}
}

Categorised as: programming


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>