Last active
May 15, 2017 05:17
-
-
Save SangeetAgarwal/51ed9cbfd633539cce84f2a9494d2cf2 to your computer and use it in GitHub Desktop.
SampleGist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main() | |
{ | |
return 0; | |
} | |
class Fraction | |
{ | |
int num; | |
int den; | |
int getNum() { | |
return num; | |
} | |
int getDen() { | |
return den; | |
} | |
Fraction() | |
{ | |
} | |
Fraction(int a, int b) | |
{ | |
num = a; | |
den = b; | |
} | |
Fraction operator +(Fraction) | |
{ | |
} | |
Fraction operator -(Fraction) | |
{ | |
} | |
Fraction operator *(Fraction) | |
{ | |
} | |
Fraction operator /(Fraction) | |
{ | |
} | |
Fraction Exp(Fraction someFraction) | |
{ | |
// 1 + [someFraction ^ 1 / 1!] + [someFraction ^ 2 / 2!] + [someFraction ^ 3 / 3!] ..... | |
1 + 2/3 + | |
// below lines calculate [someFraction ^ 1 / 1!] + [someFraction ^ 2 / 2!] + [someFraction ^ 3 / 3!] ..... | |
float result = 0; | |
Fraction resultUpper = new Fraction(1,1); | |
int counter = 1; | |
// do the summation until x^k/k! is less than 10^-5 i.e. .00001 | |
float checkValue = 0.0; | |
while (checkValue < .00001) | |
{ | |
// use the overloaded operator to multiply the fraction & the resultant fraction passed back is added to the resultUpper. | |
resultUpper = resultUpper * someFraction; | |
(1) resultUpper = 1/1 * 2/3 = 2/3 | |
(2) resultUpper = 2/3 * someFraction = 2/3 * 2/3 = 4/9 | |
(3) | |
// calculate the each value and add to the result | |
checkValue = ((resultUpper.getNum() / resultUpper.getDen()) / factorial(counter)) | |
result = result + checkValue ; | |
(1) result = 0 + (2/3 / factorial(1)) = 2/3; | |
(2) result = 2/3 + (4/9 / factorial(2)) = (2/3 + (4/9 / 2)) = [2/3] + [(2/3)^2] | |
++counter; | |
} | |
result = result + 1; | |
} | |
int factorial(int n) | |
{ | |
//return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n; | |
if (n == 1 || n == 0) { | |
return 1; | |
} | |
else { | |
factorial(n - 1) * n; | |
} | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment