Create optional numbers from 3 inserted positive digits - c#

I'm just struggled with question about that:
I need to write a program that get 3 positive digits from the user and print all the 3 digits numbers that can be created from them. I'm not allowed to use recursion.. any ideas?
thanks

Providing that a, b, c are given digits, e.g.
int a = 1;
int b = 2;
int c = 3;
the implementation (C#) could be
String report = String.Join(Environment.NewLine,
new HashSet<int>() {
100 * a + 10 * b + c,
100 * a + 10 * c + b,
100 * b + 10 * a + c,
100 * b + 10 * c + a,
100 * c + 10 * a + b,
100 * c + 10 * b + a,
});
Console.Write(report);
The output is
123
132
213
231
312
321
Note, that for (a = 1, b = 2 and c = 1) you'll get only
121
112
211
I doubt if this solution will be accepted by your professor (even if it doesn't have any recursion), but you can use it as a test when elaborating your own routine.

Related

How to find variable from Math.Round statement

We have a calculation in code that calculates some adjusted value like this
a = Math.Round(b, 3) + Math.Round(b * c1, 3) + Math.Round(b * c2, 3);
The problem is that now I need to do reversed calculation. I have values for a,c1& c2 and need to find value of b. Is that possible?
Assuming b, c1 and c2 are all positive, you have an increasing function so you can binary search over it to find b. There may exist a formula to get b directly, but this should work fast enough as well. Here is a python example:
c1 = 0.0125
c2 = 0.0517
a = 0.155
def op(b, c1, c2):
return round(b, 3) + round(b * c1, 3) + round(b * c2, 3)
minb = 0
maxb = a
while (minb + 0.00001 < maxb):
b = (minb + maxb) / 2
estimate = op(b, c1, c2)
if estimate > a:
maxb = b
elif estimate < a:
minb = b
else:
break
print(b, op(b, c1, c2))

How would i work out magnitude quickly for 3 values?

How can I use a Fast Magnitude calculation for 3 values (instead of using square root)? (+/- 3% is good enough)
public void RGBToComparison(Color32[] color)
{
DateTime start = DateTime.Now;
foreach (Color32 i in color)
{
var r = PivotRgb(i.r / 255.0);
var g = PivotRgb(i.g / 255.0);
var b = PivotRgb(i.b / 255.0);
var X = r * 0.4124 + g * 0.3576 + b * 0.1805;
var Y = r * 0.2126 + g * 0.7152 + b * 0.0722;
var Z = r * 0.0193 + g * 0.1192 + b * 0.9505;
var LB = PivotXyz(X / 95.047);
var AB = PivotXyz(Y / 100);
var BB = PivotXyz(Z / 108.883);
var L = Math.Max(0, 116 * AB - 16);
var A = 500 * (LB - AB);
var B = 200 * (AB - BB);
totalDifference += Math.Sqrt((L-LT)*(L-LT) + (A-AT)*(A-AT) + (B-BT)*(B-BT));
}
totalDifference = totalDifference / color.Length;
text.text = "Amount of Pixels: " + color.Length + " Time(MilliSeconds):" + DateTime.Now.Subtract(start).TotalMilliseconds + " Score (0 to 100)" + (totalDifference).ToString();
RandomOrNot();
}
private static double PivotRgb(double n)
{
return (n > 0.04045 ? Math.Pow((n + 0.055) / 1.055, 2.4) : n / 12.92) * 100.0;
}
private static double PivotXyz(double n)
{
return n > 0.008856 ? CubicRoot(n) : (903.3 * n + 16) / 116;
}
private static double CubicRoot(double n)
{
return Math.Pow(n, 1.0 / 3.0);
}
This is the important part: totalDifference += Math.Sqrt((L-LT)*(L-LT) + (A-AT)*(A-AT) + (B-BT)*(B-BT));
I know there are FastMagnitude calculations online, but all the ones online are for two values, not three. For example, could i use the difference between the values to get a precise answer? (By implementing the difference value into the equation, and if the difference percentage-wise is big, falling back onto square root?)
Adding up the values and iterating the square root every 4 pixels is a last resort that I could do. But firstly, I want to find out if it is possible to have a good FastMagnitude calculation for 3 values.
I know I can multi-thread and parllelize it, but I want to optimize my code before I do that.
If you just want to compare the values, why not leave the square root out and work with the length squared?
Or use the taylor series of the square root of 1+x and cut off early :)

x++ operator for playing gif animations: How do it work together in this example? [duplicate]

I have next code
int a,b,c;
b=1;
c=36;
a=b%c;
What does "%" operator mean?
It is the modulo (or modulus) operator:
The modulus operator (%) computes the remainder after dividing its first operand by its second.
For example:
class Program
{
static void Main()
{
Console.WriteLine(5 % 2); // int
Console.WriteLine(-5 % 2); // int
Console.WriteLine(5.0 % 2.2); // double
Console.WriteLine(5.0m % 2.2m); // decimal
Console.WriteLine(-5.2 % 2.0); // double
}
}
Sample output:
1
-1
0.6
0.6
-1.2
Note that the result of the % operator is equal to x – (x / y) * y and that if y is zero, a DivideByZeroException is thrown.
If x and y are non-integer values x % y is computed as x – n * y, where n is the largest possible integer that is less than or equal to x / y (more details in the C# 4.0 Specification in section 7.8.3 Remainder operator).
For further details and examples you might want to have a look at the corresponding Wikipedia article:
Modulo operation (on Wikipedia)
% is the remainder operator in many C-inspired languages.
3 % 2 == 1
789 % 10 = 9
It's a bit tricky with negative numbers. In e.g. Java and C#, the result has the same sign as the dividend:
-1 % 2 == -1
In e.g. C++ this is implementation defined.
See also
Wikipedia/Modulo operation
References
MSDN/C# Language Reference/% operator
That is the Modulo operator. It will give you the remainder of a division operation.
It's the modulus operator. That is, 2 % 2 == 0, 4 % 4 % 2 == 0 (2, 4 are divisible by 2 with 0 remainder), 5 % 2 == 1 (2 goes into 5 with 1 as remainder.)
It is the modulo operator. i.e. it the remainder after division 1 % 36 == 1 (0 remainder 1)
That is the modulo operator, which finds the remainder of division of one number by another.
So in this case a will be the remainder of b divided by c.
It's is modulus, but you example is not a good use of it. It gives you the remainder when two integers are divided.
e.g. a = 7 % 3 will return 1, becuase 7 divided by 3 is 2 with 1 left over.
It is modulus operator
using System;
class Test
{
static void Main()
{
int a = 2;
int b = 6;
int c = 12;
int d = 5;
Console.WriteLine(b % a);
Console.WriteLine(c % d);
Console.Read();
}
}
Output:
0
2
is basic operator available in almost every language and generally known as modulo operator.
it gives remainder as result.
Okay well I did know this till just trying on a calculator and playing around so basically:
5 % 2.2 = 0.6 is like saying on a calculator 5/2.2 = 2.27 then you multiply that .27 times the 2.27 and you round and you get 0.6. Hope this helps, it helped me =]
Nobody here has provided any examples of exactly how an equation can return different results, such as comparing 37/6 to 37%6, and before some of you get upset thinking that you did, pause for a moment and think about it for a minute, according to Dirk Vollmar in here the int x % int y parses as (x - (x / y) * y) which seems fairly straightforward at first glance, but not all Math is performed in the same order.
Since not every equation has it's proper brackets, some Schools will teach that the Equation is to be parsed as ((x - (x / y)) * y) whilst other Schools teach (x - ((x / y) * y)).
Now I experimented with my example (37/6 & 37%6) and figured out which selection was intended (it's (x - ((x / y) * y))), and I even displayed a nicely built if Loop (even though I forgot every End of Line Semicolon) to simulate the Divide Equation at the most Fundamental Scale, as that was in fact my point, the Equation is similar, yet Fundamentally different.
Here's what I can remember from my deleted Post (this took me around an Hour to Type from my Phone, indents are Double Spaced)
using System;
class Test
{
static void Main()
{
float exact0 = (37 / 6); //6.1666e∞
float exact1 = (37 % 6); //1
float exact2 = (37 - (37 / 6) * 6); //0
float exact3 = ((37 - (37 / 6)) * 6); //0
float exact4 = (37 - ((37 / 6) * 6)); //185
int a = 37;
int b = 6;
int c = 0;
int d = a;
int e = b;
string Answer0 = "";
string Answer1 = "";
string Answer2 = "";
string Answer0Alt = "";
string Answer1Alt = "";
string Answer2Alt = "";
Console.WriteLine("37/6: " + exact0);
Console.WriteLine("37%6: " + exact1);
Console.WriteLine("(37 - (37 / 6) * 6): " + exact2);
Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3);
Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4);
Console.WriteLine("a: " + a + ", b: " + b + ", c: " + c + ", d: " + d + ", e: " + e);
Console.WriteLine("Answer0: " + Answer0);
Console.WriteLine("Answer0Alt: " + Answer0Alt);
Console.WriteLine("Answer1: " + Answer1);
Console.WriteLine("Answer0Alt: " + Answer1Alt);
Console.WriteLine("Answer2: " + Answer2);
Console.WriteLine("Answer2Alt: " + Answer2Alt);
Console.WriteLine("Init Complete, starting Math...");
Loop
{
if (a !< b) {
a - b;
c +1;}
if else (a = b) {
a - b;
c +1;}
else
{
String Answer0 = c + "." + a; //6.1
//this is = to 37/6 in the fact that it equals 6.1 ((6*6=36)+1=37) or 6 remainder 1,
//which according to my Calculator App is technically correct once you Round Down the .666e∞
//which has been stated as the default behavior of the C# / Operand
//for completion sake I'll include the alternative answer for Round Up also
String Answer0Alt = c + "." + (a + 1); //6.2
Console.WriteLine("Division Complete, Continuing...");
Break
}
}
string Answer1 = ((d - (Answer0)) * e); //185.4
string Answer1Alt = ((d - (Answer0Alt​)) * e); // 184.8
string Answer2 = (d - ((Answer0) * e)); //0.4
string Answer2Alt = (d - ((Answer0Alt​) * e)); //-0.2
Console.WriteLine("Math Complete, Summarizing...");
Console.WriteLine("37/6: " + exact0);
Console.WriteLine("37%6: " + exact1);
Console.WriteLine("(37 - (37 / 6) * 6): " + exact2);
Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3);
Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4);
Console.WriteLine("Answer0: " + Answer0);
Console.WriteLine("Answer0Alt: " + Answer0Alt);
Console.WriteLine("Answer1: " + Answer1);
Console.WriteLine("Answer0Alt: " + Answer1Alt);
Console.WriteLine("Answer2: " + Answer2);
Console.WriteLine("Answer2Alt: " + Answer2Alt);
Console.Read();
}
}
This also CLEARLY demonstrated how an outcome can be different for the exact same Equation.

What does the '%' operator mean?

I have next code
int a,b,c;
b=1;
c=36;
a=b%c;
What does "%" operator mean?
It is the modulo (or modulus) operator:
The modulus operator (%) computes the remainder after dividing its first operand by its second.
For example:
class Program
{
static void Main()
{
Console.WriteLine(5 % 2); // int
Console.WriteLine(-5 % 2); // int
Console.WriteLine(5.0 % 2.2); // double
Console.WriteLine(5.0m % 2.2m); // decimal
Console.WriteLine(-5.2 % 2.0); // double
}
}
Sample output:
1
-1
0.6
0.6
-1.2
Note that the result of the % operator is equal to x – (x / y) * y and that if y is zero, a DivideByZeroException is thrown.
If x and y are non-integer values x % y is computed as x – n * y, where n is the largest possible integer that is less than or equal to x / y (more details in the C# 4.0 Specification in section 7.8.3 Remainder operator).
For further details and examples you might want to have a look at the corresponding Wikipedia article:
Modulo operation (on Wikipedia)
% is the remainder operator in many C-inspired languages.
3 % 2 == 1
789 % 10 = 9
It's a bit tricky with negative numbers. In e.g. Java and C#, the result has the same sign as the dividend:
-1 % 2 == -1
In e.g. C++ this is implementation defined.
See also
Wikipedia/Modulo operation
References
MSDN/C# Language Reference/% operator
That is the Modulo operator. It will give you the remainder of a division operation.
It's the modulus operator. That is, 2 % 2 == 0, 4 % 4 % 2 == 0 (2, 4 are divisible by 2 with 0 remainder), 5 % 2 == 1 (2 goes into 5 with 1 as remainder.)
It is the modulo operator. i.e. it the remainder after division 1 % 36 == 1 (0 remainder 1)
That is the modulo operator, which finds the remainder of division of one number by another.
So in this case a will be the remainder of b divided by c.
It's is modulus, but you example is not a good use of it. It gives you the remainder when two integers are divided.
e.g. a = 7 % 3 will return 1, becuase 7 divided by 3 is 2 with 1 left over.
It is modulus operator
using System;
class Test
{
static void Main()
{
int a = 2;
int b = 6;
int c = 12;
int d = 5;
Console.WriteLine(b % a);
Console.WriteLine(c % d);
Console.Read();
}
}
Output:
0
2
is basic operator available in almost every language and generally known as modulo operator.
it gives remainder as result.
Okay well I did know this till just trying on a calculator and playing around so basically:
5 % 2.2 = 0.6 is like saying on a calculator 5/2.2 = 2.27 then you multiply that .27 times the 2.27 and you round and you get 0.6. Hope this helps, it helped me =]
Nobody here has provided any examples of exactly how an equation can return different results, such as comparing 37/6 to 37%6, and before some of you get upset thinking that you did, pause for a moment and think about it for a minute, according to Dirk Vollmar in here the int x % int y parses as (x - (x / y) * y) which seems fairly straightforward at first glance, but not all Math is performed in the same order.
Since not every equation has it's proper brackets, some Schools will teach that the Equation is to be parsed as ((x - (x / y)) * y) whilst other Schools teach (x - ((x / y) * y)).
Now I experimented with my example (37/6 & 37%6) and figured out which selection was intended (it's (x - ((x / y) * y))), and I even displayed a nicely built if Loop (even though I forgot every End of Line Semicolon) to simulate the Divide Equation at the most Fundamental Scale, as that was in fact my point, the Equation is similar, yet Fundamentally different.
Here's what I can remember from my deleted Post (this took me around an Hour to Type from my Phone, indents are Double Spaced)
using System;
class Test
{
static void Main()
{
float exact0 = (37 / 6); //6.1666e∞
float exact1 = (37 % 6); //1
float exact2 = (37 - (37 / 6) * 6); //0
float exact3 = ((37 - (37 / 6)) * 6); //0
float exact4 = (37 - ((37 / 6) * 6)); //185
int a = 37;
int b = 6;
int c = 0;
int d = a;
int e = b;
string Answer0 = "";
string Answer1 = "";
string Answer2 = "";
string Answer0Alt = "";
string Answer1Alt = "";
string Answer2Alt = "";
Console.WriteLine("37/6: " + exact0);
Console.WriteLine("37%6: " + exact1);
Console.WriteLine("(37 - (37 / 6) * 6): " + exact2);
Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3);
Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4);
Console.WriteLine("a: " + a + ", b: " + b + ", c: " + c + ", d: " + d + ", e: " + e);
Console.WriteLine("Answer0: " + Answer0);
Console.WriteLine("Answer0Alt: " + Answer0Alt);
Console.WriteLine("Answer1: " + Answer1);
Console.WriteLine("Answer0Alt: " + Answer1Alt);
Console.WriteLine("Answer2: " + Answer2);
Console.WriteLine("Answer2Alt: " + Answer2Alt);
Console.WriteLine("Init Complete, starting Math...");
Loop
{
if (a !< b) {
a - b;
c +1;}
if else (a = b) {
a - b;
c +1;}
else
{
String Answer0 = c + "." + a; //6.1
//this is = to 37/6 in the fact that it equals 6.1 ((6*6=36)+1=37) or 6 remainder 1,
//which according to my Calculator App is technically correct once you Round Down the .666e∞
//which has been stated as the default behavior of the C# / Operand
//for completion sake I'll include the alternative answer for Round Up also
String Answer0Alt = c + "." + (a + 1); //6.2
Console.WriteLine("Division Complete, Continuing...");
Break
}
}
string Answer1 = ((d - (Answer0)) * e); //185.4
string Answer1Alt = ((d - (Answer0Alt​)) * e); // 184.8
string Answer2 = (d - ((Answer0) * e)); //0.4
string Answer2Alt = (d - ((Answer0Alt​) * e)); //-0.2
Console.WriteLine("Math Complete, Summarizing...");
Console.WriteLine("37/6: " + exact0);
Console.WriteLine("37%6: " + exact1);
Console.WriteLine("(37 - (37 / 6) * 6): " + exact2);
Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3);
Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4);
Console.WriteLine("Answer0: " + Answer0);
Console.WriteLine("Answer0Alt: " + Answer0Alt);
Console.WriteLine("Answer1: " + Answer1);
Console.WriteLine("Answer0Alt: " + Answer1Alt);
Console.WriteLine("Answer2: " + Answer2);
Console.WriteLine("Answer2Alt: " + Answer2Alt);
Console.Read();
}
}
This also CLEARLY demonstrated how an outcome can be different for the exact same Equation.

Can someone abuse LINQ and solve this money puzzle?

For the fun of it, I would like to see someone use and abuse LINQ to solve this money problem.
I really have no idea how you would do it - I suppose Populating some set and then selecting off of it.
If given a total number of coins and give the total amount of all coins added together:
Show every possible combination of coins. Coins are Quarters(.25), Dimes(.10) Nickels(.05) and Pennies(.01)
Include the option so that there can be zero of a type of coin or there must be at least 1 of each.
Sample Problem: If I have 19 coins and the coins add up to $1.56 and there must be at least 1 of every type of coin.
The answer would be:
1 Quarters, 9 Dimes, 8 Nickels, 1 Pennies
2 Quarters, 5 Dimes, 11 Nickels, 1 Pennies
2 Quarters, 9 Dimes, 2 Nickels, 6 Pennies
3 Quarters, 1 Dimes, 14 Nickels, 1 Pennies
3 Quarters, 5 Dimes, 5 Nickels, 6 Pennies
4 Quarters, 1 Dimes, 8 Nickels, 6 Pennies
5 Quarters, 1 Dimes, 2 Nickels, 11 Pennies
And if we allowed zero for a coint we allowed get an additional
0 Quarters, 13 Dimes, 5 Nickels, 1 Pennies
Here is some sample C# code using a brute force method to solve the problem.
Don't bother improving the sample, let's just see a solution using Linq.
//Try not to use any regualar c# looping code if possible.
private void SolveCoinProblem(int totalNumberOfCoins, double totalAmount, int minimumNumberOfEachCoin)
{
int foundCount = 0;
long numberOfTries = 0;
Console.WriteLine(String.Format("Solving Coin Problem:TotalNumberOfCoins={0}TotalAmount={1}MinimumNumberOfEachCoin{2}", totalNumberOfCoins, totalAmount, minimumNumberOfEachCoin));
for (int totalQuarters = minimumNumberOfEachCoin; totalQuarters < totalNumberOfCoins; totalQuarters++)
{
for (int totalDimes = minimumNumberOfEachCoin; totalDimes < totalNumberOfCoins; totalDimes++)
{
for (int totalNickels = minimumNumberOfEachCoin; totalNickels < totalNumberOfCoins; totalNickels++)
{
for (int totalPennies = minimumNumberOfEachCoin; totalPennies < totalNumberOfCoins; totalPennies++)
{
numberOfTries++;
if (totalQuarters + totalDimes + totalNickels + totalPennies == totalNumberOfCoins)
{
if (Math.Round((totalQuarters * .25) + (totalDimes * .10) + (totalNickels * .05) + (totalPennies * .01),2) == totalAmount)
{
Console.WriteLine(String.Format("{0} Quarters, {1} Dimes, {2} Nickels, {3} Pennies", totalQuarters, totalDimes, totalNickels, totalPennies));
foundCount++;
}
}
}
}
}
}
Console.WriteLine(String.Format("{0} Combinations Found. We tried {1} combinations.", foundCount, numberOfTries));
}
Untested, but:
int minQuarters = 1, minDimes = 1,
minNickels = 1, minPennies = 1,
maxQuarters = 19, maxDimes = 19,
maxNickels = 19, maxPennies = 19,
coinCount = 19, total = 156;
var qry = from q in Enumerable.Range(minQuarters, maxQuarters)
from d in Enumerable.Range(minDimes, maxDimes)
from n in Enumerable.Range(minNickels, maxNickels)
from p in Enumerable.Range(minPennies, maxPennies)
where q + d + n + p == coinCount
where q * 25 + d * 10 + n * 5 + p == total
select new {q,d,n,p};
foreach (var row in qry)
{
Console.WriteLine("{0} quarter(s), {1} dime(s), {2} nickel(s) and {3} pennies",
row.q, row.d, row.n, row.p);
}
Actually, for retail purposes - perhaps a better query is "what is the fewest coins I can give out"? Replace with:
...
from p in Enumerable.Range(minPennies, maxPennies)
where q + d + n + p <= coinCount
where q * 25 + d * 10 + n * 5 + p == total
orderby q + d + n + p
...
and use either First() or Take(...) ;-p
You could also probably reduce the number of checked cases by subtracting (for example) q in the maxDimes test (and so on...) - something like (simplified):
int minCount = 1,
coinCount = 19, total = 156;
var qry = from q in Enumerable.Range(minCount, coinCount - (3 * minCount))
where q * 25 <= total
from d in Enumerable.Range(minCount, coinCount - (q + (2 * minCount)))
where q * 25 + d * 10 <= total
from n in Enumerable.Range(minCount, coinCount - (q + d + minCount))
where q * 25 + d * 10 + n * 5 <= total
from p in Enumerable.Range(minCount, coinCount - (q + d + n))
where q + d + n + p == coinCount
where q * 25 + d * 10 + n * 5 + p == total
select new { q, d, n, p };

Categories