Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
simult Q3 with latex code + Quad factorization
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Threading; using MathSpace; namespace Rextester { public class Program { public static void Main(string[] args) { //Your code goes here //int[] a = simult(new int[] {4,2,20}, new int[] {7,-3,9}); //Console.WriteLine(simult(new int[] {4,2,20}, new int[] {7,-3,9})); //Console.WriteLine(a[0]); //Console.WriteLine(a[1]); // simultQ(); //Fraction[] a = simultF(new int[] {-2,-6,1}, new int[] {-5,-1,-2}); //Console.WriteLine(a[0] + " " + a[1]); //double[] b = simultD(new int[] {2,6,1}, new int[] {-5,-1,-2}); //Console.WriteLine(b[0] + " " + b[1]); //Console.WriteLine(new Fraction(1,4,2)); //Fraction v; //Console.WriteLine(Fraction.TryParse("1 3/2",out v)); //simultF(new int[] {2,6,1}, new int[] {5,-1,-2}); // Fraction number = new Fraction(3,4); // Console.WriteLine(FractionToLatex(number)); QuadEx(); // simultQ(); } public static int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); } public static void QuadEx() { int a,b,c; int[] numArray = new int[]{-9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int[] n = new int[4]; int count=0; while (count<100) { for (int i = 0; i<n.Length; i++) { n[i] = numArray[Rand.Next(1,17)]; } a = n[0]*n[2]; b = n[0]*n[3] + n[1]*n[2]; c = n[1]*n[3]; if (GCD(n[0],n[1])==1 && GCD(n[2],n[3])==1) { Fraction x1 = new Fraction(-n[1],n[0]); Fraction x2 = new Fraction(-n[3],n[2]); if (n[0]>0 && n[2]<0) {// if n[0] is + and n[2] - if(n[2]*n[3]<0) { //then n[3] must be + for both cases if (n[1]<0) { // n[1] -, b must be +, c must be - Console.Write("({0}x - {1})({3} - {2}x) = {4}x^2+{5}x-{6}, ", n[0], Math.Abs(n[1]), Math.Abs(n[2]), n[3], a, b, Math.Abs(c)); Console.WriteLine("x = {0}, {1}", x1,x2); count++; } else if (n[1]>0) { // n[1] +, c must be + if (b<0) { // b - Console.Write("({0}x + {1})({3} - {2}x) = {4}x^2-{5}x+{6}, ", n[0], n[1], Math.Abs(n[2]), n[3], a, Math.Abs(b), c); Console.WriteLine("x = {0}, {1}", x1,x2); count++; } else if (b>0) { // b + Console.Write("({0}x + {1})({2}x + {3}) = {4}x^2+{5}x+{6}, ", n[0], n[1], n[2], n[3], a, b, c); Console.WriteLine("x = {0}, {1}", x1,x2); count++; } } } } else if (n[0]<0 && n[2]>0){ // if n[0] is - and n[2] + if(n[0]*n[1]<0) { //then n[1] must be + for both cases if (n[3]<0) { // n[3] -, b must be +, c must be - Console.Write("({0}x + {1})({2}x - {3}) = {4}x^2+{5}x-{6}, ", n[0], n[1], n[2], Math.Abs(n[3]), a, b, Math.Abs(c)); Console.WriteLine("x = {0}, {1}", x1,x2); count++; } else if (n[3]>0) { // n[3] +, c must be + if (b<0) { // b - Console.Write("({0}x + {1})({2}x + {3}) = {4}x^2-{5}x+{6}, ", n[0], n[1], n[2], n[3], a, Math.Abs(b), c); Console.WriteLine("x = {0}, {1}", x1,x2); count++; } else if (b>0) { // b + Console.Write("({0}x + {1})({2}x + {3}) = {4}x^2+{5}x+{6}, ", n[0], n[1], n[2], n[3], a, b, c); Console.WriteLine("x = {0}, {1}", x1,x2); count++; } } } } else if (n[0]>0 && n[2]>0){ //both n[0] and n[2] + if (n[1]<0 && n[3]>0) { // n[1] -, n[3] + (c must be -) if (b<0) { // b - Console.Write("({0}x - {1})({2}x + {3}) = {4}x^2-{5}x-{6}, ", n[0], Math.Abs(n[1]), n[2], n[3], a, Math.Abs(b), Math.Abs(c)); Console.WriteLine("x = {0}, {1}", x1,x2); count++; } else if (b>0){ // b + Console.Write("({0}x - {1})({2}x + {3}) = {4}x^2+{5}x-{6}, ", n[0], Math.Abs(n[1]), n[2], n[3], a, b, Math.Abs(c)); Console.WriteLine("x = {0}, {1}", x1,x2); count++; } } else if (n[1]>0 && n[3]<0) { // n[1] +, n[3] - (c must be -) if (b<0) { // b - Console.Write("({0}x + {1})({2}x - {3}) = {4}x^2-{5}x+{6}, ", n[0], n[1], n[2], Math.Abs(n[3]), a, Math.Abs(b), Math.Abs(c)); Console.WriteLine("x = {0}, {1}", x1,x2); count++; } else if (b>0) { // b+ Console.Write("({0}x + {1})({2}x - {3}) = {4}x^2+{5}x+{6}, ", n[0], n[1], n[2], Math.Abs(n[3]), a, b, Math.Abs(c)); Console.WriteLine("x = {0}, {1}", x1,x2); count++; } } else if (n[1]<0 && n[3]<0) { // n[1] -, n[3] - (c must be +, b must be -) Console.Write("({0}x - {1})({2}x - {3}) = {4}x^2-{5}x+{6}, ", n[0], Math.Abs(n[1]), n[2], Math.Abs(n[3]), a, Math.Abs(b), c); Console.WriteLine("x = {0}, {1}", x1,x2); count++; } else if (n[1]>0 && n[3]>0) { // n[1] +, n[3] + (b must be +, c must be +) Console.Write("({0}x + {1})({2}x + {3}) = {4}x^2+{5}x+{6}, ", n[0], n[1], n[2], n[3], a, b, c); Console.WriteLine("x = {0}, {1}", x1,x2); count++; } } else if (n[0]<0 && n[2]<0){ // if both n[0] and n[2] are - if (n[1]>0 && n[3]>0) { // both n[1] and b[3] must be +, b must be -, c must be + Console.Write("({0}x + {1})({2}x + {3}) = {4}x^2-{5}x+{6}, ", n[0], n[1], n[2], n[3], a, Math.Abs(b), c); Console.WriteLine("x = {0}, {1}", x1,x2); count++; } } } } } public static Fraction[] simultF(int[] c, int[] d) { Fraction x, y; //int[] c = new int[3]; //int[] d = new int[3]; if(c.Length != 3 && d.Length!=3) throw new ArgumentException("array should be int[3]"); int yLeft = c[0]*d[2]-d[0]*c[2]; int yRight = c[0]*d[1]-d[0]*c[1]; y = new Fraction(yLeft, yRight); Fraction xLeft = c[2]-c[1]*y; int xRight = c[0]; x = new Fraction(xLeft,xRight); Fraction[] z = new Fraction[] {x,y}; //Console.WriteLine("x = {0}, y = {1}",x,y); return z; } public static double[] simultD(int[] c, int[] d) { double x,y; //int[] c = new int[3]; //int[] d = new int[3]; if(c.Length != 3 && d.Length!=3) throw new ArgumentException("array should be int[3]"); y = (double) (c[0]*d[2]-d[0]*c[2] ) / (c[0]*d[1]-d[0]*c[1]); x = (double) (c[2]-c[1]*y)/c[0]; double[] z = new double[] {x,y}; //Console.WriteLine("x = {0}, y = {1}",x,y); return z; } public static string FractionToLatex(Fraction result){ if (result.denom!=1) { return "\\frac{"+result.num+"}{"+result.denom+"}"; } else return result.ToString(); } public static void simultQ() { int[] c = new int[6]; int count = 0; int[] numArray = new int[] {-9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9}; while (count <100 ) { for (int i = 0; i<c.Length; i++) { c[i] = numArray[Rand.Next(1,17)]; } if (c[0]*c[4]!=c[3]*c[1]) { Fraction[] result = simultF(new int[] {c[0], c[1], c[2]}, new int[] {c[3], c[4], c[5]}); // //if (Math.Abs(result[0]*result[1])<200 && result[0]%1==0 && result[1]%1==0 && result[0]+result[1]!=0 && result[0]*result[1]!=0) { if (Math.Abs((result[0]*result[1]).toDeci())<200 && result[0].Num+result[1].Num!=0 && result[0].Num*result[1].Num!=0 && Math.Abs(result[0].num)<10 && Math.Abs(result[0].denom)<10 && Math.Abs(result[1].num)<10 && Math.Abs(result[1].denom)<10) { count++; if (c[1]<0 && c[4]>0) { Console.WriteLine("\\item \\("+c[0]+"x - "+ Math.Abs(c[1])+"y = "+c[2]+" \\\\ "+c[3]+"x + "+ c[4]+"y = "+c[5] + "\\) @x = "+FractionToLatex(result[0].Simplify())+", y = "+FractionToLatex(result[1].Simplify())); //Console.WriteLine(c[0]+"x - "+ Math.Abs(c[1])+"y = "+c[2]); //Console.WriteLine(c[3]+"x + "+ c[4]+"y = "+c[5]); //Console.WriteLine("@x = "+result[0].Simplify()+", y = "+result[1].Simplify()); } else if (c[1]>0 && c[4]<0) { Console.WriteLine("\\item \\("+c[0]+"x + "+ c[1]+"y = "+c[2]+" \\\\ "+c[3]+"x - "+Math.Abs(c[4])+"y = "+c[5] + "\\) @x = "+FractionToLatex(result[0].Simplify())+", y = "+FractionToLatex(result[1].Simplify())); //Console.WriteLine(c[0]+"x + "+ c[1]+"y = "+c[2]); //Console.WriteLine(c[3]+"x - "+ Math.Abs(c[4])+"y = "+c[5]); //Console.WriteLine("@x = "+result[0].Simplify()+", y = "+result[1].Simplify()); } else if (c[1]<0 && c[4]<0) { Console.WriteLine("\\item \\("+c[0]+"x - "+ Math.Abs(c[1])+"y = "+c[2]+" \\\\ "+c[3]+"x - "+ Math.Abs(c[4])+"y = "+c[5] + "\\) @x = "+FractionToLatex(result[0].Simplify())+", y = "+FractionToLatex(result[1].Simplify())); //Console.WriteLine(c[0]+"x - "+ Math.Abs(c[1])+"y = "+c[2]); //Console.WriteLine(c[3]+"x - "+ Math.Abs(c[4])+"y = "+c[5]); //Console.WriteLine("@x = "+result[0].Simplify()+", y = "+result[1].Simplify()); } else { Console.WriteLine("\\item \\("+c[0]+"x + "+ c[1]+"y = "+c[2]+" \\\\ "+c[3]+"x + "+ c[4]+"y = "+c[5] + "\\) @x = "+FractionToLatex(result[0].Simplify())+", y = "+FractionToLatex(result[1].Simplify())); //Console.WriteLine(c[0]+"x + "+ c[1]+"y = "+c[2]); //Console.WriteLine(c[3]+"x + "+ c[4]+"y = "+c[5]); //Console.WriteLine("@x = "+result[0].Simplify()+", y = "+result[1].Simplify()); } } } } } } public static class Rand { /// <summary> /// Random number generator used to generate seeds, /// which are then used to create new random number /// generators on a per-thread basis. /// </summary> private static readonly Random globalRandom = new Random(); private static readonly object globalLock = new object(); /// <summary> /// Random number generator /// </summary> private static readonly ThreadLocal<Random> threadRandom = new ThreadLocal<Random>(NewRandom); /// <summary> /// Creates a new instance of Random. The seed is derived /// from a global (static) instance of Random, rather /// than time. /// </summary> public static Random NewRandom() { lock (globalLock) { return new Random(globalRandom.Next()); } } /// <summary> /// Returns an instance of Random which can be used freely /// within the current thread. /// </summary> public static Random Instance { get { return threadRandom.Value; } } /// <summary>See <see cref="Random.Next()" /></summary> public static int Next() { return Instance.Next(); } /// <summary>See <see cref="Random.Next(int)" /></summary> public static int Next(int maxValue) { return Instance.Next(maxValue); } /// <summary>See <see cref="Random.Next(int, int)" /></summary> public static int Next(int minValue, int maxValue) { return Instance.Next(minValue, maxValue); } /// <summary>See <see cref="Random.NextDouble()" /></summary> public static double NextDouble() { return Instance.NextDouble(); } /// <summary>See <see cref="Random.NextBytes(byte[])" /></summary> public static void NextBytes(byte[] buffer) { Instance.NextBytes(buffer); } public static double NextD(double minValue, double maxValue) { return Instance.NextDouble()*(maxValue-minValue)+minValue; } } } namespace MathSpace { public struct Fraction { public long num; public long denom; public long integer; public long Num { get { return num; } set { num = value; } } public long Denom { get { return denom; } set { denom = value; } } public long Integer { get { return integer; } set { integer = value; } } public Fraction(int num, int denom) { // num + denom - if (num > 0 && denom < 0) { this.num = -num; this.denom = Math.Abs(denom); this.integer = 0; } // num - denom + else if (num < 0 && denom > 0 ) { this.num = -Math.Abs(num); this.denom = Math.Abs(denom); this.integer = 0; } // both positive or negative or zero else { this.num = Math.Abs(num); this.denom = Math.Abs(denom); this.integer = 0; } } public Fraction(long num, long denom) { // num + denom - if (num > 0 && denom < 0) { this.num = -num; this.denom = Math.Abs(denom); this.integer = 0; } // num - denom + else if (num < 0 && denom > 0 ) { this.num = num; this.denom = Math.Abs(denom); this.integer = 0; } // both positive or negative or zero else { this.num = Math.Abs(num); this.denom = Math.Abs(denom); this.integer = 0; } } public Fraction(int integer, int num, int denom) { if (num*denom<0) throw new ArgumentException("Numerator and Denominator must be positive."); // all + or only two + if (integer * num * denom > 0 ) { this.integer = Math.Abs(integer); this.num = Math.Abs(num); this.denom = Math.Abs(denom); } // if an odd - else { this.integer = -integer; this.num = Math.Abs(num); this.denom = Math.Abs(denom); } } public Fraction(long integer, long num, long denom) { if (num*denom<0) throw new ArgumentException("Numerator and Denominator must be positive."); // all + or only two + if (integer * num * denom > 0 ) { this.integer = Math.Abs(integer); this.num = Math.Abs(num); this.denom = Math.Abs(denom); } // if an odd - else { this.integer = -integer; this.num = Math.Abs(num); this.denom = Math.Abs(denom); } } public Fraction (Fraction left, int right) { Fraction result = left/right; this.num = result.num; this.denom = result.denom; this.integer = 0; } public Fraction (int left, Fraction right) { Fraction result = left/right; this.num = result.num; this.denom = result.denom; this.integer = 0; } public Fraction (Fraction left, Fraction right) { Fraction result = left/right; this.num = result.num; this.denom = result.denom; this.integer = 0; } public Fraction toMixed() { if (this.integer == 0 && (this.num < 0 || this.denom < 0)) { return new Fraction(this.num / this.denom, Math.Abs(this.num % this.denom), Math.Abs(this.denom)); } else if (this.integer == 0 && (this.num * this.denom > 0)) { return new Fraction(this.num / this.denom, this.num % this.denom, this.denom); } else return new Fraction(this.integer + this.num / this.denom, this.num % this.denom, this.denom); } public Fraction toImproper() { if (this.integer!=0 && this.integer<0) { return new Fraction (-(Math.Abs(this.integer)*Math.Abs(this.denom)+Math.Abs(this.num)),Math.Abs(this.denom)); } else if (this.integer!=0 && this.integer>0) { return new Fraction (Math.Abs(this.integer)*Math.Abs(this.denom)+Math.Abs(this.num),Math.Abs(this.denom)); } else return new Fraction (this.num,this.denom); } public override string ToString() { if (this.denom == 0) { return "Div by 0"; } // if fraction is proper or improper fraction if (this.denom != 1 && this.integer == 0 && this.num%this.denom!=0) { // if fraction is negative (either num or denom negative but not both) if ((this.num < 0 || this.denom < 0 ) && !(this.num<0 && this.denom<0) ) { this.denom = Math.Abs(this.denom); return this.num + "/" + this.denom; } // if fraction is positive else return num + "/" + denom; // + ", " + num/denom + " " + num%denom + "/" + denom; } //if fraction is mixed number else if (this.denom != 1 && this.integer != 0 && this.num%this.denom!=0) { var new_integer = Math.Abs(this.integer) + this.num/this.denom; var new_num = this.num % this.denom; this.integer = new_integer; this.num = new_num; // if fraction is negative (either num or denom negative but not both) if (this.integer < 0) { return this.integer + " " + this.num + "/" + this.denom; } // if fraction is positive else return this.integer + " " + this.num + "/" + this.denom; } // if denom not 1 and numerator is a multiple of the denominator (ie. 4/2 ==> 2) and not mixed number (result is integer) else if (this.num % this.denom == 0 && this.integer==0) { return (this.num / this.denom).ToString(); } // if denom not 1 and numerator is a multiple of the denominator (ie. 4/2 ==> 2) and mixed number (result is integer) else if (this.num % this.denom == 0 && this.integer!=0) { return (this.integer+(this.num / this.denom)).ToString(); } // if denominator is 1, ie., whole number and not mixed number else if (this.integer!=0) { return (integer+num).ToString(); } else return num.ToString(); } public Fraction Simplify() { int divisor = Convert.ToInt32(GCD(this.num, this.denom)); if (this.denom != 0 && this.integer==0 && this.num >0) { return new Fraction(this.num / divisor, this.denom / divisor); } else if (this.denom != 0 && this.integer==0 && this.num <0) { return new Fraction(-Math.Abs(this.num / divisor), Math.Abs(this.denom / divisor)); } else if (this.denom != 0 && this.integer!=0) { return new Fraction(this.integer, this.num / divisor, this.denom / divisor); } else return new Fraction (this.num, this.denom); } public static int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); } public static long GCD(long a, long b) { return b == 0 ? a : GCD(b, a % b); } public Fraction Reciprocal() { return new Fraction(this.Denom, this.Num); } public static Fraction operator +(Fraction left, Fraction right) { Fraction result; if (left.integer==0 &&right.integer==0){ result = new Fraction(left.num * right.denom + right.num * left.denom, left.denom * right.denom); } else { //result = new Fraction((left.integer*left.denom+left.num)*right.denom+(right.integer*right.denom+right.num)*left.denom, left.denom * right.denom); result = (new Fraction(left.integer+right.integer, left.num * right.denom + right.num * left.denom, left.denom * right.denom)).toMixed(); } return result.toMixed().Simplify(); } public static Fraction operator +(int left, Fraction right) { Fraction result; if (right.integer==0) { result = new Fraction(left * right.denom + right.num, right.denom); } else { result = (new Fraction(left+right.integer, right.num, right.denom)).toMixed(); } return result.Simplify(); } public static Fraction operator +(Fraction left, int right) { Fraction result; if (left.integer==0) { result = new Fraction(left.num + left.denom * right, left.denom); } else { result = (new Fraction(left.integer+right, left.denom)).toMixed(); } return result.Simplify(); } public static Fraction operator -(Fraction frac) { if (frac.integer==0) { return new Fraction(-frac.num,frac.denom); } else return (new Fraction (-frac.integer,frac.num,frac.denom)).toMixed(); } public static Fraction operator -(Fraction left, Fraction right) { Fraction result; if (left.integer==0 && right.integer==0) { result = (new Fraction(left.num * right.denom - right.num * left.denom, left.denom * right.denom)).toMixed(); } else { if (((left.integer*left.denom+left.num)/left.denom)>=((right.integer*right.denom+right.num)/right.denom)) { result = (left.toImproper()-right.toImproper()).toMixed(); } else result = (left.toImproper()-right.toImproper()).toMixed(); } return result.Simplify(); } public static Fraction operator -(int left, Fraction right) { Fraction result; if (right.integer==0) { result = new Fraction(left * right.denom - right.num, right.denom); } else result = (new Fraction(left * right.denom - right.toImproper().num, right.denom)).toMixed(); return result.Simplify(); } public static Fraction operator -(Fraction left, int right) { Fraction result; if (left.integer==0) { result = new Fraction(left.num - left.denom * right, left.denom); } // if mixed numbers else result = (new Fraction(left.toImproper().num - left.denom * right, left.denom)).toMixed(); return result.Simplify(); } public static Fraction operator *(Fraction left, Fraction right) { Fraction result; if (left.integer==0 && right.integer==0){ result = new Fraction(left.num * right.num, Math.Abs(left.denom * right.denom)); } // if mixed numbers else result = (left.toImproper()*right.toImproper()).toMixed(); return result.Simplify(); } public static Fraction operator *(int left, Fraction right) { Fraction result; if (right.integer==0){ result = new Fraction(left * right.num, right.denom); } else result = (new Fraction(right.toImproper().num * left, right.denom)).toMixed(); return result.Simplify(); } public static Fraction operator *(Fraction left, int right) { Fraction result; // if improper fraction if (left.integer==0){ result = new Fraction(left.num * right, left.denom); } // if mixed numbers else result = (new Fraction(left.toImproper().num * right, left.denom)).toMixed(); return result.Simplify(); } public static Fraction operator /(Fraction left, Fraction right) { Fraction result; if (left.integer==0 &&right.integer==0){ result = left * right.Reciprocal(); } else result = (left.toImproper() * right.toImproper().Reciprocal()).toMixed(); return result.Simplify(); } public static Fraction operator /(int left, Fraction right) { Fraction result; if (right.integer==0) { result = left * right.Reciprocal(); } else result = left * right.toImproper().Reciprocal(); return result.Simplify(); } public static Fraction operator /(Fraction left, int right) { Fraction rightRecip = new Fraction(1,right); Fraction result; if (left.integer==0) { result = left * rightRecip; } else { result = left.toImproper() * rightRecip; } return result.Simplify(); } public static int LCM(int a, int b) { int lcm; int a_r = a; int b_r = b; int d = max(a_r, b_r); while (d > 1) { if (a_r % d == 0 && b_r % d == 0) { int a_r_temp = a_r / d; int b_r_temp = b_r / d; a_r = a_r_temp; b_r = b_r_temp; lcm = d * a_r * b_r; break; } d--; } lcm = d * a_r * b_r; return lcm; } public static int LCM(long a, long b) { int lcm; long a_r = a; long b_r = b; int d = Convert.ToInt32(max(a_r, b_r)); while (d > 1) { if (a_r % d == 0 && b_r % d == 0) { long a_r_temp = a_r / d; long b_r_temp = b_r / d; a_r = a_r_temp; b_r = b_r_temp; lcm = Convert.ToInt32(d * a_r * b_r); break; } d--; } lcm = Convert.ToInt32(d * a_r * b_r); return lcm; } public static int max(int a, int b) { if (a > b) { return a; } else if (b > a) { return b; } else { return a; } } public static long max(long a, long b) { if (a > b) { return a; } else if (b > a) { return b; } else { return a; } } public double toDeci() { if (this.integer==0) { return (double) this.num/this.denom; } else { return (double) (this.integer*this.denom+this.num)/this.denom; } } public static Fraction toFrac(double deci) { /*List<double> d = new List<double>(); List<double> z = new List<double>(); List<double> n = new List<double>();*/ double[] d = new double[6]; double[] z = new double[6]; double[] n = new double[6]; z[1] = deci; //X d[0] = 0; d[1] = 1; for (int i = 1; i < 5; i++) { z[i + 1] = 1 / (z[i] - Math.Floor(z[i])); d[i + 1] = d[i] * Math.Floor(z[i + 1]) + d[i - 1]; n[i + 1] = Math.Round(z[1] * d[i + 1]); } int num, denom; num = (int)n[5]; denom = (int)d[5]; return new Fraction(num, denom); } /* public static Fraction RealToFraction(double value, double error) { if (error <= 0.0 || error >= 1.0) { throw new ArgumentOutOfRangeException("error", "Must be between 0 and 1 (exclusive)."); } int sign = Math.Sign(value); if (sign == -1) { value = Math.Abs(value); } if (sign != 0) { // error is the maximum relative error; convert to absolute error *= value; } int n = (int)Math.Floor(value); value -= n; if (value < error) { return new Fraction(sign * n, 1); } if (1 - error < value) { return new Fraction(sign * (n + 1), 1); } // The lower fraction is 0/1 int lower_n = 0; int lower_d = 1; // The upper fraction is 1/1 int upper_n = 1; int upper_d = 1; while (true) { // The middle fraction is (lower_n + upper_n) / (lower_d + upper_d) int middle_n = lower_n + upper_n; int middle_d = lower_d + upper_d; if (middle_d * (value + error) < middle_n) { // real + error < middle : middle is our new upper upper_n = middle_n; upper_d = middle_d; } else if (middle_n < (value - error) * middle_d) { // middle < real - error : middle is our new lower lower_n = middle_n; lower_d = middle_d; } else { // Middle is our best fraction return new Fraction((n * middle_d + middle_n) * sign, middle_d); } } }*/ public static Fraction Parse(string input) { if (String.IsNullOrWhiteSpace(input)) { throw new ArgumentNullException(); } //var instance = new Fraction(); // checks if there is more than one "/" if (input.IndexOf("/") != input.LastIndexOf("/")) { throw new ArgumentException(); } //return instance; int v; // if (input.IndexOf("/") != -1) { if (int.TryParse(input.Substring(0, input.IndexOf("/")), out v) && int.TryParse(input.Substring(input.IndexOf("/") + 1), out v)) { int num1 = int.Parse(input.Substring(0, input.IndexOf("/"))); int denom1 = int.Parse(input.Substring(input.IndexOf("/") + 1)); var frac = new Fraction(num1, denom1); return frac; } else throw new IndexOutOfRangeException(); } else // if { if (int.TryParse(input, out v)) { return new Fraction(int.Parse(input), 1); } else throw new IndexOutOfRangeException(); } } public static bool TryParse(string input, out Fraction frac) { if (string.IsNullOrWhiteSpace(input)) { frac = new Fraction(0, 0); return false; } if (input.IndexOf("/") == -1 || input.IndexOf("/") != input.LastIndexOf("/")) { frac = new Fraction(0, 0); return false; } long v; if (long.TryParse(input.Substring(0, input.IndexOf("/")), out v) && long.TryParse(input.Substring(input.IndexOf("/") + 1), out v)) { frac = new MathSpace.Fraction(1, 1); v = 1; return true; } else frac = new Fraction(0, 0); return false; } public static Fraction ConvertFrom(object obj) { Fraction frac = new MathSpace.Fraction(0, 0); if (obj.GetType() == frac.GetType()) { frac = (Fraction) obj; return frac; } else throw new NotSupportedException(); } public static double ConvertToDecimal(Fraction frac) { return Convert.ToDouble(frac.Num) / Convert.ToDouble(frac.Denom); } public static double operator +(Fraction left, double right) { double result = ConvertToDecimal(left) + right; return result; } public static double operator +(double left, Fraction right) { double result = left + ConvertToDecimal(right); return result; } public static double operator -(Fraction left, double right) { double result = ConvertToDecimal(left) - right; return result; } public static double operator -(double left, Fraction right) { double result = left - ConvertToDecimal(right); return result; } public static double operator *(Fraction left, double right) { double result = ConvertToDecimal(left) * right; return result; } public static double operator *(double left, Fraction right) { double result = left * ConvertToDecimal(right); return result; } public static double operator /(Fraction left, double right) { double result = ConvertToDecimal(left) / right; return result; } public static double operator /(double left, Fraction right) { double result = left / ConvertToDecimal(right); return result; } public static Fraction Pow(Fraction frac, int n) { long num_new = (long) Math.Pow(frac.Num, n); long denom_new = (long) Math.Pow(frac.Denom, n); return new MathSpace.Fraction(num_new, denom_new); } } }
run
|
edit
|
history
|
help
0
my cool stuff
Time by 15 minutes
Dokkan 2.0
Overriding call parent & child class
FREE V-BUCKS GENERATOR NO HUMAN VERIFICATION 2022 [zT4]
asxsdf
Code challenge: Integer Comparison - Overkill - Composite Function
Bid Status
amstrong number
ERFDAX