I think this is the first actual discussion thread of code on a coder's website I've actually ever seen, and I'm the one posting it. (HA!) So basically; I got bored and found a website called Project Euler. It has a bunch of "mathematical" problems to solve. Eight pages and I believe 50 problems per page. Well I've been jumping back and forth between pages all day solving the problems out of boredom. Well, I came across this one and just had to share my work. It was the first time I've ever implemented the .Substring(int, int) method before so it gave me a little bit of problems at first, but I figured it out and got the answer. I'm deciding to share my source code as well as the answer to the problem. I just wanted to know other's thoughts on the topic and see if there is a way (which I'm sure there is) to shorten the source code, or even a completely different way of solving this problem.
The problem is: Find the highest product of 5 consecutive digits in the 1000 digit number.
Answer: 40,824 with 994 possible other answers, and the consecutive digits being 9,9,8,7,9.
Source:
private string OneThousandDigitNumber = "7316717653133062491922511967442657474235534
919493496983520312774506326239578318016984801869478851843858615607891129494954595017
379583319528532088055111254069874715852386305071569329096329522744304355766896648950
445244523161731856403098711121722383113622298934233803081353362766142828064444866452
387493035890729629049156044077239071381051585930796086670172427121883998797908792274
921901699720888093776657273330010533678812202354218097512545405947522435258490771167
055601360483958644670632441572215539753697817977846174064955149290862569321978468622
482839722413756570560574902614079729686524145351004748216637048440319989000889524345
065854122758866688116427171479924442928230863465674813919123162824586178664583591245
665294765456828489128831426076900422421902267105562632111110937054421750694165896040
807198403850962455444362981230987879927244284909188845801561660979191338754992005240
636899125607176060588611646710940507754100225698315520005593572972571636269561882670
428252483600823257530420752963450";
private void button1_Click(object sender, EventArgs e) {
List<int> Possibles = new List<int>();
List<string> Pos2 = new List<string>();
for (int i = 0; i < OneThousandDigitNumber.Length - 5; i++) {
int Temp = 0;
string Temp2 = "";
Temp = Convert.ToInt32(OneThousandDigitNumber.Substring(i, 1));
Temp *= Convert.ToInt32(OneThousandDigitNumber.Substring(i + 1, 1));
Temp *= Convert.ToInt32(OneThousandDigitNumber.Substring(i + 2, 1));
Temp *= Convert.ToInt32(OneThousandDigitNumber.Substring(i + 3, 1));
Temp *= Convert.ToInt32(OneThousandDigitNumber.Substring(i + 4, 1));
Temp2 = OneThousandDigitNumber.Substring(i, 1);
Temp2 += OneThousandDigitNumber.Substring(i + 1, 1);
Temp2 += OneThousandDigitNumber.Substring(i + 2, 1);
Temp2 += OneThousandDigitNumber.Substring(i + 3, 1);
Temp2 += OneThousandDigitNumber.Substring(i + 4, 1);
Possibles.Add(Temp);
Pos2.Add(Temp2);
}
int Highest = 0;
string High2 = "";
for (int i = 0; i < Possibles.Count; i++) {
if (Possibles[i] > Highest) {
Highest = Possibles[i];
High2 = Pos2[i];
}
}
MessageBox.Show("The highest possible product of five consecutive digits in the
one thousand digit number is: " + Highest.ToString() + "\nWith " +
Possibles.Count.ToString() + " possibles.\nThe consecutive digits were: " + High2);
}
So what are the thoughts of other programmers on the site?
Love to hear some replies!
Jamie