Don't like ads? PRO users don't see any ads ;-)
Guest

Skib can't read comments properly.

By: a guest on Apr 28th, 2012  |  syntax: C#  |  size: 5.16 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2.  
  3. namespace EulerProblem1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.             //Solves problem 1 of Project Euler, easily found by doing a Google search for it.
  9.             //This is my first actual attempt at commenting my code. Cheers. -Yanlin
  10.         {
  11.             bool retry = true;
  12.             while (retry == true)
  13.                 //A later function sets retry as false and breaks the loop if the user specifies he does not wish to run the program again.
  14.             {
  15.                 ulong mult1 = 0;
  16.                 //The first multiple to check
  17.                 ulong mult2 = 0;
  18.                 //The second multiple to check.
  19.                 ulong limit = 0;
  20.                 //The upper limit for the multiples' results. IE: limit of 10 for multiple of 3 makes 9 the biggest possible.
  21.                 ulong total = 0;
  22.                 //Sum of both multiples, up to the limit.
  23.                 Console.Clear();
  24.                 Console.WriteLine("Yanlin's Project Euler Problem 1 solver. V1.2.2");
  25.                 string name1 = "multiple";
  26.                 //Hardcoded string for user input function
  27.                 string name2 = "limit";
  28.                 //Hardcoded string for user input function
  29.                 mult1 = userinput(mult1, name1);
  30.                 //New function for user input, coded in V1.2.
  31.                 mult2 = userinput(mult2, name1);
  32.                 //New function for user input, coded in V1.2.
  33.                 limit = userinput(limit, name2);
  34.                 //New function for user input, coded in V1.2.
  35.                 total = Problem(mult1, mult2, limit, total);
  36.                 //New function for actually calculating the solution. Coded in V1.2.2
  37.                 Console.Clear();
  38.                 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);
  39.                 Console.WriteLine(total);
  40.                 retry = close(retry);
  41.             }
  42.             Console.Clear();
  43.             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.");
  44.             Console.ReadKey();
  45.         }
  46.         //Room for functions here:
  47.         static ulong Problem(ulong mult1, ulong mult2, ulong limit, ulong total)
  48.             // Used to calculate the two multiples. Coded in V1.2.2
  49.             //0 acts as a "none"
  50.             // If identical numbers are set as mults, both will be calculated as if they are different, resulting in a "double" result. Intended operation.
  51.         {
  52.             ulong number1 = 0;
  53.             ulong number2 = 0;
  54.             ulong track1 = 0;
  55.             ulong track2 = 0;
  56.  
  57.             while (track1 < limit && mult1 != 0)
  58.             {
  59.                 number1 += mult1;
  60.                 track1 += number1;
  61.             }
  62.             if (number1 == limit)
  63.                 track1 -= number1;
  64.  
  65.             while (track2 < limit && mult2 != 0)
  66.             {
  67.                 number2 += mult2;
  68.                 track2 += number2;
  69.             }
  70.             if (number2 == limit)
  71.                 track2 -= number2;
  72.             total = track1 + track2;
  73.             return total;
  74.  
  75.         }
  76.         static bool close(bool retry)
  77.             //Closes the program or allows for a reset. Coded in V1.2.2
  78.             //Works as long as the main loop uses a bool to check if to run again.
  79.         {
  80.            
  81.             char YN = 'A'; //Would be even easier to code as a string, with the ability to use .ToLower
  82.             while (YN != 'Y')
  83.             {
  84.                 Console.WriteLine("\nWould you like to restart the program? Y/N");
  85.                 while (true)
  86.                 {
  87.                     try
  88.                     {
  89.                         YN = char.Parse(Console.ReadLine());
  90.                         break;
  91.                     }
  92.                     catch
  93.                     {
  94.                         Console.WriteLine("Only Y or N are valid inputs");
  95.                     }
  96.                 }
  97.                 if (YN == 'y' || YN == 'Y')
  98.                     YN = 'Y';
  99.                 else if (YN == 'N' || YN == 'n')
  100.                     YN = 'N';
  101.                 else
  102.                     Console.WriteLine("Only Y or N are valid inputs");
  103.                 if (YN == 'N')
  104.                     return false;
  105.             }
  106.             return true;
  107.         }
  108.         static ulong userinput (ulong input1, string input2)
  109.             //Used as a replacement to doing this code three times. Coded in V1.2.2
  110.             //Takes the user input and a hardcoded string to display what the user is inputting currently.
  111.         {
  112.             while (true)
  113.                 {
  114.                     Console.WriteLine("\nInput first {0}:", input2);
  115.                     try
  116.                     {
  117.                         input1 = ulong.Parse(Console.ReadLine());
  118.                         return input1;
  119.                     }
  120.                     catch
  121.                     {
  122.                         Console.WriteLine("Only natural numbers (Positive integers) are valid inputs");
  123.                     }
  124.                 }
  125.  
  126.         }
  127.     }
  128. }