Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #29256
| X-Received | by 10.182.28.41 with SMTP id y9mr14642834obg.14.1452966255386; Sat, 16 Jan 2016 09:44:15 -0800 (PST) |
|---|---|
| X-Received | by 10.50.117.3 with SMTP id ka3mr4360igb.0.1452966255363; Sat, 16 Jan 2016 09:44:15 -0800 (PST) |
| Path | csiph.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!h5no5564127igh.0!news-out.google.com!kr2ni311igb.0!nntp.google.com!o2no3739671iga.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.javascript |
| Date | Sat, 16 Jan 2016 09:44:14 -0800 (PST) |
| In-Reply-To | <fe4e5a1d-fe10-4d8e-b83b-1fedc290ba3b@googlegroups.com> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=85.229.217.28; posting-account=kxPkPAoAAACjJi8w0gL9bnyznPzdw9HW |
| NNTP-Posting-Host | 85.229.217.28 |
| References | <4f7240ca-c0c5-4ba8-9bda-9026cfe4c3d9@googlegroups.com> <fe4e5a1d-fe10-4d8e-b83b-1fedc290ba3b@googlegroups.com> |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <5ea5a46e-e078-41aa-8884-fbda2052a3d9@googlegroups.com> (permalink) |
| Subject | Re: Need help loop construcion, can not get it to work. |
| From | jonas.thornvall@gmail.com |
| Injection-Date | Sat, 16 Jan 2016 17:44:15 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | quoted-printable |
| X-Received-Bytes | 5125 |
| X-Received-Body-CRC | 4255805097 |
| Xref | csiph.com comp.lang.javascript:29256 |
Show key headers only | View raw
Den lördag 16 januari 2016 kl. 18:43:09 UTC+1 skrev jonas.t...@gmail.com:
> Den lördag 16 januari 2016 kl. 18:31:19 UTC+1 skrev jonas.t...@gmail.com:
> > I try to explain what i try to do some pseudo code starting from base 2
> >
> > x=2
> >
> > while base < x so for all those bases.
> >
> > In an inner loop.
> > I construct counters to test legs in the form
> > y=x*base+i if all y composite numbers "noneprime".
> >
> > so "i" is an integer constant within the loop and x multiples of the base
> > but i reduce it down to i=i+base so using base= 10 and i=1 would give.
> > 1+10,1+20,1+30... and so on within the loop.
> >
> > However i just do not want to check when i is 1 so i need an inbetween loop that just add 1 to "i" for each call to loop i call that counter j++
> >
> > The whole thing calling a primality check for each loop instance and report a boolean for "for the leg" that is printed out.
> >
> > The idea is finding the percentage of legs that are prime.
> > For base 2 it is easy to see that 50% using the form is prime, only odd legs prime.
> > However for base 10 it is 60%. 2,4,5,6,8,10 legs can not be prime so they are not necessary to check.
> >
> > So i will check for best base biggest? and only save the legs with primes in an array. That array will work as a counter that sieve out the composites from the primality test.
> >
> >
> > Here is my 2 hours spent on the code, i can not quite see what is going wrong in the loops. I just try Im just using static base 10 and it should report true for +1,+3,+7,+9 leg and false for +2,+4,+5,+6,+8,+10.
> >
> > But ir report memory allocation overflow?
> >
> > <script language="Javascript">
> >
> > function factor_it(i){
> > prime=true;
> > sqroot=Math.floor(Math.sqrt(i));
> > for (j=2;j<sqroot;j++){ k=i/j; prime=!Number.isInteger(k); if (prime) {return prime}}
> > return prime;
> > }
> >
> > function main(){
> > base=10;
> > outStr="";
> > while (base==10) {
> > prime=true;
> >
> > j=1;
> > while(j<=base){
> > i=j;
> > i=i+base;
> > while (i<100){
> > primeleg=factor_it(i);
> > if(primeleg==true){break;}
> > i=i+base;
> >
> > }
> > outStr+="Base="+base+" counterval i = "+i+" P primeleg ="+primeleg+"\n";
> > j++;
> > }
> > base++;
> > }
> > document.prime.out.value =outStr;
> > }
> > </script>
> > <!DOCTYPE html>
> > <head>
> > <meta http-equiv="content-type" content="text/html; charset=UTF-8">
> > </head>
> >
> > <body onload="main()" bgcolor="gold">
> > <form name="prime" action="" onsubmit="MAIN(); return false;">
> > <textarea name="out" cols=80 rows=80></textarea>
> > </form>
> > </body>
> > </html>
>
> I did spot an obvious fault the prime boolean should be initiated to false and initiated in the j++ loop.
<script language="Javascript">
function factor_it(i){
prime=true;
sqroot=Math.floor(Math.sqrt(i));
for (j=2;j<sqroot;j++){ k=i/j; prime=!Number.isInteger(k); if (prime) {return prime}}
return prime;
}
function main(){
base=10;
outStr="";
while (base==10) {
j=1;
while(j<=base){
prime=false;
i=j;
i=i+base;
while (i<100){
primeleg=factor_it(i);
if(primeleg==true){break;}
i=i+base;
}
outStr+="Base="+base+" counterval i = "+i+" P primeleg ="+primeleg+"\n";
j++;
}
base++;
}
document.prime.out.value =outStr;
}
</script>
<!DOCTYPE html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body onload="main()" bgcolor="gold">
<form name="prime" action="" onsubmit="MAIN(); return false;">
<textarea name="out" cols=80 rows=80></textarea>
</form>
</body>
</html>
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Need help loop construcion, can not get it to work. jonas.thornvall@gmail.com - 2016-01-16 09:31 -0800
Re: Need help loop construcion, can not get it to work. jonas.thornvall@gmail.com - 2016-01-16 09:42 -0800
Re: Need help loop construcion, can not get it to work. jonas.thornvall@gmail.com - 2016-01-16 09:44 -0800
Re: Need help loop construcion, can not get it to work. jonas.thornvall@gmail.com - 2016-01-16 09:54 -0800
csiph-web