Project Euler Problem #24
00:00:00.0716283
This one hurt my head, even after I was led onto Narayana Pandita’s algorithm. The algorithm depends on finding the largest integer that is smaller than another integer in the sequence/array; however, I’ve looked at several code solutions that just come in from the right and pick the first integer that is smaller than another. So here I am!
Time to start studying for my repeat XNA exam.
using System;
public class TwentyFour
{
static void Main()
{
int[] a = {0,1,2,3,4,5,6,7,8,9};
int b = 1;
int c = 1000000;
while (b < c)
{
a = Permute(a);
b++;
}
Print(a);
}
static void Print(int[] z)
{
Console.WriteLine();
for (int i = 0; i < z.Length; i++)
Console.Write(z[i]);
Console.WriteLine("n");
}
static int[] Permute(int[] z)
{
int k = -1;
int l = 0;
for (int i = (z.Length - 2); i >= 0; i--)
if (z[i] < z[i + 1])
{
k = i;
break;
}
for (int i = z.Length - 1; i > k; i--)
if (z[i] > z[k])
{
l = i;
break;
}
int tmp = z[k];
z[k] = z[l];
z[l] = tmp;
Array.Reverse(z, k + 1, z.Length - (k + 1));
return z;
}
}
Categorised as: programming