c# - Sum of Numbers as Distinct Primes -
//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 number"); //n = convert.toint32 (console.readline()); //list <int> primes = new list <int>(); //list handle numbers //hashset <int> myprimes = new hashset <int> (primes); system.console.writeline ("enter 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 {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 avoid dupes if (n <= 0 && <= 0 && j <= 0){ console.write("{0}\n",n); } } } } } public static int checkprime(int n){ int i, j, flag = 1; (i = 2; i<=(math.sqrt(n)); i++){ (j = 2; j<=(math.sqrt(n)); j++){ if (n%i == 0 && n%j == 0 ){ //even number check i++; j++; flag = 0; } } } return flag; } }
so have been experimenting while now. cant seem print possible solutions. example 24 able print 7+17 not 2+5+17. there answers being repeated , might have fact dont have duplicate checks. tried push integers in list , use hashset have distinct integers got stuck , tried brute force it. numbers printed supposed distinct prime integers. dont understand how print distinct numbers , there elegant way print out possible.
thanks help!
don't know if it's elegant enough you, i've mashed dirty way make work:
static void main() { console.writeline("enter 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) => + " + " + n) + " = " + numbertosum; console.writeline(formatoperation); } console.readline(); } public static ienumerable<int> getprimesupto(int end) { var primes = new hashset<int>(); (var = 2; <= 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 solution quite dirty divided problem more understandable. getprimesupto returns prime number 2 number you've provided in input, subsetsof returns combination of numbers summed equals input number you've provided , foreach in main produces formatted output easy on eye. hope helps!
Comments
Post a Comment