Ahhh now this one uses something we built before.. around problem 16 I think… just a slight change needed… job done!! Nice to have an easy one for a change!
Problem = sum the numbers from the answer of 100!
http://projecteuler.net/index.php?section=problems&id=20
_________
static void Main(string[] args)
{
int[] c = new int[999];
c[0] = 1; //starting condition
for (int i = 1; i < 100; i++) //go to the power of 1000
{
//do mult in place
for (int n = 0; n < 999; n++)
{
c[n] *= i;
}
//sort out carries across to right
for (int n = 0; n < 999; n++)
{
while (c[n]>9)
{
if (c[n] >= 10)
{
c[n + 1] += 1;
c[n] -= 10;
}
}
}
}
//now add up all the columns
long sum = 0;
for (int n = 0; n < 999; n++)
{
sum += c[n];
}
Console.WriteLine(sum);
Console.ReadLine();
}
No comments:
Post a Comment