# [RESOLVED] Largest prime factor

• 01-26-2014, 04:50 PM
zhshqzyc
[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....
• 01-26-2014, 07:17 PM
007Julien
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 !
• 01-26-2014, 09:58 PM
zhshqzyc
What is the format svr:function() in js?
• 01-27-2014, 08:26 AM
007Julien
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.
• 01-27-2014, 08:59 AM
zhshqzyc
I meant the named function expression.
Code:

`svr:function() == function svr() ????`
• 01-27-2014, 09:16 AM
tech_soul8
Quote:

Originally Posted by zhshqzyc
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.
• 01-27-2014, 10:09 AM
007Julien
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...
• 01-27-2014, 01:22 PM
zhshqzyc
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.
• 01-27-2014, 02:04 PM
007Julien
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; }```
• 01-28-2014, 03:05 AM
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
<meta charset="utf-8">
<title>scroll up and down</title>

<body>
<div id="point1" STYLE="position:absolute;visibility:visible;">
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
}

}

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

if (ie||ns4||ns6)

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

need your help to run this script with this doctype <!DOCTYPE HTML>
• 01-28-2014, 06:02 AM
007Julien
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

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.12213 seconds
• Memory Usage 2,402KB
• Queries Executed 11 (?)
Template Usage (21):
• (10)bbcode_code_printable
• (1)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates