using System;
namespace EulerProblem1
{
class Program
{
static void Main(string[] args)
//Solves problem 1 of Project Euler, easily found by doing a Google search for it.
//This is my first actual attempt at commenting my code. Cheers. -Yanlin
{
bool retry = true;
while (retry == true)
//A later function sets retry as false and breaks the loop if the user specifies he does not wish to run the program again.
{
ulong mult1 = 0;
//The first multiple to check
ulong mult2 = 0;
//The second multiple to check.
ulong limit = 0;
//The upper limit for the multiples' results. IE: limit of 10 for multiple of 3 makes 9 the biggest possible.
ulong total = 0;
//Sum of both multiples, up to the limit.
Console.Clear();
Console.WriteLine("Yanlin's Project Euler Problem 1 solver. V1.2.2");
string name1 = "multiple";
//Hardcoded string for user input function
string name2 = "limit";
//Hardcoded string for user input function
mult1 = userinput(mult1, name1);
//New function for user input, coded in V1.2.
mult2 = userinput(mult2, name1);
//New function for user input, coded in V1.2.
limit = userinput(limit, name2);
//New function for user input, coded in V1.2.
total = Problem(mult1, mult2, limit, total);
//New function for actually calculating the solution. Coded in V1.2.2
Console.Clear();
Console.Write("\n\n\nThe sum of the multiples of:\n{0} and {1}\n\nto a limit of:\n{2}\n\nis:\n", mult1, mult2, limit);
Console.WriteLine(total);
retry = close(retry);
}
Console.Clear();
Console.WriteLine("\n\n\n\n\nThank you for using:\nYanlin's Project Euler Problem 1 solver. V1.2.2\n\nPress any key to close the program.");
Console.ReadKey();
}
//Room for functions here:
static ulong Problem(ulong mult1, ulong mult2, ulong limit, ulong total)
// Used to calculate the two multiples. Coded in V1.2.2
//0 acts as a "none"
// If identical numbers are set as mults, both will be calculated as if they are different, resulting in a "double" result. Intended operation.
{
ulong number1 = 0;
ulong number2 = 0;
ulong track1 = 0;
ulong track2 = 0;
while (track1 < limit && mult1 != 0)
{
number1 += mult1;
track1 += number1;
}
if (number1 == limit)
track1 -= number1;
while (track2 < limit && mult2 != 0)
{
number2 += mult2;
track2 += number2;
}
if (number2 == limit)
track2 -= number2;
total = track1 + track2;
return total;
}
static bool close(bool retry)
//Closes the program or allows for a reset. Coded in V1.2.2
//Works as long as the main loop uses a bool to check if to run again.
{
char YN = 'A'; //Would be even easier to code as a string, with the ability to use .ToLower
while (YN != 'Y')
{
Console.WriteLine("\nWould you like to restart the program? Y/N");
while (true)
{
try
{
YN = char.Parse(Console.ReadLine());
break;
}
catch
{
Console.WriteLine("Only Y or N are valid inputs");
}
}
if (YN == 'y' || YN == 'Y')
YN = 'Y';
else if (YN == 'N' || YN == 'n')
YN = 'N';
else
Console.WriteLine("Only Y or N are valid inputs");
if (YN == 'N')
return false;
}
return true;
}
static ulong userinput (ulong input1, string input2)
//Used as a replacement to doing this code three times. Coded in V1.2.2
//Takes the user input and a hardcoded string to display what the user is inputting currently.
{
while (true)
{
Console.WriteLine("\nInput first {0}:", input2);
try
{
input1 = ulong.Parse(Console.ReadLine());
return input1;
}
catch
{
Console.WriteLine("Only natural numbers (Positive integers) are valid inputs");
}
}
}
}
}