www.webdeveloper.com
Results 1 to 11 of 11

Thread: [RESOLVED] Largest prime factor

  1. #1
    Join Date
    May 2008
    Posts
    55

    resolved [RESOLVED] Largest prime factor

    What is the largest prime factor of the number 600851475143? My code:
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>P3--Largest prime factor</title>
    </head>
    <body>
    <!--The prime factors of 13195 are 5, 7, 13 and 29.
    
    What is the largest prime factor of the number 600851475143 --> 
    <script type="text/javascript">
    var value = Largest();
    	alert(value);
    function Largest(){
    	var x;
    	for(i=2;i<600851475143;i++)
    	{
    		if(6008514751435%i == 0)
    		  x = i;
    	}
    	return x;
    }
    
    </script>
    </body>
    </html>
    However the web page is not responding....

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,273
    Test only the prime numbers !

    To find all prime Numbers (an old script)
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
      <meta name="generator" content="PSPad editor, www.pspad.com">
      <title>Premiers</title>
    
      </head>
      <body>
      <div id="pge"></div>
      <script type="text/javascript">
    var Prm={
        nbr:null,prm:[2],rng:1,
        ini:function(){Prm.nbr=3;Prm.prm[Prm.rng++]=3;
            document.getElementById('pge').innerHTML+=2, 3;
            Prm.svr();
    },
        svr:function(){
             Prm.nbr+=2; 
            if (Prm.tst()) {document.getElementById('pge').innerHTML+=', '+Prm.nbr;
                 Prm.prm[Prm.rng++]=Prm.nbr;}
             setTimeout(Prm.svr,7);
        },
         
        tst:function(){var i=0,m=~~Math.sqrt(Prm.nbr);
            while(Prm.prm[i]<=m && (Prm.nbr%Prm.prm[i])!=0) i++;
            return (Prm.nbr%Prm.prm[i])!=0;}
    }
    Prm.ini();
    
    </script> 
      </body>
    </html>
    With some changes to list the prime factors

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title>Prime factors</title>
    <style type="text/css">
    
    </style>
    </head>
    <body>
    <div id="pge"><fieldset><legend>primes factors</legend>
    <p>Enter a number and click on this sentence<br><input id="nmb" type="text" onblur="prmFct(this.value)"></p>
    
    <div id="rsp"></div>
    </fieldset></div>
    <script type="text/javascript">
    var Prm={
        nbr:null,prm:[2],rng:1,oki:null,
        ini:function(){Prm.nbr=3;Prm.prm[Prm.rng++]=3;Prm.oki=true;
            // document.getElementById('pge').innerHTML+=2, 3;
            document.getElementById('rsp').innerHTML=N+' = ';
            Prm.svr();
    },
        svr:function(){
             Prm.nbr+=2;
            if (Prm.tst()) {
                // document.getElementById('pge').innerHTML+=', '+Prm.nbr;
                if (!(N%Prm.nbr)) {
                    document.getElementById('rsp').innerHTML+=(Prm.oki?'':' x ')+Prm.nbr;
                    N/=Prm.nbr;Prm.oki=false;
                    if (N==1) {document.getElementById('rsp').innerHTML+=' !';return}}
             Prm.prm[Prm.rng++]=Prm.nbr;}
             setTimeout(Prm.svr,7);
        },
    
        tst:function(){var i=0,m=~~Math.sqrt(Prm.nbr);
            while(Prm.prm[i]<=m && (Prm.nbr%Prm.prm[i])!=0) i++;
            return (Prm.nbr%Prm.prm[i])!=0;}
    }
    
    var N;
    function prmFct(n){N=parseInt(n);
        if (!isNaN(N)) Prm.ini();
        else alert("Invalid number !");
    }
    
    </script>
    </body>
    </html>
    Be patient ! The response is, unless I am mistaken, 6857 !
    Last edited by 007Julien; 01-26-2014 at 07:25 PM. Reason: complements

  3. #3
    Join Date
    May 2008
    Posts
    55
    I don't understand your algorithm and can you please explain?
    What is the format svr:function() in js?

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,273
    In the first algorithm the function svr examines only the following number after 2 and 3 which is 5, 7, 9 ... etc (we test only odd numbers).

    The second algorithm is wrong !
    At first one factor may appear more than once. We have then to write something like this :

    Code:
        svr:function(){
             Prm.nbr+=2;
            if (Prm.tst()) {
                // document.getElementById('pge').innerHTML+=', '+Prm.nbr;
                if (!(N%Prm.nbr)) {
                    Prm.prm[Prm.rng++]=Prm.nbr;
                    while(!(N%Prm.nbr)) {
                        document.getElementById('rsp').innerHTML+=(Prm.oki?'':' x ')+Prm.nbr;
                        N/=Prm.nbr;Prm.oki=false;}
                    if (N==1) {document.getElementById('rsp').innerHTML+=' !';return}}
             }
             setTimeout(Prm.svr,7);
        },
    Further more 2 and 3 are not tested !

    Finally your method is perhaps better with divisions and a setTimeout to avoid a too long script...
    Something like this ?
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title></title>
    <style type="text/css">
    
    </style>
    </head>
    <body>
    <div id="pge"><fieldset><legend>primes factors</legend>
    <p>Enter a number and click on this sentence<br><input id="nmb" type="text" onblur="prmFct(this.value)"></p>
    
    <div id="rsp"></div>
    </fieldset></div>
    <script type="text/javascript">
    
    // http://www.webdeveloper.com/forum/showthread.php?209526-Is-Prime-Help&highlight=primes
    
    var N,F,okF;
    function prmFct(n){ N=parseInt(n);
    	if (isNaN(N)){alert("Invalid number !");return}
    	document.getElementById('rsp').innerHTML=N+' = ';
    	F=2;okF=false;algo();
    }
    function algo(){
    	while (N%F) F++;
      	while(!(N%F)) {
    		document.getElementById('rsp').innerHTML+=(okF?' x ':'')+F;
    		N/=F;okF=true;}
    	if (N==1) {document.getElementById('rsp').innerHTML+=' !';return}
    	setTimeout(algo,7);
    }
    </script>
    </body>
    </html>
    NB : There is many post about primes. See for example this with the rnd me solution proposed by Kor.
    Last edited by 007Julien; 01-27-2014 at 08:37 AM.

  5. #5
    Join Date
    May 2008
    Posts
    55
    I meant the named function expression.
    Code:
    svr:function() == function svr() ????

  6. #6
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by zhshqzyc View Post
    I meant the named function expression.
    Code:
    svr:function() == function svr() ????
    It's a property of an object.

    Code:
    var obj = { propertyName: expression }
    So svr is a property name and its value is a function expression.

  7. #7
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,273
    A final proposal form ?
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title></title>
    <style type="text/css">
    body {font-family:Georgia;background:#ccc}
    #pge {display:block;width:400px;margin:30px auto;padding:10px;text-align:center;background:#ffc}
    legend {font-size:large;}
    input {text-align:center;}
    #rsp {color:#f00;font-weight:bold;}
    </style>
    </head>
    <body>
    <div id="pge"><fieldset><legend>Prime factors</legend>
    <p>Enter a number and click on this sentence<br><br><input id="nmb" type="text" onblur="prmFct(this.value)"></p>
    <div id="rsp"></div>
    </fieldset></div>
    <script type="text/javascript">
    var N,F,okF;
    function prmFct(n){ N=parseInt(n);
        if (isNaN(N)){alert("Invalid number !");return}
        document.getElementById('rsp').innerHTML=N+' = ';
        F=2;okF=false;setTimeout(algo,7);
    }
    function algo(){
        while (N%F) F++;
          while(!(N%F)) {
            document.getElementById('rsp').innerHTML+=(okF?' x ':'')+F;
            N/=F;okF=true;}
        if (N==1) {document.getElementById('rsp').innerHTML+=' !<br>&nbsp;';return}
        setTimeout(algo,7);
    }
    </script>
    </body>
    </html>
    For large numbers, the number of increments with the while (N%F) should be limited to call the setTimeout...
    Last edited by 007Julien; 01-27-2014 at 10:24 AM.

  8. #8
    Join Date
    May 2008
    Posts
    55
    Hi, guys. I am not looking a solution and simply copy and paste your code.
    I want to dig up my own code's problem. Put my code in Firefox, it will crashes. Trust me, please take a try.

  9. #9
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,273
    Your script need to much time and Firefox proposes simply to stop it (It's to avoid erroneous infinite actions).

    It's possible to modify your script like this :
    Code:
    var nmb=600851475143;
    var value = Largest();
        alert(value);
    function Largest(){
        var x;
        for(i=2;i<=nmb;i++){
            if(nmb%i == 0){
              while (nmb%i==0) nmb/=i;
              x=i;
              }
        }
        return x;
    }

  10. #10
    Join Date
    Jan 2014
    Posts
    2

    need your help to run this script with this doctype <!DOCTYPE HTML>

    Hello,

    The below script not working on declaring doctype

    <!DOCTYPE HTML>---------------when i make it only <html> this is working
    <head>
    <meta charset="utf-8">
    <title>scroll up and down</title>

    </head>
    <body>
    <div id="point1" STYLE="position:absolute;visibility:visible;">
    <!--Please delete this table and insert into your html elements-->
    Here Image and text will come
    <!--End of the customizable area, please do not delete div the tag -->
    </div>


    <script type="text/javascript">
    var XX=20; // X position of the scrolling objects
    var xstep=1;
    var delay_time=60;

    //Begin of the unchangable area, please do not modify this area
    var YY=0;
    var ch=0;
    var oh=0;
    var yon=0;

    var ns4=document.layers?1:0
    var ie=document.all?1:0
    var ns6=document.getElementById&&!document.all?1:0

    if(ie){
    YY=document.body.clientHeight;
    point1.style.top=YY;
    }
    else if (ns4){
    YY=window.innerHeight;
    document.point1.pageY=YY;
    document.point1.visibility="hidden";
    }
    else if (ns6){
    YY=window.innerHeight
    document.getElementById('point1').style.top=YY
    }


    function reloc1()
    {

    if(yon==0){YY=YY-xstep;}
    else{YY=YY+xstep;}
    if (ie){
    ch=document.body.clientHeight;
    oh=point1.offsetHeight;
    }
    else if (ns4){
    ch=window.innerHeight;
    oh=document.point1.clip.height;
    }
    else if (ns6){
    ch=window.innerHeight
    oh=document.getElementById("point1").offsetHeight
    }

    if(YY<0){yon=1;YY=0;}
    if(YY>=(ch-oh)){yon=0;YY=(ch-oh);}
    if(ie){
    point1.style.left=XX;
    point1.style.top=YY+document.body.scrollTop;
    }
    else if (ns4){
    document.point1.pageX=XX;
    document.point1.pageY=YY+window.pageYOffset;
    }
    else if (ns6){
    document.getElementById("point1").style.left=XX
    document.getElementById("point1").style.top=YY+window.pageYOffset
    }

    }

    function onad()
    {
    if(ns4)
    document.point1.visibility="visible";
    loopfunc();
    }
    function loopfunc()
    {
    reloc1();
    setTimeout('loopfunc()',delay_time);
    }

    if (ie||ns4||ns6)
    window.onload=onad

    </script>
    </body>
    </html>



    need your help to run this script with this doctype <!DOCTYPE HTML>

  11. #11
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,273
    Then the largest prime number that can be written in javascript(*) is 9007199254740881 !

    (*) The largest value is 253 = 9007199254740992 See ECMA Section 8.5 Number

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles