Tuesday, July 10, 2007

Code Challenge: Michael the Math Maniac

Time for another summer code-challenge. Hopefully this one is a bit easier than the last one :-)

Mr. Michael was a lucky man, cause today, 20070710 (ISO standard) was his birthday!
But Michael wasn't your average lucky birthday boy. He was a Math Maniac. And on this special day, he was wondering: How many of the numbers between 0 and 1.000.000.000 contains the ciphers "20070710" (in that order) somewhere within the number?

Design a method int CountNumbers(int min, int max, int SequenceToFind); that returns the count of numbers which contains the SequenceToFind.
1st prize goes to first valid entry, 2nd prize to best performing entry.
The prizes are still "honour & mocking rights".
May the best developer win.

2 comments:

Peter J. said...

well, i'll never say that it's the best math approach to it, but it does count it.. performance will probably be mayhem tho :)

public int CountNumbers(int min, int max, int SequenceToFind)
{
int results = 0;
string search = SequenceToFind.ToString();

if (SequenceToFind == max)
{
results = 1;
}
if (SequenceToFind < max)
{
if (SequenceToFind>min)
{
min = SequenceToFind;
}
for (int i = min; i <= max; i++)
{
if (i.ToString().Contains(search))
{
results++;
}
}
}
return results;
}

Allan Thræn said...

Congrats Peter. Although you're right - the performance is terrible - you presented the first valid entry and thereby you win the mocking rights of us all until next challenge!