# Thread: [RESOLVED] Largest prime factor

1. Registered User
Join Date
May 2008
Posts
57

## [RESOLVED] Largest prime factor

What is the largest prime factor of the number 600851475143? My code:
Code:
```<!DOCTYPE HTML>
<html>
<meta charset="utf-8">
<title>P3--Largest prime factor</title>
<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();
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. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
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>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Premiers</title>

<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>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Prime factors</title>
<style type="text/css">

</style>
<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();
}

</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. Registered User
Join Date
May 2008
Posts
57
What is the format svr:function() in js?

4. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
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>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title></title>
<style type="text/css">

</style>
<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 N,F,okF;
function prmFct(n){ N=parseInt(n);
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. Registered User
Join Date
May 2008
Posts
57
I meant the named function expression.
Code:
`svr:function() == function svr() ????`

6. 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.

7. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
A final proposal form ?
Code:
```<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title></title>
<style type="text/css">
body {font-family:Georgia;background:#ccc}
legend {font-size:large;}
input {text-align:center;}
#rsp {color:#f00;font-weight:bold;}
</style>
<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);
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. Registered User
Join Date
May 2008
Posts
57
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. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
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();
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. Registered User
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
<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>

11. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
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

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
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.40267 seconds
• Memory Usage 2,966KB
• Queries Executed 15 (?)
Template Usage (34):
• (10)bbcode_code
• (1)bbcode_quote
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (11)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (11)postbit
• (11)postbit_onlinestatus
• (11)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./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/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (72):
• init_startup
• friendlyurl_resolve_class
• 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_postinfo_query
• fetch_postinfo
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete