HTML5
<style>input[type='text']{width:64px;margin:4px;}</style> <form> a: <input type="text" name="a"> b: <input type="text" name="b"> c: <input type="text" name="c"> <input type="button" value="bấm" onclick="tinh(this.form)"/> <div id="kq"></div> </form> <script> function tinh(form){ var kq; function uc(x,y){ while(x!=y) { if(x>y) x=x-y; else y=y-x; } return x; } var kq,um,uh; a = parseInt(form.a.value); b = parseInt(form.b.value); c = parseInt(form.c.value); re = document.getElementById('kq'); de = b*b - 4*a*c; cde = Math.sqrt(de); if(de>0){ if(de%cde==0) { ng = -b+cde; ngk = uc(ng,2*a); nh = -b-cde; nhk = uc(nh,2*a); if(ng%(2*a)==0){ ng = ng/(2*a);} else {ng = ng/ngk+"/"+(2*a)/ngk;} if(nh%(2*a)==0) {nh = nh/(2*a);} else {nh = nh/nhk+"/"+(2*a)/nhk;} kq = "phương trình "+a+"x² +"+b+"x + "+c+" = 0 có 2 nghiệm phân biệt: "+ng+"và "+nh; } else{ g=''; f=''; d = true; for(i=1;i<de;i++){ if(de%i==0){ e = de/i; ci = Math.sqrt(i); if(de%ci==0&&ci!=1){ d = false; f+="√"+e; g+=ci; um = uc(-b,ci); uh = uc(um,2*a); break; } } } if(d==true){kq = "phương trình "+a+"x² +"+b+"x + "+c+" = 0 có 2 nghiệm phân biệt: ("+-b" + "f+")/"+2*a;} else{kq = "phương trình "+a+"x² +"+b+"x + "+c+" = 0 có 2 nghiệm phân biệt: ("+-b/um+" + "g/uh+f+")/"+2*a/uh+" và ("+-b/um+" - "g/uh+f+")/"+2*a/uh;} } re.innerHTML = kq; } </script>
không hiểu sao hàm uc() chạy không nôi
![[OFF]](/assets/images/off.gif)