I have a product that sells in a strange way.
The formula to get the current price is:
total_products_sold * 0.05
I need a function that will return the total sales value, given a total_products_sold value.
This needs to figure out all past pricing, using the above formula.
e.g. if I sold 3 products, the total sales amount is:
1 * 0.5 + 2 * 0.5 + 3 * 0.5 = .30
I can't remember the formula, if its a factorial issue or a exponential type equation.
Formula is
total(N) =
1 * 0.5 + 2 * 0.5 + ... + N * 0.5 =
(1 + 2 + ... + N) * 0.5 = (check this link)
((N + 1) * N / 2) * 0.5
function could be something like
float total (int products) {
return (products + 1) * products / 2 * 0.5;
}
Related
I am trying to write a code in C# that prints out the total of dollars and cents I have. I have the first part done, but the end of the code won't show how much cents there is when I run the whole code, it only shows a whole number (such as $1), when I need the code to show something like $1.56. What am I missing in the program in order to show this?
Console.WriteLine("Enter number of quarters: ");
int quarters = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter number of dimes: ");
int dimes = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter number of nickels: ");
int nickels = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter number of pennies: ");
int pennies = Convert.ToInt32(Console.ReadLine());
double dollars = (int)(((quarters * 0.25) + (dimes * 0.10) + (nickels * 0.05) + (pennies * 0.01)) % 1 * 100);
Console.WriteLine("Your total is $" + dollars);
When I typed in 4 quarters, 0 dimes, 0 nickels, and 1 penny, it shows the result as $1 not $1.01
You are forcing the result as int, so the decimals are cut
double dollars = (int)(((quarters * 0.25) + (dimes * 0.10) + (nickels * 0.05) + (pennies * 0.01)) % 1 * 100);
Do not force to int, on the contrary, force to double
double dollars = ((quarters * 0.25) + (dimes * 0.10) + (nickels * 0.05) + (pennies * 0.01)) % 1 * 100;
But, since you are handling money, I would recommend to handle everything with decimal
decimal dollars = ((quarters * 0.25) + (dimes * 0.10) + (nickels * 0.05) + (pennies * 0.01)) % 1 * 100;
Better explained in dotnet perls
Decimal accurately stores numeric data. The .NET Framework offers this
type for programs where rounding errors are harmful. Decimal stores
large and small numbers with many digits after the decimal place.
EDIT
When I put in decimal dollars instead of double dollars, I then get
the error, "Cannot implicitly convert type 'double' to 'decimal'. An
explicit conversion exists (are you missing a cast?)
It is becuse you need all the operation of the same type, you can do
int quarters =1;
int dimes = 1;
int nickels = 1;
int pennies = 1;
decimal dollars = ((quarters * 0.25m) + (dimes * 0.10m) + (nickels * 0.05m) + (pennies * 0.01m)) % 1 * 100;
Console.WriteLine("Your total is $" + dollars);
Note the use of a m after the number, this is to tell the compiler that the type is decimal or Money, not double.
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 :)
So, as the title says I need to calculate the APR for a loan. I have the following:
apr = (rate * ((Math.Pow((1 + rate), duration))) /
((Math.Pow((1 + rate), duration)) - 1)) -
(installment / (loanamount - extracost));
But its not returning the correct value. I also tried another version of that equation with even worse results:
apr = ((loanamount + extracost) * rate * Math.Pow((1 + rate), duration)) / (Math.Pow((1 + rate),duration) - 1);
The calculations are all wrong. I tried adjusting some parenthesis and checking the order of operations. Any help would be greatly appreciated!
Maybe it sounds like cheating, but I just used the existing VB financial library .NET has. Its for annuities so you have to make the payment a negative number.
Term is full number of payments(i.e. 15 year mortgage has 180 payments so 180)
Payment is payment per (so monthly payment amt... x -1)
Starting Amount = original loan amount with no fees
double apr = Microsoft.VisualBasic.Financial.Rate(term,-payment,originalLoanAmt)
Then just multiply by the answer by the annual # of payments (if monthly then 12) or if you want the % expressed as a number like 3.25%, then multiply by 100 as well.
**note you have to add a reference to Microsoft.VisualBasic assembly.
You're calculating the payment amount given duration as the number of payments. That's not the APR. Here's what I'm seeing:
double apr, loanamount = 3000d, extracost = 7000d, rate = 0.05;
int duration = 1;
apr = ((loanamount + extracost) * rate * Math.Pow((1 + rate), duration)) / (Math.Pow((1 + rate),duration) - 1);
Console.WriteLine(apr);
duration = 2;
apr = ((loanamount + extracost) * rate * Math.Pow((1 + rate), duration)) / (Math.Pow((1 + rate),duration) - 1);
Console.WriteLine(apr);
duration = 3;
apr = ((loanamount + extracost) * rate * Math.Pow((1 + rate), duration)) / (Math.Pow((1 + rate),duration) - 1);
Console.WriteLine(apr);
where the output is
10500
5378.0487804878
3672.08564631245
Only the first answer is the same as what you might expect from A=Pe^(rt) or similar calculations of the amount from the principal.
The actual calculation you need depends on both the legal jurisdiction and the type of credit/loan though.
I use 'Stimulsoft Reports.Net' to calculate a total price based on multiple items with different prices, ammount and tax-rates. This is my code:
{
Sum
(
DataBandTax,
(
Positions.UnitPrice *
Positions.Amount *
(
Positions.Article.TaxRate +
100
)
) /
100
)
}
Positions is a Bussines-Object, each object of Positions has exactly one Article.
While Positions.UnitPrice and Positions.Amount are multiplied correctly, Stimulsoft uses for every calculation the same Positions.Article.TaxRate, instead the TaxRate which fits to its Position. For example:
Position | UnitPrice | Amount | TaxRate
1 | 100 | 3 | 5
2 | 50 | 10 | 10
3 | 20 | 5 | 3
So the calculation should be:
((100 * 3 * (5 + 100)) / 100)
+ ((50 * 10 * (10 + 100)) / 100)
+ ((20 * 5 * (3 + 100)) / 100)
= 315 + 550 + 103
= 968
Instead stimulsoft calculates this: (In this example it uses only the TaxRate of Position 1)
((100 * 3) + (50 * 10) + (20 * 5))
* (1+(5/100))
= (300 + 200 + 100) * 1.05
= 600 * 1.05
= 630
How do I stop stimulsoft from doing so?
I was able to solve this issue, by placing the TaxRate inside Positions (instead of Positions.Article). Seems like Stimulsoft has a problem when a sum includes object-values and sub-object-values at the same time.
{
Sum
(
DataBandTax,
(
Positions.UnitPrice *
Positions.Amount *
(
Positions.TaxRate +
100
)
) /
100
)
}
Let's say I have a range of two values:
5...........98
and let's assume the user position's the slider at value 40
Now I want to get the value from another range of values at the exact percentage position as from range 1
let's say the second range of values are 10.........80
int nRange1 = 98 - 5;
int nRange2 = 80 - 10;
int nValue1 = 40;
int nPercentOnRange1 = ((nValue1 - 5) / nRange1)*100;
Now I have to get the value from Range2 at the exact percentage as nPercentOnRange1, but I don't know how
First need to find % from first range and apply that % to new range.
Here is what I will do:
Range1(A to B) Selected value: c
Range2(E to F)
Range1 % = (C-A) / (B-A) * 100
Range 2 corresponding value = ((F - E) * (Range 1 %) / 100) + E
C#:
int Range1Min = 5, Range1Max=90, Range1SelectedValue = 40;
int Range2Min = 6, Range2Max=80;
decimal range1Percent = (Range1SelectedValue-Range1Min ) / (Range1Max-Range1Min) * 100.0
decimal range2NewValue = (Range2Max - Range2Min) * range1Percent / 100 + Range2Min;
Watch out for
int nPercentOnRange1 = ((nValue1 - 5)/ nRange1) * 100;
ending up as zero since nValue1 and nRange1 are integers. This might be better:
int nPercentOnRange1 = ((nValue1 - 5) * 100 / nRange1);
Then you can do
int nValue2 = 10 + nPercentOnRange1*nRange2/100;
The value you need is
x = 10 + nRange2 * nPercentOnRange1 / 100.0
Let me explain why. You need a number x such that
((x - 10) / nRange2) * 100.0 = nPercentOnRange1
Therefore, just solve for x.
((x - 10) / nRange2) * 100.0 = nPercentOnRange1 =>
((x - 10) / nRange2) = nPercentOnRange1 / 100.0 =>
x - 10 = nRange2 * nPercentOnRange1 / 100.0 =>
x = 10 + nRange2 * nPercentOnRange1 / 100.0
And note that this actually makes intuitive sense. We're saying take the percentage, scale that into the length of the second range (that's what nRange2 * nPercentOnRange1 / 100.0) is doing and then add that to the lower bound of the second range. Basically we are saying step nPercentOnRange1 percent into the second range. That's exactly what the formula is expressing.
Perhaps this will work:
nValue2 = nPercentage1 * nRange2 / 100 + 10