Project Euler Problem #18

I am beginning to feel as if my head is full of mush every time I have to deal with a jagged array in a loop. I stole the solution from here, but the code is entirely my own.

In short, you begin at the bottom-left corner of the jagged array. You move to the right and evaluate each pair of numbers. You add the higher of the two to the number above. Take the first three numbers:

63
04 62

Weigh 04 and 62. Add the higher of the two – 62 – to 63:

125
04 62

Repeat this cascade. Ultimately everything will be added to a[0][0]. Output this. Problem #67 repeats this problem exactly, and except for some additional file handling the solution below can be used.

using System;

public class Eighteen
{
	static void Main()
	{
		int[][] a = new int[15][];

		a[0]  = new int[1]  {75};
		a[1]  = new int[2]  {95,64};
		a[2]  = new int[3]  {17,47,82};
		a[3]  = new int[4]  {18,35,87,10};
		a[4]  = new int[5]  {20,04,82,47,65};
		a[5]  = new int[6]  {19,01,23,75,03,34};
		a[6]  = new int[7]  {88,02,77,73,07,63,67};
		a[7]  = new int[8]  {99,65,04,28,06,16,70,92};
		a[8]  = new int[9]  {41,41,26,56,83,40,80,70,33};
		a[9]  = new int[10] {41,48,72,33,47,32,37,16,94,29};
		a[10] = new int[11] {53,71,44,65,25,43,91,52,97,51,14};
		a[11] = new int[12] {70,11,33,28,77,73,17,78,39,68,17,57};
		a[12] = new int[13] {91,71,52,38,17,14,91,43,58,50,27,29,48};
		a[13] = new int[14] {63,66,04,68,89,53,67,30,73,16,69,87,40,31};
		a[14] = new int[15] {04,62,98,27,23,09,70,98,73,93,38,53,60,04,23};

		for (int i = 14; i > 0; i--)
			for (int j = 0; j < a[i].Length-1; j++)
				if (a[i][j] > a[i][j+1])
					a[i-1][j] += a[i][j];
				else if (a[i][j+1] > a[i][j])
					a[i-1][j] += a[i][j+1];

		Console.WriteLine("\n{0}\n", a[0][0]);
	}
}

Project Euler Problem #12

This was a question of two parts:

  1. Calculate the next triangle number in sequence.
  2. Take said triangle number and calculate how many divisors it has. Loop until you find one with 500, and break.

The first part was simple:

Start with n = 1. For every iteration of the loop, add n to n, and add 1:

n = 1
n = n + n + 1

The second part…not so simple. I’ll honestly say I am struggling with understanding the correct way to test for divisors; this works, however:

Take a number, i. Find the square root of i. Loop. Check every integer up to n to see if it divides evenly. Increment the count. Double the answer and return it.

using System;

public class Twelve
{
	static void Main()
	{
		long a = 1;
		long b = 1;
		long c = 0;

		while (c < = 500)
		{
			c = factors(a);

			if (c > 500)
			{
				Console.Write("\n{0}\n", b);
				break;
			}
			else
			{
				a += b + 1;
				b++;
			}
		}
	}

	static long factors(long a)
	{
		long b = 1;

		for (int i = 1; i < = Math.Sqrt(a); i++)
			if (a % i == 0)
				b++;

		return 2 * b;
	}
}

Project Euler Problem #6

using System;

public class Six
{
	static void Main()
	{
		double a = 100;
		double b = 0;
		double c = 0;
		
		for (int i = 1; i < = a; i++)
		{
			b += (i * i);
			c += i;
		}

		c = c * c;

		if (b > c)
			Console.WriteLine("n{0}n", b - c);
		else if (c > b)
			Console.WriteLine("n{0}n", c - b);
	}
}

Project Euler Problem #5

using System;

public class Five
{
	static void Main()
	{
		int a = 20;
		int b = 0;

		while (b < = 0)
		{
			for (int i = 11; i < 20; i++)
			{
				if (a % i != 0)
				{
					a += 20;
					break;
				}
				else if (i == 19)
				{
					b = a;
					break;
				}
			}
		}

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