Advertisement
Guest User

Untitled

a guest
May 26th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 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;//Input 2 integers
  14. Console.Write("Int 1: ");
  15. Console.ForegroundColor = ConsoleColor.White;
  16. int num1 = Convert.ToInt32(Console.ReadLine());
  17. Console.ForegroundColor = ConsoleColor.Cyan;
  18. Console.Write("Int 2: ");
  19. Console.ForegroundColor = ConsoleColor.White;
  20. int num2 = Convert.ToInt32(Console.ReadLine());
  21. Console.WriteLine();
  22.  
  23. int input = num1;
  24. List<int> factors1 = Factorise(input);//Finds prime factors of input 1
  25. input = num2;
  26. List<int> factors2 = Factorise(input);//Finds prime factors of input 2
  27.  
  28. Console.ForegroundColor = ConsoleColor.Cyan; //Prints prime factors of input 1
  29. Console.Write("Int 1 prime factors: ");
  30. Console.ForegroundColor = ConsoleColor.White;
  31. factors1.ForEach(item => Console.Write(item + ", "));
  32. Console.WriteLine();
  33.  
  34. Console.ForegroundColor = ConsoleColor.Cyan;//Prints prime factors of input 2
  35. Console.Write("Int 2 prime factors: ");
  36. Console.ForegroundColor = ConsoleColor.White;
  37. factors2.ForEach(item => Console.Write(item + ", "));
  38. Console.WriteLine();
  39.  
  40. Shared(factors1, factors2, num1, num2);
  41.  
  42. }
  43.  
  44.  
  45. static List<int> Factorise(int input)
  46. {
  47. List<int> factors = new List<int>();
  48. int a = 2;
  49. while (true)
  50. {
  51. if (a == input)
  52. {
  53. factors.Add(a);
  54. return factors;
  55. }
  56. int b = input % a; //b is remainder.
  57. if (b > 0)
  58. {
  59. //if b is not 0, a is not a factor of input
  60. a++;
  61. }
  62. else if (b == 0)
  63. {
  64. factors.Add(a);
  65. input = (input / a);
  66. a = 2;
  67. }
  68. }
  69. }
  70. static void Shared(List<int> factors1, List<int> factors2, int num1, int num2)
  71. {
  72. List<int> shared = new List<int>();
  73. for (int i = 0; i <= factors1.Count; ++i)
  74. {
  75. for (int j = 0; j <= factors2.Count; ++j)
  76. {
  77. if (factors1[i] == factors2[j])
  78. {
  79. shared.Add(factors1[i]);
  80. factors1.Remove(i);
  81. factors2.Remove(j);
  82. --i;
  83. --j;
  84. break;
  85. }
  86. }
  87. }
  88.  
  89.  
  90. Console.ForegroundColor = ConsoleColor.Cyan; //Prints shared factors
  91. Console.Write("Shared factors: ");
  92. Console.ForegroundColor = ConsoleColor.White;
  93. shared.ForEach(item => Console.Write(item + ", "));
  94. Console.WriteLine();
  95. Console.WriteLine();
  96.  
  97. int hcf = Hcf(shared);
  98. Console.ForegroundColor = ConsoleColor.Cyan;//Prints HCF
  99. Console.Write("Highest Common Factor: ");
  100. Console.ForegroundColor = ConsoleColor.White;
  101. Console.WriteLine(hcf);
  102. }
  103. static int Hcf(List<int> shared)
  104. {
  105. int num = 1;
  106. for (int i=0; i < shared.Count;i++)
  107. {
  108. num = shared[i] * num;
  109. }
  110. return num;
  111. }
  112.  
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement