Connecting Tech Pros Worldwide Forums | Help | Site Map

Simplyfiying fractions in Javascript

mouseit101@gmail.com
Guest
 
Posts: n/a
#1: Jan 13 '06
Does anybody have a script to simplify fractions?


mouseit101@gmail.com
Guest
 
Posts: n/a
#2: Jan 13 '06

re: Simplyfiying fractions in Javascript


Or just like a recommendation?

aunko
Guest
 
Posts: n/a
#3: Jan 13 '06

re: Simplyfiying fractions in Javascript



mouseit101@gmail.com wrote:[color=blue]
> Or just like a recommendation?[/color]
What exactly do you need? Please provide sample input/output...

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#4: Jan 13 '06

re: Simplyfiying fractions in Javascript


mouseit101@gmail.com wrote:
[color=blue]
> Does anybody have a script to simplify fractions?[/color]

To simplify a fraction, find the greatest common divisor of the numerator
and the denominator of the fraction and divide both by it if there is one
greater than 1.

<URL:http://google.com/search?q=simplify+fraction&filter=0>


HTH

PointedEars
Dr John Stockton
Guest
 
Posts: n/a
#5: Jan 13 '06

re: Simplyfiying fractions in Javascript


JRS: In article <1137117147.708779.271350@g14g2000cwa.googlegroups .com>
, dated Thu, 12 Jan 2006 17:52:27 local, seen in
news:comp.lang.javascript, mouseit101@gmail.com <mouseit101@gmail.com>
posted :[color=blue]
>Does anybody have a script to simplify fractions?[/color]

Let the fraction be X/Y, X & Y being variables.

function HCF(u, v) { var U = u, V = v
while (true) {
if (!(U%=V)) return V
if (!(V%=U)) return U } }

T = HCF(X, Y) ; X /= T ; Y /= T ;

HCF is used in js-dates.htm, via FAQ, see below. It is intended for use
only with positive integer parameters, but full compliance is not
obligatory.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Randy Webb
Guest
 
Posts: n/a
#6: Jan 14 '06

re: Simplyfiying fractions in Javascript


Dr John Stockton said the following on 1/13/2006 3:03 PM:[color=blue]
> JRS: In article <1137117147.708779.271350@g14g2000cwa.googlegroups .com>
> , dated Thu, 12 Jan 2006 17:52:27 local, seen in
> news:comp.lang.javascript, mouseit101@gmail.com <mouseit101@gmail.com>
> posted :
>[color=green]
>>Does anybody have a script to simplify fractions?[/color]
>
>
> Let the fraction be X/Y, X & Y being variables.
>
> function HCF(u, v) { var U = u, V = v[/color]

Just for my own curiosity, does the U = u and V = v serve any useful
purpose? Meaning, does the code fail if you use u and v below instead of
U and V?

It shouldn't is why I am asking.
[color=blue]
> while (true) {
> if (!(U%=V)) return V
> if (!(V%=U)) return U } }
>
> T = HCF(X, Y) ; X /= T ; Y /= T ;
>
> HCF is used in js-dates.htm, via FAQ, see below. It is intended for use
> only with positive integer parameters, but full compliance is not
> obligatory.
>[/color]


--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#7: Jan 14 '06

re: Simplyfiying fractions in Javascript


Randy Webb <HikksNotAtHome@aol.com> writes:
[color=blue]
> Dr John Stockton said the following on 1/13/2006 3:03 PM:[/color]
[color=blue][color=green]
>> function HCF(u, v) { var U = u, V = v[/color]
>
> Just for my own curiosity, does the U = u and V = v serve any useful
> purpose? Meaning, does the code fail if you use u and v below instead
> of U and V?[/color]

Not in this case. Both "u" and "U" are local variables, and since "u"
is never used again, there is no immediate need to rename it.

I'm guessing it's a general strategy to avoid changing the parameters,
in case they are needed again later.

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
mouseit101@gmail.com
Guest
 
Posts: n/a
#8: Jan 14 '06

re: Simplyfiying fractions in Javascript


thanks all

Dr John Stockton
Guest
 
Posts: n/a
#9: Jan 15 '06

re: Simplyfiying fractions in Javascript


JRS: In article <W6CdnY61-ql3_lXeRVn-oQ@comcast.com>, dated Fri, 13 Jan
2006 21:38:20 local, seen in news:comp.lang.javascript, Randy Webb
<HikksNotAtHome@aol.com> posted :[color=blue]
>Dr John Stockton said the following on 1/13/2006 3:03 PM:[/color]
[color=blue][color=green]
>> Let the fraction be X/Y, X & Y being variables.
>>
>> function HCF(u, v) { var U = u, V = v[/color]
>
>Just for my own curiosity, does the U = u and V = v serve any useful
>purpose? Meaning, does the code fail if you use u and v below instead of
>U and V?
>
>It shouldn't is why I am asking.
>[color=green]
>> while (true) {
>> if (!(U%=V)) return V
>> if (!(V%=U)) return U } }
>>
>> T = HCF(X, Y) ; X /= T ; Y /= T ;
>>
>> HCF is used in js-dates.htm, via FAQ, see below. It is intended for use
>> only with positive integer parameters, but full compliance is not
>> obligatory.[/color][/color]

Usually at least, ISTM that it will not fail. I converted it from
another language, in which there are three possible ways of passing
parameters and the one which copies must be used (another won't compile
with that body; the third will change the external quantities). I put
the copying in as being easier than deciding whether it might ever
matter.

The only javascript in which I at present use it is

F = A[0] ; J = N ; while (J--) F = HCF(F, A[J])

after which A is abandoned and F is displayed - so I'd not notice the
difference anyway even if there were any.

Code, in js-dates.htm, changed. Thanks.

<URL:http://www.merlyn.demon.co.uk/hcfactor.pas> has several HCF
algorithms.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>.
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)
Closed Thread


Similar JavaScript / Ajax / DHTML bytes