Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #29255

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

Newsgroups comp.lang.javascript
Date 2016-01-16 09:42 -0800
References <4f7240ca-c0c5-4ba8-9bda-9026cfe4c3d9@googlegroups.com>
Message-ID <fe4e5a1d-fe10-4d8e-b83b-1fedc290ba3b@googlegroups.com> (permalink)
Subject Re: Need help loop construcion, can not get it to work.
From jonas.thornvall@gmail.com

Show all headers | View raw


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.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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