-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transform_To_Prime.cs
68 lines (54 loc) · 2.4 KB
/
Transform_To_Prime.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Collections.Generic;
using System.Linq;
namespace CodingChallenges;
/*
Transform To Prime
Given a List [] of n integers , find minimum number to be inserted in a list, so that sum of all elements of list should equal the closest prime number .
Notes
List size is at least 2 .
List's numbers will only positives (n > 0) .
Repetition of numbers in the list could occur .
The newer list's sum should equal the closest prime number .
Input >> Output Examples
1- minimumNumber ({3,1,2}) ==> return (1)
Explanation:
Since , the sum of the list's elements equal to (6) , the minimum number to be inserted to transform the sum to prime number is (1) , which will make the sum of the List equal the closest prime number (7) .
2- minimumNumber ({2,12,8,4,6}) ==> return (5)
Explanation:
Since , the sum of the list's elements equal to (32) , the minimum number to be inserted to transform the sum to prime number is (5) , which will make the sum of the List equal the closest prime number (37) .
3- minimumNumber ({50,39,49,6,17,28}) ==> return (2)
Explanation:
Since , the sum of the list's elements equal to (189) , the minimum number to be inserted to transform the sum to prime number is (2) , which will make the sum of the List equal the closest prime number (191) .
*/
[TestClass]
public class Transform_To_Prime {
[TestMethod]
public void Test() {
Assert.AreEqual(1, MinimumNumber(new int[] { 3, 1, 2 }));
Assert.AreEqual(0, MinimumNumber(new int[] { 5, 2 }));
Assert.AreEqual(0, MinimumNumber(new int[] { 1, 1, 1 }));
Assert.AreEqual(5, MinimumNumber(new int[] { 2, 12, 8, 4, 6 }));
Assert.AreEqual(2, MinimumNumber(new int[] { 50, 39, 49, 6, 17, 28 }));
}
public static int MinimumNumber(int[] numbers) {
int numbresSum = numbers.Sum();
IEnumerable<int> primes = GetPrimesFromRange(numbresSum, numbresSum * 2 + 1);
int bestPrime = primes.FirstOrDefault();
int diff = bestPrime - numbresSum;
return diff;
}
private static IEnumerable<int> GetPrimesFromRange(int min, int max) {
if (min <= 0) min = 1;
bool no = false;
for (int i = min; i <= max; i++) {
no = false;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
no = true;
break;
}
}
if (!no) yield return i;
}
}
}