Sum of Numbers as Distinct Primes - c#

//List Style
using System;
using System.Collections.Generic;
using System.Linq;
public class pr{
static public void Main (){
int n, i, j, k, l, sum,flag = 0;
//int sum = i+j;
//int k = (n-i);
//int l = (n-j);
//System.Console.WriteLine ("Enter a number");
//n = Convert.ToInt32 (Console.ReadLine());
//List <int> primes = new List <int>(); //list to handle the numbers
//HashSet <int> myPrimes = new HashSet <int> (primes);
System.Console.WriteLine ("Enter a number");
n = Convert.ToInt32 (Console.ReadLine());
//myPrimes.Add(n);
//myPrimes.Add(i);
//myPrimes.Add(j);
// var count = string.Join(", ", primes);
//System.Console.WriteLine("The value of n is {0}",myPrimes);
for(i=3; i<n/2; i++){
for(j=3; j<n/2; j++){
if(checkPrime(i) == 1){
if(checkPrime(j) == 1){
if (checkPrime(n-i) == 1){
if (checkPrime(n-j) == 1){
//if(i == j){
//sum = i+j;
System.Console.WriteLine("{0}={1}+{2}\n",n,i,n-i);
//}
}
}
}
}
if (flag == 0 && (n-i) <= 0 && (n-j) <= 0){ //check to avoid dupes
if (n <= 0 && i <= 0 && j <= 0){
Console.Write("{0}\n",n);
}
}
}
}
}
public static int checkPrime(int n){
int i, j, flag = 1;
for (i = 2; i<=(Math.Sqrt(n)); i++){
for (j = 2; j<=(Math.Sqrt(n)); j++){
if (n%i == 0 && n%j == 0 ){ //even number check
i++;
j++;
flag = 0;
}
}
}
return flag;
}
}
So I have been experimenting with this for a while now. I cant seem to print all possible solutions. For example for 24 I am able to print 7+17 but not 2+5+17. There are also some answers being repeated and this might have to do with the fact that I dont have duplicate checks. I tried to push the integers in a list and then use a hashset to only have distinct integers but I got stuck and tried to brute force it. All the numbers to be printed are supposed to be distinct prime integers. I dont understand how to print all distinct numbers and is there an elegant way to print out all the possible.
Thanks for the help!

Don't know if it's elegant enough for you, but I've just mashed a dirty way to make it work:
static void Main()
{
Console.WriteLine("Enter a number");
var numberToSum = Convert.ToInt32(Console.ReadLine());
var primesInRange = GetPrimesUpTo(numberToSum);
var foundSolutions = primesInRange.SubSetsOf().Where(prime => prime.Sum() == numberToSum);
foreach (var solution in foundSolutions.ToList())
{
var formatOperation = solution
.Select(x => x.ToString())
.Aggregate((a, n) => a + " + " + n) + " = " + numberToSum;
Console.WriteLine(formatOperation);
}
Console.ReadLine();
}
public static IEnumerable<int> GetPrimesUpTo(int end)
{
var primes = new HashSet<int>();
for (var i = 2; i <= end; i++)
{
var ok = true;
foreach (var prime in primes)
{
if (prime * prime > i)
break;
if (i % prime == 0)
{
ok = false;
break;
}
}
if (ok)
primes.Add(i);
}
return primes;
}
public static IEnumerable<IEnumerable<T>> SubSetsOf<T>(this IEnumerable<T> source)
{
if (!source.Any())
return Enumerable.Repeat(Enumerable.Empty<T>(), 1);
var element = source.Take(1);
var haveNots = SubSetsOf(source.Skip(1));
var haves = haveNots.Select(set => element.Concat(set));
return haves.Concat(haveNots);
}
I've found your solution quite dirty so I divided the problem to be more understandable. GetPrimesUpTo returns all prime number from 2 to the number you've provided in the input, SubSetsOf returns combination of numbers that summed up equals the input number you've provided and finally the foreach in Main produces formatted output that is easy on the eye. Hope it helps!

Providing that you have collection of primes and IsPrime method
private static int[] primes = new[] {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 };
private static bool IsPrime(int value) {
return primes.Contains(value);
}
You can implement recoursive solution
private List<List<int>> ToListOfPrimes(int value, List<int> parts = null) {
if (null == parts)
parts = new List<int>();
List<List<int>> result = new List<List<int>>();
if (value == 0) {
result.Add(parts.ToList());
return result;
}
int minPrime = parts.Count <= 0 ? 0 : parts[parts.Count - 1];
if (value <= minPrime)
return result;
// not that efficient: binary search will be a better choice here
for (int i = 0; i < primes.Length; ++i) {
int p = primes[i];
if (p <= minPrime)
continue;
else if (p > value)
break;
var list = parts.ToList();
list.Add(p);
var outcome = ToListOfPrimes(value - p, list);
foreach (var solution in outcome)
result.Add(solution);
}
return result;
}
Test
var result = ToListOfPrimes(28);
string report = String.Join(Environment.NewLine, result
.Select(line => String.Join(", ", line)));
Console.Write(report);
Outcome (28)
2, 3, 5, 7, 11
2, 3, 23
2, 7, 19
3, 5, 7, 13
5, 23
11, 17
For 24
2, 3, 19
2, 5, 17
5, 19
7, 17
11, 13

If you really want to implement it in other languages just throw your solution to rubbish bin. You should be more explicit about what is happening during execution. Nested for loops with multiple if statements are not explicit at all. What's even worse in the sample - you'll need to add new for loop every time you want more numbers in the sum. I do believe it's hard to understand it for a novice, but I find recursion the only way to go here.
See for yourself:
it's hard to say why output of your program is wrong, because of the logic
Variables should be named meaningfully so you know what they store instead of blind guessing.
Your checkPrime method returns int even though you return 0 or 1 so it should really return bool type
Use debugger and a piece of paper to understand how recursion works either in my previous answer or the one provided by Dmitry Bychenko

Related

Count All Prime Numbers that Can be Formed using Digits of a Given Number

I am facing a problem while solving this task.
I should write a program which find the count of prime numbers which can be created using digits of given number, but without repetition unless digit itself repeated in given number.
For example, program should give 5 as output for number = 123. Because
{1, 2, 3, 12, 13, 21, 23, 123, 132, 213, 231, 312, 321}
has 5 prime numbers.
But if given number = 133 then program should count prime number from this list :
{1, 3, 13, 31, 33, 133, 313, 331}
Is there any way to write this program without using array? I have searched every source, but still cannot find a solution. If you have any idea, please help.
I am trying to write something like that. But it is still not working. It is not creating number as I want. One-digit numbers is a little bit closer than 3-digit when I enter 123 . Output is like that:
enter image description here
System.Console.WriteLine("Enter the number: ");
int number = Convert.ToInt32(Console.ReadLine());
int enteredNumber = number;
int length = 0;
while (number != 0)
{
length++;
number /= 10;
}
System.Console.WriteLine($"{length} length");
number = enteredNumber;
int nDigit = 1;
int count = 0;
int temp = number;
while (nDigit <= length)
{
int n = nDigit;
while (number != 0)
{
int digit = number % 10;
if (nDigit == 1 && isPrime(digit))
{
System.Console.WriteLine("1-digit prime number : " + digit);
count++;
}
else
{
int tempNewNumber = digit * Convert.ToInt32(Math.Pow(10, Convert.ToDouble(nDigit - 1)));
int newNumber = tempNewNumber;
while (temp != 0)
{
if (nDigit - 2 >= 0)
{
newNumber += (temp % 10) * Convert.ToInt32(Math.Pow(10, Convert.ToDouble(nDigit - 2)));
nDigit--;
}
if (nDigit == 1)
{
System.Console.WriteLine("in while : " + newNumber);
if (isPrime(newNumber))
{
System.Console.WriteLine("prime number : " + newNumber);
count++;
}
newNumber = tempNewNumber;
nDigit = n;
}
temp /= 10;
}
nDigit = n;
temp = enteredNumber;
}
number /= 10;
}
number = enteredNumber;
nDigit++;
}
System.Console.WriteLine("Count : " + count);
}
static bool isPrime(int num)
{
if (num <= 1) return false;
int i = 2;
while (i <= num / 2)
{
if (num % i == 0)
return false;
i++;
}
return true;
}`
You can do what you want without arrays: Identify a number by the product of the nth prime number for each digit n.
If two numbers have the same id, they are digit-wise permutations of each other.
If the id of one number p is evenly divisibly by the id of another number q, then q has fewer digits than p, but all digits of q can also found in p.
You can now test all numbers in the possible range, for example all numbers with 3 or fewer digits, whether they can be made from the given digit. This is probably not very efficient, especially for large numbers, but hey! – no arrays. (And permutations of large sets have their own performance issues.)
The example code below finds the 16 permutations of 123, but it doesn't check whether the numbers are primes.
public class Permutor
{
static uint[] prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
static uint code(uint n)
{
uint res = 1;
while (n > 0) {
res *= prime[n % 10];
n /= 10;
}
return res;
}
static uint ceiling(uint n)
{
uint c = 1;
while (n > 0) {
c *= 10;
n /= 10;
}
return c;
}
static void Main(string[] args)
{
uint num = 123;
uint id = code(num);
uint max = ceiling(num);
for (uint i = 1; i < max; i++) {
if (id % code(i) == 0) {
Console.WriteLine(i);
}
}
}
}
There's room for improvement. For example, the actual ceiling for 123 is, of course, 321, so you could try to find that from the code instead of overestimating the ciling to 1000.
When you look for primes, you don't need to visit every number. Except 2 and 3, all prime numbers are either 2*k - 1 or 2*k + 1.

Find all possible multiplications numbers to a specified sum. C#

I need help with finding all possible unordered representations for a given natural number n(breakdowns) of n as a product of natural numbers (not necessarily different).
An integer n is entered from the keyboard.
Example: input: 50 output: 25 * 2 , 10 * 5 , 5 * 5 * 2
Hint: The algorithm by which you can perform the decomposition is analogous to the one that is
described in problem 3. Instead of devNum (n-k, cnt + 1) we will recursively call devNum (n / k, cnt + 1), when
this is not for every k, but only for those for which n% k == 0. The condition for continuing
the break (loop for) will be k & gt; 1, not k ≥ 1, i.e. the bottom of the recursion will be k == 1, not
k == 0 (the latter is easily explained: 0 and 1 are precisely the identities of the operations of addition and
multiplication).
This program I wrote may be close to what you're looking for. It would be helpful if you provided more information to the problem and any answers to prior questions (like question #3) and/or examples or clues provided by the homework on how the program is modeled. I can see there are strict constraints on how to write it, and not all of those exact boundaries are followed here. However, recursion is used on the same function with the same parameters, with similar arguments passed under certain conditionals. A list of lists is used to store the different sets of factors/divisors.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
Console.WriteLine("Enter a positive integer: ");
int n;
while (!int.TryParse(Console.ReadLine(), out n))
{
Console.WriteLine("Please enter a whole number.");
}
List<List<int>> factors = FindFactors(n);
PrintFactors(factors);
}
public static List<List<int>> FindFactors(int n)
{
var res = new List<List<int>>();
if (n > 0) {
for (var i = n - 1; i >= 1; i--)
{
var temp = devNum(n, i);
foreach (var list in temp)
{
list.Reverse();
if (list.Count > 2 && list[list.Count - 1] == 1)
{
list.RemoveAt(list.Count - 1);
}
res.Add(list);
}
}
}
return res;
}
private static List<List<int>> devNum(int n, int k)
{
var beginWithFirst = new List<List<int>>();
if (n % k != 0 || (n > 1 && k == 1))
{
return beginWithFirst;
}
if (k == 1)
{
var end = new List<int>();
end.Add(k);
beginWithFirst.Add(end);
return beginWithFirst;
}
for (var i = n / k; i >= 1; i--)
{
var temp = devNum(n / k, i);
foreach (var list in temp.Where(list => list.Count > 0 && list[list.Count - 1] <= k))
{
list.Add(k);
beginWithFirst.Add(list);
}
}
return beginWithFirst;
}
public static void PrintFactors(List<List<int>> factors) {
int counter = 0;
foreach (List<int> lst in factors) {
counter++;
int[] arr = lst.ToArray();
if (counter != factors.Count) {
Console.Write("{0}", string.Join(" * ", arr) + ", ");
}
else {
Console.Write("{0}", string.Join(" * ", arr));
}
}
}
}
DotNet Fiddle

Finding an integer sum in an array of 1 000 000

Given a large list of integers (more than 1 000 000 values) find how many ways there are of selecting two of them that add up to 0.... Is the question
What I have done is create a positive random integer list:
Random pos = new Random();
int POSNO = pos.Next(1, 1000000);
lstPOS.Items.Add(POSNO);
lblPLus.Text = lstPOS.Items.Count.ToString();
POSCount++;
And created a negative list:
Random neg = new Random();
int NEGNO = neg.Next(100000, 1000000);
lstNEG.Items.Add("-" + NEGNO);
lblNegative.Text = lstNEG.Items.Count.ToString();
NegCount++;
To do the sum checking I am using:
foreach (var item in lstPOS.Items)
{
int POSItem = Convert.ToInt32(item.ToString());
foreach (var negItem in lstNEG.Items)
{
int NEGItem = Convert.ToInt32(negItem.ToString());
int Total = POSItem - NEGItem;
if (Total == 0)
{
lstADD.Items.Add(POSItem + "-" + NEGItem + "=" + Total);
lblAddition.Text = lstADD.Items.Count.ToString();
}
}
}
I know this is not the fastest route. I have considered using an array. Do you have any suggestions?
Let's see; your array is something like this:
int[] data = new int[] {
6, -2, 3, 2, 0, 0, 5, 7, 0, -2
};
you can add up to zero in two different ways:
a + (-a) // positive + negative
0 + 0 // any two zeros
in the sample above there're five pairs:
-2 + 2 (two pairs): [1] + [3] and [3] + [9]
0 + 0 (three pairs): [4] + [5], [4] + [8] and [5] + [8]
So you have to track positive/negative pairs and zeros. The implementation
Dictionary<int, int> positives = new Dictionary<int, int>();
Dictionary<int, int> negatives = new Dictionary<int, int>();
int zeros = 0;
foreach(var item in data) {
int v;
if (item < 0)
if (negatives.TryGetValue(item, out v))
negatives[item] = negatives[item] + 1;
else
negatives[item] = 1;
else if (item > 0)
if (positives.TryGetValue(item, out v))
positives[item] = positives[item] + 1;
else
positives[item] = 1;
else
zeros += 1;
}
// zeros: binomal coefficent: (2, zeros)
int result = zeros * (zeros - 1) / 2;
// positive/negative pairs
foreach (var p in positives) {
int n;
if (negatives.TryGetValue(-p.Key, out n))
result += n * p.Value;
}
// Test (5)
Console.Write(result);
Note, that there's no sorting, and dictionaries (i.e. hash tables) are used for positives and negatives so the execution time will be linear, O(n); the dark side of the implementation is that two additional structures (i.e. additional memory) required. In your case (millions integers only - Megabytes) you have that memory.
Edit: terser, but less readable Linq solution:
var dict = data
.GroupBy(item => item)
.ToDictionary(chunk => chunk.Key, chunk => chunk.Count());
int result = dict.ContainsKey(0) ? dict[0] * (dict[0] - 1) / 2 : 0;
result += dict
.Sum(pair => pair.Key > 0 && dict.ContainsKey(-pair.Key) ? pair.Value * dict[-pair.Key] : 0);
Fastest way without sorting!.
First of all you know that the sum of two integers are only 0 when they have equal absolute value but one is negative and the other is positive. So you dont need to sort. what you need is to Intersect positive list with negative list (by comparing absolute value). the result is numbers that ended up 0 sum.
Intersect has time complexity of O(n+m) where n is size of first list and m is size of second one.
private static void Main(string[] args)
{
Random random = new Random();
int[] positive = Enumerable.Range(0, 1000000).Select(n => random.Next(1, 1000000)).ToArray();
int[] negative = Enumerable.Range(0, 1000000).Select(n => random.Next(-1000000, -1)).ToArray();
var zeroSum = positive.Intersect(negative, new AbsoluteEqual());
foreach (var i in zeroSum)
{
Console.WriteLine("{0} - {1} = 0", i, i);
}
}
You also need to use this IEqualityComparer.
public class AbsoluteEqual : IEqualityComparer<int>
{
public bool Equals(int x, int y)
{
return (x < 0 ? -x : x) == (y < 0 ? -y : y);
}
public int GetHashCode(int obj)
{
return obj < 0 ? (-obj).GetHashCode() : obj.GetHashCode();
}
}
You tried to avoid check two numbers that are close (1, 2 are close, 3, 4 are close), but you didn't avoid check like (-100000, 1), (-1, 100000). Time complexity is O(n^2).
To avoid that you need to sort them first, then search from two direction.
var random = new Random();
var input = Enumerable.Range(1, 100).Select(_ => random.Next(200) - 100).ToArray();
Array.Sort(input); // This causes most computation. Time Complexity is O(n*log(n));
var expectedSum = 0;
var i = 0;
var j = input.Length - 1;
while (i < j) // This has liner time complexity O(n);
{
var result = input[i] + input[j];
if(expectedSum == result)
{
var anchori = i;
while (i < input.Length && input[i] == input[anchori] )
{
i++;
}
var anchorj = j;
while (j >= 0 && input[j] == input[anchorj])
{
j--;
}
// Exclude (self, self) combination
Func<int, int, int> combination = (n, k) =>
{
var mink = k * 2 < n ? k : n - k;
return mink == 0 ? 1
: Enumerable.Range(0, mink).Aggregate(1, (x, y) => x * (n - y))
/ Enumerable.Range(1, mink).Aggregate((x, y) => x * y);
};
var c = i < j ? (i - anchori) * (anchorj - j) : combination(i - anchori, 2);
for (int _ = 0; _ < c; _++)
{
// C# 6.0 String.Format
Console.WriteLine($"{input[anchori]}, {input[anchorj]}");
}
}
else if(result < expectedSum) {
i++;
}
else if(result > expectedSum) {
j--;
}
}
Here is another solution using (huh) LINQ. Hope the code is self explanatory
First some data
var random = new Random();
var data = new int[1000000];
for (int i = 0; i < data.Length; i++) data[i] = random.Next(-100000, 100000);
And now the solution
var result = data
.Where(value => value != int.MinValue)
.GroupBy(value => Math.Abs(value), (key, values) =>
{
if (key == 0)
{
var zeroCount = values.Count();
return zeroCount * (zeroCount - 1) / 2;
}
else
{
int positiveCount = 0, negativeCount = 0;
foreach (var value in values)
if (value > 0) positiveCount++; else negativeCount++;
return positiveCount * negativeCount;
}
})
.Sum();
Theoretically the above should have O(N) time and O(M) space complexity, where M is the count of the unique absolute values in the list.

An algorithm for a number divisible to n

At first user gives a number (n) to program, for example 5.
the program must find the smallest number that can be divided to n (5).
and this number can only consist of digits 0 and 9 not any other digits.
for example if user gives 5 to program.
numbers that can be divided to 5 are:
5, 10, 15, 20, 25, 30, ..., 85, 90, 95, ...
but 90 here is the smallest number that can be divided to 5 and also consist of digits (0 , 9). so answer for 5 must be 90.
and answer for 9 is 9, because it can be divided to 9 and consist of digit (9).
my code
string a = txtNumber.Text;
Int64 x = Convert.ToInt64(a);
Int64 i ,j=1,y=x;
bool t = false;
for (i = x + 1; t == false; i++)
{
if (i % 9 == 0 && i % 10 == 0 && i % x == 0)
{
j = i;
for (; (i /= 10) != 0; )
{
i /= 10;
if (i == 0)
t = true;
continue;
}
}
}
lblAnswer.Text = Convert.ToString(j);
If you're happy to go purely functional then this works:
Func<IEnumerable<long>> generate = () =>
{
Func<long, IEnumerable<long>> extend =
x => new [] { x * 10, x * 10 + 9 };
Func<IEnumerable<long>, IEnumerable<long>> generate2 = null;
generate2 = ns =>
{
var clean = ns.Where(n => n > 0).ToArray();
return clean.Any()
? clean.Concat(generate2(clean.SelectMany(extend)))
: Enumerable.Empty<long>();
};
return generate2(new[] { 9L, });
};
Func<long, long?> f = n =>
generate()
.Where(x => x % n == 0L)
.Cast<long?>()
.FirstOrDefault();
So rather than iterate through all possible values and test for 0 & 9 and divisibility, this just generates only numbers with 0 & 9 and then only tests for visibility. It is much faster this way.
I can call it like this:
var result = f(5L); // 90L
result = f(23L); //990909L
result = f(123L); //99999L
result = f(12321L); //90900999009L
result = f(123212L); //99909990090000900L
result = f(117238L); //990990990099990990L
result = f(1172438L); //null == No answer
These results are super fast. f(117238L) returns a result on my computer in 138ms.
You can try this way :
string a = txtNumber.Text;
Int64 x = Convert.ToInt64(a);
int counter;
for (counter = 1; !isValid(x * counter); counter++)
{
}
lblAnswer.Text = Convert.ToString(counter*x);
code above works by searching multiple of x incrementally until result that satisfy criteria : "consist of only 0 and or 9 digits" found. By searching only multiple of x, it is guaranteed to be divisible by x. So the rest is checking validity of result candidate, in this case using following isValid() function :
private static bool isValid(int number)
{
var lastDigit = number%10;
//last digit is invalid, return false
if (lastDigit != 0 & lastDigit != 9) return false;
//last digit is valid, but there is other digit(s)
if(number/10 >= 1)
{
//check validity of digit(s) before the last
return isValid(number/10);
}
//last digit is valid, and there is no other digit. return true
return true;
}
About strange empty for loop in snippet above, it is just syntactic sugar, to make the code a bit shorter. It is equal to following while loop :
counter = 1;
while(!isValid(input*counter))
{
counter++;
}
Use this simple code
int inputNumber = 5/*Or every other number, you can get this number from input.*/;
int result=1;
for (int i = 1; !IsOk(result,inputNumber); i++)
{
result = i*inputNumber;
}
Print(result);
IsOk method is here:
bool IsOk(int result, int inputNumber)
{
if(result%inputNumber!=0)
return false;
if(result.ToString().Replace("9",string.Empty).Replace("0",string.Empty).Length!=0)
return false;
return true;
}
My first solution has very bad performance, because of converting a number to string and looking for characters '9' and '0'.
New solution:
My new solution has very good performance and is a technical approach since of using Breadth-first search(BFS).
Algorithm of this solution:
For every input number, test 9, if it is answer print it, else add 2 child numbers (90 & 99) to queue, and continue till finding answer.
int inputNumber = 5;/*Or every other number, you can get this number from input.*/
long result;
var q = new Queue<long>();
q.Enqueue(9);
while (true)
{
result = q.Dequeue();
if (result%inputNumber == 0)
{
Print(result);
break;
}
q.Enqueue(result*10);
q.Enqueue(result*10 + 9);
}
Trace of number creation:
9
90,99
900,909,990,999
9000,9009,9090,9099,9900,9909,9990,9999
.
.
.
I wrote this code for console, and i used goto command however it is not prefered but i could not write it with only for.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace main
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter your number");
Int64 x = Convert.ToInt64(Console.ReadLine());
Int64 y, j, i, k, z = x;
x = x + 5;
loop:
x++;
for (i = 0, y = x; y != 0; i++)
y /= 10;
for (j = x, k = i; k != 0; j /= 10, k--)
{
if (j % 10 != 9)
if (j % 10 != 0)
goto loop;
}
if (x % z != 0)
goto loop;
Console.WriteLine("answer:{0}",x);
Console.ReadKey();
}
}
}

Finding all numbers that sum to a specified target number (integer partition)

First i want to say i'm still learning so my programming skills are not very good, and i'm willing to accept any advice you have.
Second i'm still learning english so i want to say sorry for the inconvenience.
Well my problem is this, i need help improving my algorithm or any information about it, i don't know what words use for searching this.
The algorithm is supposed to find all the combinations of numbers that added is equal to a given number. Example: if i have the number 6 my results are supposed to be: [1,5],[2,4],[2,2,2],[3,3]
i have the following:
public List<List<int>> discompose(int number)
{
List<List<int>> discomposedPairs = new List<List<int>>();
if (number <= 3)
return discomposedPairs;
int[] numsForCombine = new int[number-1];
for(int i = 1; i < number;i++){
numsForCombine[i - 1] = i;
}
int ini = 0;
int end = numsForCombine.Length - 1;
while (ini <= end)
{
List<int> pair = new List<int>();
pair.Add(numsForCombine[ini++]);
pair.Add(numsForCombine[end--]);
discomposedPairs.Add(pair);
}
return discomposedPairs;
}
public List<List<int>> discomposePair(List<int> pair)
{
List<List<int>> parDisc = new List<List<int>>();
for (int i = 0; i < pair.Count; i++) {
if (pair[i] > 3)
{
List<List<int>> disc = discomposeList(discompose(pair[i]));
foreach (List<int> item in disc)
{
if (i > 0)
{
var temp = pair.GetRange(0, i);
temp.AddRange(item);
parDisc.Add(temp);
} else {
item.AddRange(pair.GetRange(i+1, pair.Count-(i+1)));
parDisc.Add(item);
}
}
}
}
return parDisc;
}
public List<List<int>> discomposeList(List<List<int>> list)
{
List<List<int>> mainDiscomposedList = new List<List<int>>();
for (int i = 0; i < list.Count; i++)
{
mainDiscomposedList.Add(list[i]);
List<List<int>> discomposedSubset = discomposePair(list[i]);
foreach(List<int> item in discomposedSubset){
mainDiscomposedList.Add(item);
}
}
return mainDiscomposedList;
}
The first method descompose the number given in pairs that added are equal the number given.
The second and the third method are the ugliest they are recursive and have bucles so they don't have any good performance. Between the two generates a List with numbers as i the problem says but there are a few inconvenients:
1) Don't have good performance
2) Gives a lot of repetitive sequences
here is the output for the number 10
[2,8,]
[2,2,6,]
[2,2,2,4,]
[2,2,2,2,2,]
[2,2,3,3,]
[2,3,5,]
[2,3,2,3,]<-------------repeated
[2,4,4,]
[2,2,2,4,]<-------------repeated
[2,4,2,2,]<-------------repeated
[3,7,]
[3,2,5,]<-------------repeated
[3,2,2,3,]<-------------repeated
[3,3,4,]
[3,3,2,2,]
[4,6,]
[2,2,6,]<-------------repeated
[4,2,4,]<-------------repeated
[4,2,2,2,]<-------------repeated
[4,3,3,]<-------------repeated
[5,5,]
[2,3,5,]<-------------repeated
[5,2,3,]<-------------repeated
Finally i want to improve the performance and have the less possible repeated items being a repeated item [1,1,3], [1,3,1], [3,1,1]
[Here is the full project link]1
Here's one approach that first builds the combinations into a tree structure, then arranges them into lists of ints:
internal class Combination
{
internal int Num;
internal IEnumerable<Combination> Combinations;
}
internal static IEnumerable<Combination> GetCombinationTrees(int num, int max)
{
return Enumerable.Range(1, num)
.Where(n => n <= max)
.Select(n =>
new Combination
{
Num = n,
Combinations = GetCombinationTrees(num - n, n)
});
}
internal static IEnumerable<IEnumerable<int>> BuildCombinations(
IEnumerable<Combination> combinations)
{
if (combinations.Count() == 0)
{
return new[] { new int[0] };
}
else
{
return combinations.SelectMany(c =>
BuildCombinations(c.Combinations)
.Select(l => new[] { c.Num }.Concat(l))
);
}
}
public static IEnumerable<IEnumerable<int>> GetCombinations(int num)
{
var combinationsList = GetCombinationTrees(num, num);
return BuildCombinations(combinationsList);
}
public static void PrintCombinations(int num)
{
var allCombinations = GetCombinations(num);
foreach (var c in allCombinations)
{
Console.WriteLine(string.Join(", ", c));
}
}
When run with the input value 6, this produces:
1, 1, 1, 1, 1, 1
2, 1, 1, 1, 1
2, 2, 1, 1
2, 2, 2
3, 1, 1, 1
3, 2, 1
3, 3
4, 1, 1
4, 2
5, 1
6

Categories

Resources