Self-hatred

Project Euler Problem #10

As of the time of publishing, I have not yet submitted this solution to the website because it is offline. I have, however, searched around and verified the correctness. I kept returning an incorrect answer until I switched variables a and b to long. It works. This was a boring problem: I just copied my algorithm from problems three and seven, and set it to a counter running to 2000000.

using System;

public class Ten
{
	static void Main()
	{
		long a = 2000000;
		long b = 0;

		for (int i = 2; i < = a; i++)
			if (check(i))
				b += i;

		Console.WriteLine("\n{0}\n",b);
	}

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

		for (int i = 2; i <= Math.Sqrt(n); 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>