Advertisement
Guest User

Untitled

a guest
May 26th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp7
  8. {
  9. class Program
  10. {
  11. static public void Main(string[] args)
  12. {
  13. Console.ForegroundColor = ConsoleColor.Cyan;//title
  14. Console.WriteLine(" Sweeney's Highest Common Factor and Lowest Common Multiple Calculator");
  15. Console.ForegroundColor = ConsoleColor.White;
  16.  
  17. Console.ForegroundColor = ConsoleColor.Cyan;//Input 2 integers
  18. Console.Write("Int 1: ");
  19. Console.ForegroundColor = ConsoleColor.White;
  20. int num1 = Convert.ToInt32(Console.ReadLine());
  21. Console.ForegroundColor = ConsoleColor.Cyan;
  22. Console.Write("Int 2: ");
  23. Console.ForegroundColor = ConsoleColor.White;
  24. int num2 = Convert.ToInt32(Console.ReadLine());
  25. Console.WriteLine();
  26.  
  27. int input = num1;
  28. List<int> factors1 = Factorise(input);//Finds prime factors of input 1
  29. input = num2;
  30. List<int> factors2 = Factorise(input);//Finds prime factors of input 2
  31.  
  32. Console.ForegroundColor = ConsoleColor.Cyan; //Prints prime factors of input 1
  33. Console.Write("Int 1 prime factors: ");
  34. Console.ForegroundColor = ConsoleColor.White;
  35. factors1.ForEach(item => Console.Write(item + ", "));
  36. Console.WriteLine();
  37.  
  38. Console.ForegroundColor = ConsoleColor.Cyan;//Prints prime factors of input 2
  39. Console.Write("Int 2 prime factors: ");
  40. Console.ForegroundColor = ConsoleColor.White;
  41. factors2.ForEach(item => Console.Write(item + ", "));
  42. Console.WriteLine();
  43.  
  44. Shared(factors1, factors2, num1, num2);
  45.  
  46. }
  47.  
  48.  
  49. static List<int> Factorise(int input)
  50. {
  51. List<int> factors = new List<int>();
  52. int a = 2;
  53. while (true)
  54. {
  55. if (a == input)
  56. {
  57. factors.Add(a);
  58. return factors;
  59. }
  60. int b = input % a; //b is remainder.
  61. if (b > 0)
  62. {
  63. //if b is not 0, a is not a factor of input
  64. a++;
  65. }
  66. else if (b == 0)
  67. {
  68. factors.Add(a);
  69. input = (input / a);
  70. a = 2;
  71. }
  72. }
  73. }
  74. static void Shared(List<int> factors1, List<int> factors2, int num1, int num2)
  75. {
  76. List<int> shared = new List<int>();
  77. for (int i = 0; i < factors1.Count; ++i)
  78. {
  79. for (int j = 0; j < factors2.Count; ++j)
  80. {
  81. if (factors1[i] == factors2[j])
  82. {
  83. shared.Add(factors1[i]);
  84. factors1.RemoveAt(i);
  85. factors2.RemoveAt(j);
  86. --i;
  87. --j;
  88. break;
  89. }
  90. }
  91. }
  92.  
  93.  
  94. Console.ForegroundColor = ConsoleColor.Cyan; //Prints shared factors
  95. Console.Write("Shared factors: ");
  96. Console.ForegroundColor = ConsoleColor.White;
  97. shared.ForEach(item => Console.Write(item + ", "));
  98. Console.WriteLine();
  99. Console.WriteLine();
  100.  
  101. int hcf = Hcf(shared);
  102. Console.ForegroundColor = ConsoleColor.Cyan;//Prints HCF
  103. Console.Write("Highest Common Factor: ");
  104. Console.ForegroundColor = ConsoleColor.White;
  105. Console.WriteLine(hcf);
  106.  
  107. int lcm = Lcm(hcf, factors1, factors2);
  108. Console.ForegroundColor = ConsoleColor.Cyan;//Prints LCM
  109. Console.Write("Lowest Common Multiple: ");
  110. Console.ForegroundColor = ConsoleColor.White;
  111. Console.WriteLine(lcm);
  112. Console.ReadLine();
  113. }
  114. static int Hcf(List<int> shared)
  115. {
  116. int num = 1;
  117. for (int i = 0; i < shared.Count; i++)
  118. {
  119. num = shared[i] * num;
  120. }
  121. return num;
  122. }
  123. static int Lcm(int hcf, List<int> factors1, List<int> factors2)
  124. {
  125. for (int i = 0; i < factors1.Count; i++)
  126. {
  127. hcf = factors1[i] * hcf;
  128. }
  129. for (int i = 0; i < factors2.Count; i++)
  130. {
  131. hcf = factors2[i] * hcf;
  132. }
  133. return hcf;
  134. }
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement