[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Need help loop construcion, can not get it to work.

JT

1/16/2016 5:31:00 PM

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>



3 Answers

JT

1/16/2016 5:43:00 PM

0

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.

JT

1/16/2016 5:44:00 PM

0

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>

JT

1/16/2016 5:55:00 PM

0

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>

What is causing memory allocation overflow in this?
j is resetting i for each loop call the out loop reach to 10 and the inner to 100, what is there to overflow?

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++;
}