Fraction reduction algorithm -> Implements Basic factoring algorithm.

I have always been interested in the fact that there is no simple
function that translates a decimal into a fraction. Then I saw that
the Texas Instruments graphing calculator has a function that would
do this translation. I always wondered how it worked. I sat down
and came up with this. The factoring algorithm is my own and is
obviously not optimized. If you plug in a number such as 0.252555,
you will notice that it takes a long time. That is because the system
is factoring to reduce the fraction. Factoring is an unproven hard
problem. That is why it is the basis for strong encryption techniques.

If you were to open the task manager, you would noice that your processor
goes to 100% utilization. Factoring requires resource intensive mathematical
computations. If you are prompted to abort the script, choose no. On my
notebook computer, large decimals cause the cpu fan to run.


Factoring and Fraction Source: function chkInt(arg) { return(arg==parseInt(arg)) } function factor(arg) { var factor = new Array() for(var i=1;i<=arg;i++) { if(chkInt(arg/i)) { factor[factor.length] = i } } return factor } function dec_fraction(arg) { // arg should be a decimal or real var numerator = arg.toString().split('.')[1] var denominator = '1' for(var i=0;i<numerator.length;i++) denominator += '0' var fraction = new Array() fraction[0] = numerator fraction[1] = denominator return fraction } function compArr(arr1,arr2) { // returns greatest common factor var ans = 1 for(var i=0;i<arr1.length;i++) { for(var j=0;j<arr2.length;j++) { if(arr1[i]==arr2[j]) { ans = arr1[i] } } } return ans } function fraction(arg) { var gcf = compArr(factor(dec_fraction(arg)[0]),factor(dec_fraction(arg)[1])) var numerator = dec_fraction(arg)[0] var denominator = dec_fraction(arg)[1] var ans = (numerator/gcf)+'/'+(denominator/gcf) return ans }