Solver Fractions Solver
Algebra
->
Algebra
->
Numeric Fractions Calculators, Lesson and Practice
-> Solver Fractions Solver
Log On
Ad:
Algebra Solved!™
: algebra software solves algebra homework problems with step-by-step help!
Ad:
Algebrator™
solves your algebra problems and provides step-by-step explanations!
Algebra: Numeric Fractions
Solvers
Lessons
Problems, free tutors
Quiz
In Depth
Source code of 'Fractions Solver'
This Solver (Fractions Solver)
was created by by
jim_thompson5910(13794)
:
View Source
,
Show
,
Put on YOUR site
About jim_thompson5910
:
==section input Enter the two fractions and choose which operation to perform on them. Make sure you only enter integers (whole numbers) for each numerator/denominator. <table><tr><td>*[input input_num1=1]</td><td></td><td>*[input input_num2=3]</td></tr> <tr><td>______</td><td>*[choice op + - * /]</td><td>______</td></tr> <tr><td>*[input input_denom1=2]</td><td></td><td>*[input input_denom2=4]</td></tr> </table> ==section solution perl sub mult{ my ($split,@arr1,@arr2,$temp1,$temp2, @hold); $split=$_[0]; @hold=split(/;/,$split); @arr1=split(/,/,$hold[0]); $arr1[2]=$arr1[1]; $arr1[1]="/"; @arr2=split(/,/,$hold[1]); $arr2[2]=$arr2[1]; $arr2[1]="/"; #print "arr1=@arr1\n\n"; $temp1=$arr1[0]*$arr2[0]; $temp2=$arr1[2]*$arr2[2]; #$temp1=$numer1*$numer2; #$temp2=$denom1*$denom2; return ($temp1,"/",$temp2); } sub add{ my ($split,@arr1,@arr2, @hold, @tempfrac, $gcf); $split=$_[0]; @hold=split(/;/,$split); @arr1=split(/,/,$hold[0]); $arr1[2]=$arr1[1]; $arr1[1]="/"; @arr2=split(/,/,$hold[1]); $arr2[2]=$arr2[1]; $arr2[1]="/"; if(($arr1[2]!=$arr2[2])&&(($arr1[2]!=0)&&($arr2[2]!=0))) { for(my $count=1;$count<abs($arr1[2]*$arr2[2]);$count++) {if(($arr1[2]%$count==0)&&($arr2[2]%$count==0)) {$gcf=$count;} } my $lcm=($arr1[2]*$arr2[2])/$gcf; my $temp=$lcm/$arr1[2]; $exported_lcm=$lcm; $exported_gcf=$gcf; $arr1[0]=($lcm/$arr1[2])*$arr1[0]; $arr2[0]=($lcm/$arr2[2])*$arr2[0]; $arr1[2]=($lcm/$arr1[2])*$arr1[2]; $arr2[2]=($lcm/$arr2[2])*$arr2[2]; } $tempfrac1[0]=$arr1[0]+$arr2[0]; $tempfrac2[0]=$arr1[2]; return ($tempfrac1[0],"/",$tempfrac2[0]); } sub subtract{ my ($split,@arr1,@arr2, @hold, @tempfrac, $gcf); $split=$_[0]; @hold=split(/;/,$split); @arr1=split(/,/,$hold[0]); $arr1[2]=$arr1[1]; $arr1[1]="/"; @arr2=split(/,/,$hold[1]); $arr2[2]=$arr2[1]; $arr2[1]="/"; if(($arr1[2]!=$arr2[2])&&(($arr1[2]!=0)&&($arr2[2]!=0))) { for($count=1;$count<abs($arr1[2]*$arr2[2]);$count++) {if(($arr1[2]%$count==0)&&($arr2[2]%$count==0)) {$gcf=$count;} } my $lcm=($arr1[2]*$arr2[2])/$gcf; my $temp=$lcm/$arr1[2]; $exported_lcm=$lcm; $exported_gcf=$gcf; $arr1[0]=($lcm/$arr1[2])*$arr1[0]; $arr2[0]=($lcm/$arr2[2])*$arr2[0]; $arr1[2]=($lcm/$arr1[2])*$arr1[2]; $arr2[2]=($lcm/$arr2[2])*$arr2[2]; } $tempfrac1[0]=$arr1[0]-$arr2[0]; $tempfrac2[0]=$arr1[2]; return ($tempfrac1[0],"/",$tempfrac2[0]); } sub reduce{ my (@arr1, @original_arr1, $flag); #$arr1[0]=$_[0]; #$arr1[1]="/"; #$arr1[2]=$_[1]; @arr1=@_; $arr1[2]=$arr1[1]; $arr1[1]="/"; @original_arr1=@arr1; $flag=$_[2]; if($arr1[0]==0) {return $arr1[0];} if($arr1[2]!=0) { $store=$arr1[0]/$arr1[2]; if($store>0) { $arr1[0]=abs($arr1[0]); $arr1[2]=abs($arr1[2]); } if(($store<0)&&($arr1[2]<0)) { $arr1[0]=0-$arr1[0]; $arr1[2]=abs($arr1[2]); } if($arr1[0]%$arr1[2]==0) { if($flag==1) {$arr1[0]=$arr1[0]/$arr1[2]; return $arr1[0];} if($flag==2) {$arr1[0]=$arr1[0]/$arr1[2]; $arr1[2]=1; return ($arr1[0],"/",$arr1[2]);} # if($flag==0) # {return $quotient;} } } for (my $i=abs($arr1[0]*$arr1[2]); $i>1; $i--) { if (($arr1[0]%$i==0)&&($arr1[2]%$i==0)) { $arr1[0]/=$i; $arr1[2]/=$i; my $j=$i; } } if($original_arr1[0]==$arr1[0]) {$exported_no_reduce=1;} return ($arr1[0],"/",$arr1[2]); } sub invert{ my(@arr1,$temp); @arr1=@_; $temp=$arr1[0]; $arr1[0]=$arr1[1]; $arr1[1]=$temp; return ($arr1[0], "/", $arr1[1]); } sub divide{ my ($split,@arr1,@arr2,$temp1,$temp2, @hold); $split=$_[0]; @hold=split(/;/,$split); @arr1=split(/,/,$hold[0]); $arr1[2]=$arr1[1]; $arr1[1]="/"; @arr2=split(/,/,$hold[1]); $arr2[2]=$arr2[1]; $arr2[1]="/"; #print "arr1=@arr1\n\n"; $temp1=$arr1[0]*$arr2[2]; $temp2=$arr1[2]*$arr2[0]; #$temp1=$numer1*$numer2; #$temp2=$denom1*$denom2; return ($temp1,"/",$temp2); } $in1="$input_num1/$input_denom1"; $in2="$input_num2/$input_denom2"; @in_array1=split(/\//,$in1); @in_array2=split(/\//,$in2); ################################################################## if($op eq '+') { @sum=add("$in_array1[0],$in_array1[1];$in_array2[0],$in_array2[1]"); $fact_num1=$exported_lcm/$in_array1[1]; $fact_num2=$exported_lcm/$in_array2[1]; if($fact_num1!=1) {$fact1="(".$exported_lcm/$in_array1[1]."/".$exported_lcm/$in_array1[1].")"; $fact_exp1="Multiply {{{$in1}}} by {{{$fact1}}}"; $mult1="".($exported_lcm/$in_array1[1])*$in_array1[0]."/".($exported_lcm/$in_array1[1])*$in_array1[1].""; $mult_exp1="Multiply {{{$in1}}} and {{{$fact1}}} to get {{{$mult1}}}";} else {$mult1=$in1;} if($fact_num2!=1) {$fact2="(".$exported_lcm/$in_array2[1]."/".$exported_lcm/$in_array2[1].")"; $fact_exp2="Multiply {{{$in2}}} by {{{$fact2}}}"; $mult2="".($exported_lcm/$in_array2[1])*$in_array2[0]."/".($exported_lcm/$in_array2[1])*$in_array2[1].""; $mult_exp2="Multiply {{{$in2}}} and {{{$fact2}}} to get {{{$mult2}}}";} else {$mult2=$in2;} print " {{{$in1+$in2}}} Start with the given expression "; # if($in_array1[1]!=$in_array2[1]) { print " In order to add these two fractions, these fractions need to have a common denominator. In order to do that, we simply find that the LCM of $in_array1[1] and $in_array2[1] is $exported_lcm (note: if you need help with finding the LCM, check out this <a href=http://www.algebra.com/algebra/homework/divisibility/least-common-multiple.solver>solver</a>) Now we need to get each denominator to $exported_lcm "; if($fact_num1!=1) { print " {{{$fact1($in1)+$in2}}} $fact_exp1 {{{$mult1+$in2}}} $mult_exp1 "; } if($fact_num2!=1) { print " {{{$mult1+$fact2($in2)}}} $fact_exp2 {{{$mult1+$mult2}}} $mult_exp2 "; } } else {$exported_lcm=$in_array1[1];} print " Since both fractions have a common denominator of $sum[2], we can now combine the fractions {{{(",($exported_lcm/$in_array1[1])*$in_array1[0],"+",($exported_lcm/$in_array2[1])*$in_array2[0],")/$sum[2]}}} Combine the fractions {{{",($exported_lcm/$in_array1[1])*$in_array1[0]+($exported_lcm/$in_array2[1])*$in_array2[0],"/$sum[2]}}} Add the numerators So {{{$in1+$in2=@sum}}} "; @final_fraction=@sum; } elsif($op eq '-') { @difference=subtract("$in_array1[0],$in_array1[1];$in_array2[0],$in_array2[1]"); $fact_num1=$exported_lcm/$in_array1[1]; $fact_num2=$exported_lcm/$in_array2[1]; if($fact_num1!=1) {$fact1="(".$exported_lcm/$in_array1[1]."/".$exported_lcm/$in_array1[1].")"; $fact_exp1="Multiply {{{$in1}}} by {{{$fact1}}}"; $mult1="".($exported_lcm/$in_array1[1])*$in_array1[0]."/".($exported_lcm/$in_array1[1])*$in_array1[1].""; $mult_exp1="Multiply {{{$in1}}} and {{{$fact1}}} to get {{{$mult1}}}";} else {$mult1=$in1;} if($fact_num2!=1) {$fact2="(".$exported_lcm/$in_array2[1]."/".$exported_lcm/$in_array2[1].")"; $fact_exp2="Multiply {{{$in2}}} by {{{$fact2}}}"; $mult2="".($exported_lcm/$in_array2[1])*$in_array2[0]."/".($exported_lcm/$in_array2[1])*$in_array2[1].""; $mult_exp2="Multiply {{{$in2}}} and {{{$fact2}}} to get {{{$mult2}}}";} else {$mult2=$in2;} print " {{{$in1-$in2}}} Start with the given expression "; # if($in_array1[1]!=$in_array2[1]) { print " In order to subtract these two fractions, these fractions need to have a common denominator. In order to do that, we simply find that the LCM of $in_array1[1] and $in_array2[1] is $exported_lcm (note: if you need help with finding the LCM, check out this <a href=http://www.algebra.com/algebra/homework/divisibility/least-common-multiple.solver>solver</a>) Now we need to get each denominator to $exported_lcm "; if($fact_num1!=1) { print " {{{$fact1($in1)-$in2}}} $fact_exp1 {{{$mult1-$in2}}} $mult_exp1 "; } if($fact_num2!=1) { print " {{{$mult1-$fact2($in2)}}} $fact_exp2 {{{$mult1-$mult2}}} $mult_exp2 "; } } else {$exported_lcm=$in_array1[1];} print " Since both fractions have a common denominator of $difference[2], we can now combine the fractions {{{(",($exported_lcm/$in_array1[1])*$in_array1[0],"-",($exported_lcm/$in_array2[1])*$in_array2[0],")/$difference[2]}}} Combine the fractions {{{",($exported_lcm/$in_array1[1])*$in_array1[0]-($exported_lcm/$in_array2[1])*$in_array2[0],"/$difference[2]}}} Subtract the numerators So {{{$in1-$in2=@difference}}} "; @final_fraction=@difference; } elsif($op eq '*') { @product=mult("$in_array1[0],$in_array1[1];$in_array2[0],$in_array2[1]"); print " {{{($in1)($in2)}}} Start with the given expression "; print " In order to multiply these fractions, simply multiply the numerators and denominators separately like this: {{{($in_array1[0]*$in_array2[0])/($in_array1[1]*$in_array2[1])}}} Multiply the numerators $in_array1[0] and $in_array2[0]. Place them as the numerator. Multiply the denominators $in_array1[1] and $in_array2[1]. Place them as the denominator {{{",$in_array1[0]*$in_array2[0],"/",$in_array1[1]*$in_array2[1],"}}} Now multiply So {{{($in1)($in2)=@product}}} "; @final_fraction=@product; } elsif($op eq '/') { @quotient=divide("$in_array1[0],$in_array1[1];$in_array2[0],$in_array2[1]"); print " {{{($in1)/($in2)}}} Start with the given expression "; @inverted_in2=invert($in_array2[0],$in_array2[1]); print " In order to divide these fractions, first we need to flip the second fraction {{{$in2}}} to get {{{@inverted_in2}}}. Now multiply the first fraction {{{$in1}}} by the second fraction {{{@inverted_in2}}} {{{($in1)(@inverted_in2)}}} Now simply multiply the numerators and denominators separately like this: {{{($in_array1[0]*$in_array2[1])/($in_array1[1]*$in_array2[0])}}} Multiply the numerators $in_array1[0] and $in_array2[1]. Place them as the numerator. Multiply the denominators $in_array1[1] and $in_array2[0]. Place them as the denominator {{{",$in_array1[0]*$in_array2[1],"/",$in_array1[1]*$in_array2[0],"}}} Now multiply So {{{($in1)/($in2)=@quotient}}} "; @final_fraction=@quotient; } else {print "WRONG SELECTION!!";} $final_fraction_string="@final_fraction"; $final_fraction_string=~s/\s//g; $final_fraction[1]=$final_fraction[2]; pop(@final_fraction); @reduced_final=reduce(@final_fraction); if($exported_no_reduce!=1) { print " If want to reduce the fraction, {{{$final_fraction_string}}} reduces to {{{@reduced_final}}} (note: click <a href=http://www.algebra.com/algebra/homework/NumericFractions/reduce-fraction.solver>here</a> if you need help with reducing fractions) "; } ==section output ==section check angle=40 angle1=50